tesseract 4.1.1
Loading...
Searching...
No Matches
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
25#include "fileerr.h"
26#include "ccutil.h"
27
28namespace tesseract {
29/**********************************************************************
30 * main_setup
31 *
32 * Main for mithras demo program. Read the arguments and set up globals.
33 **********************************************************************/
34
44void CCUtil::main_setup(const char *argv0, const char *basename) {
45 imagebasename = basename;
47 char *tessdata_prefix = getenv("TESSDATA_PREFIX");
48
49 if (argv0 != nullptr && *argv0 != '\0') {
50 /* Use tessdata prefix from the command line. */
51 datadir = argv0;
52 } else if (tessdata_prefix) {
53 /* Use tessdata prefix from the environment. */
54 datadir = tessdata_prefix;
55#if defined(_WIN32)
56 } else if (datadir == nullptr || _access(datadir.string(), 0) != 0) {
57 /* Look for tessdata in directory of executable. */
58 char path[_MAX_PATH];
59 DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
60 if (length > 0 && length < sizeof(path)) {
61 char* separator = std::strrchr(path, '\\');
62 if (separator != nullptr) {
63 *separator = '\0';
64 datadir = path;
65 datadir += "/tessdata";
66 }
67 }
68#endif /* _WIN32 */
69#if defined(TESSDATA_PREFIX)
70 } else {
71/* Use tessdata prefix which was compiled in. */
72#define _STR(a) #a
73#define _XSTR(a) _STR(a)
74 datadir = _XSTR(TESSDATA_PREFIX) "/tessdata";
75#undef _XSTR
76#undef _STR
77#endif
78 }
79
80 // datadir may still be empty:
81 if (datadir.length() == 0) {
82 datadir = "./";
83 }
84
85 // check for missing directory separator
86 const char *lastchar = datadir.string();
87 lastchar += datadir.length() - 1;
88 if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0))
89 datadir += "/";
90}
91} // namespace tesseract
STRING datadir
Definition: ccutil.h:69
STRING imagebasename
Definition: ccutil.h:70
void main_setup(const char *argv0, const char *basename)
CCUtil::main_setup - set location of tessdata and name of image.
Definition: mainblk.cpp:44
int32_t length() const
Definition: strngs.cpp:189
const char * string() const
Definition: strngs.cpp:194