tesseract  5.0.0
mainblk.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: mainblk.cpp (Formerly main.c)
3  * Description: Function to call from main() to setup.
4  * Author: Ray Smith
5  *
6  * (C) Copyright 1991, Hewlett-Packard Ltd.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *
17  **********************************************************************/
18 
19 #include <cstdlib>
20 #include <cstring> // for std::strrchr
21 #if defined(_WIN32)
22 # include <io.h> // for _access
23 #endif
24 #if defined(__riscos__)
25 # include <unixlib/local.h>
26 #endif
27 #include "ccutil.h"
28 #include "fileerr.h"
29 
30 namespace tesseract {
40 void CCUtil::main_setup(const std::string &argv0, const std::string &basename) {
41  imagebasename = basename;
43  char *tessdata_prefix = getenv("TESSDATA_PREFIX");
44 
45  if (!argv0.empty()) {
46  /* Use tessdata prefix from the command line. */
47  datadir = argv0;
48  } else if (tessdata_prefix) {
49  /* Use tessdata prefix from the environment. */
50  datadir = tessdata_prefix;
51 #if defined(_WIN32)
52  } else if (datadir.empty() || _access(datadir.c_str(), 0) != 0) {
53  /* Look for tessdata in directory of executable. */
54  char path[_MAX_PATH];
55  DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
56  if (length > 0 && length < sizeof(path)) {
57  char *separator = std::strrchr(path, '\\');
58  if (separator != nullptr) {
59  *separator = '\0';
60  datadir = path;
61  datadir += "/tessdata";
62  }
63  }
64 #elif defined(__riscos__)
65  } else if (datadir.empty() != 0) {
66  char path[PATH_MAX] = "<Tesseract$Dir>.tessdata";
67  /* Look for tessdata in directory of executable. */
68  datadir = __unixify(path, 0, NULL, 0, 0);
69 #endif /* _WIN32 */
70 #if defined(TESSDATA_PREFIX) && !defined(__riscos__)
71  } else {
72  // Use tessdata prefix which was compiled in.
73  datadir = TESSDATA_PREFIX "/tessdata";
74 #endif
75  }
76 
77  // datadir may still be empty:
78  if (datadir.empty()) {
79  datadir = "./";
80  }
81 
82  // check for missing directory separator
83  const char *lastchar = datadir.c_str();
84  lastchar += datadir.length() - 1;
85  if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0)) {
86  datadir += "/";
87  }
88 }
89 } // namespace tesseract
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: mainblk.cpp:40