tesseract v5.3.3.20231005
ccutil.cpp
Go to the documentation of this file.
1// Copyright 2008 Google Inc. All Rights Reserved.
2// Author: scharron@google.com (Samuel Charron)
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6// http://www.apache.org/licenses/LICENSE-2.0
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13#if defined(_WIN32)
14# include <io.h> // for _access
15#endif
16
17#include "ccutil.h"
18
19#include <cstdlib>
20#include <cstring> // for std::strrchr
21
22namespace tesseract {
23
25 : params_()
26 , INT_INIT_MEMBER(ambigs_debug_level, 0, "Debug level for unichar ambiguities", &params_)
27 , BOOL_MEMBER(use_ambigs_for_adaption, false,
28 "Use ambigs for deciding"
29 " whether to adapt to a character",
30 &params_) {}
31
32// Destructor.
33// It is defined here, so the compiler can create a single vtable
34// instead of weak vtables in every compilation unit.
35CCUtil::~CCUtil() = default;
36
46void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
47 imagebasename = basename;
49 char *tessdata_prefix = getenv("TESSDATA_PREFIX");
50
51 if (!argv0.empty()) {
52 /* Use tessdata prefix from the command line. */
53 datadir = argv0;
54 } else if (tessdata_prefix) {
55 /* Use tessdata prefix from the environment. */
56 datadir = tessdata_prefix;
57#if defined(_WIN32)
58 } else if (datadir.empty() || _access(datadir.c_str(), 0) != 0) {
59 /* Look for tessdata in directory of executable. */
60 char path[_MAX_PATH];
61 DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
62 if (length > 0 && length < sizeof(path)) {
63 char *separator = std::strrchr(path, '\\');
64 if (separator != nullptr) {
65 *separator = '\0';
66 std::string subdir = path;
67 subdir += "/tessdata";
68 if (_access(subdir.c_str(), 0) == 0) {
69 datadir = subdir;
70 }
71 }
72 }
73#endif /* _WIN32 */
74 }
75
76 // datadir may still be empty:
77 if (datadir.empty()) {
78#if defined(TESSDATA_PREFIX)
79 // Use tessdata prefix which was compiled in.
80 datadir = TESSDATA_PREFIX "/tessdata";
81#else
82 datadir = "./";
83#endif /* TESSDATA_PREFIX */
84 }
85
86 // check for missing directory separator
87 const char *lastchar = datadir.c_str();
88 lastchar += datadir.length() - 1;
89 if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0)) {
90 datadir += "/";
91 }
92}
93
94} // namespace tesseract
#define INT_INIT_MEMBER(name, val, comment, vec)
Definition: params.h:377
#define BOOL_MEMBER(name, val, comment, vec)
Definition: params.h:371
std::string imagebasename
Definition: ccutil.h:58
std::string datadir
Definition: ccutil.h:57
void main_setup(const std::string &argv0, const std::string &basename)
CCUtil::main_setup - set location of tessdata and name of image.
Definition: ccutil.cpp:46