tesseract  4.0.0-1-g2a2b
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  * Created: Tue Oct 22 11:09:40 BST 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include <cstdlib>
21 #if defined(_WIN32)
22 #include <io.h> // for _access
23 #endif
24 
25 #include "fileerr.h"
26 #include "ccutil.h"
27 
29 "Warning:explicit path for executable will not be used for configs";
30 static const ERRCODE USAGE = "Usage";
31 
32 namespace tesseract {
33 /**********************************************************************
34  * main_setup
35  *
36  * Main for mithras demo program. Read the arguments and set up globals.
37  **********************************************************************/
38 
48 void CCUtil::main_setup(const char *argv0, const char *basename) {
49  imagebasename = basename;
51  char *tessdata_prefix = getenv("TESSDATA_PREFIX");
52 
53  if (argv0 != nullptr && *argv0 != '\0') {
54  /* Use tessdata prefix from the command line. */
55  datadir = argv0;
56  } else if (tessdata_prefix) {
57  /* Use tessdata prefix from the environment. */
58  datadir = tessdata_prefix;
59 #if defined(_WIN32)
60  } else if (datadir == nullptr || _access(datadir.string(), 0) != 0) {
61  /* Look for tessdata in directory of executable. */
62  char drive[_MAX_DRIVE];
63  char dir[_MAX_DIR];
64  char path[_MAX_PATH];
65  DWORD length = GetModuleFileName(nullptr, path, sizeof(path));
66  if (length > 0 && length < sizeof(path)) {
67  errno_t result = _splitpath_s(path, drive, sizeof(drive),
68  dir, sizeof(dir), nullptr, 0, nullptr, 0);
69  if (result == ERANGE) {
70  tprintf("Error: Path too long: %s\n", path);
71  }
72 
73  datadir = drive;
74  datadir += dir;
75  datadir += "/tessdata";
76  }
77 #endif /* _WIN32 */
78 #if defined(TESSDATA_PREFIX)
79  } else {
80 /* Use tessdata prefix which was compiled in. */
81 #define _STR(a) #a
82 #define _XSTR(a) _STR(a)
83  datadir = _XSTR(TESSDATA_PREFIX) "/tessdata";
84 #undef _XSTR
85 #undef _STR
86 #endif
87  }
88 
89  // datadir may still be empty:
90  if (datadir.length() == 0) {
91  datadir = "./";
92  }
93 
94  // check for missing directory separator
95  const char *lastchar = datadir.string();
96  lastchar += datadir.length() - 1;
97  if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0))
98  datadir += "/";
99 }
100 } // namespace tesseract
const ERRCODE NO_PATH
Definition: mainblk.cpp:28
const char * string() const
Definition: strngs.cpp:196
STRING imagebasename
Definition: ccutil.h:65
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
STRING datadir
Definition: ccutil.h:64
void main_setup(const char *argv0, const char *basename)
CCUtil::main_setup - set location of tessdata and name of image.
Definition: mainblk.cpp:48
int32_t length() const
Definition: strngs.cpp:191