tesseract  4.0.0-beta.1-59-g2cc4
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 "fileerr.h"
21 #ifdef __UNIX__
22 #include <unistd.h>
23 #include <signal.h>
24 #else
25 #include <io.h>
26 #endif
27 #include <stdlib.h>
28 #include "ccutil.h"
29 
30 #define VARDIR "configs/"
31 #define EXTERN
32 
34 "Warning:explicit path for executable will not be used for configs";
35 static const ERRCODE USAGE = "Usage";
36 
37 namespace tesseract {
38 /**********************************************************************
39  * main_setup
40  *
41  * Main for mithras demo program. Read the arguments and set up globals.
42  **********************************************************************/
43 
53 void CCUtil::main_setup(const char *argv0, const char *basename) {
54  imagebasename = basename;
56  char *tessdata_prefix = getenv("TESSDATA_PREFIX");
57 
58  if (argv0 != NULL && *argv0 != '\0') {
59  /* Use tessdata prefix from the command line. */
60  datadir = argv0;
61  } else if (tessdata_prefix) {
62  /* Use tessdata prefix from the environment. */
63  datadir = tessdata_prefix;
64 #if defined(_WIN32)
65  } else if (datadir == NULL || access(datadir.string(), 0) != 0) {
66  /* Look for tessdata in directory of executable. */
67  static char dir[128];
68  static char exe[128];
69  DWORD length = GetModuleFileName(NULL, exe, sizeof(exe));
70  if (length > 0 && length < sizeof(exe)) {
71  _splitpath(exe, NULL, dir, NULL, NULL);
72  datadir = dir;
73  }
74 #endif /* _WIN32 */
75 #if defined(TESSDATA_PREFIX)
76  } else {
77 /* Use tessdata prefix which was compiled in. */
78 #define _STR(a) #a
79 #define _XSTR(a) _STR(a)
80  datadir = _XSTR(TESSDATA_PREFIX) "/tessdata";
81 #undef _XSTR
82 #undef _STR
83 #endif
84  }
85 
86  // datadir may still be empty:
87  if (datadir.length() == 0) {
88  datadir = "./";
89  }
90 
91  // check for missing directory separator
92  const char *lastchar = datadir.string();
93  lastchar += datadir.length() - 1;
94  if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0))
95  datadir += "/";
96 }
97 } // namespace tesseract
void main_setup(const char *argv0, const char *basename)
CCUtil::main_setup - set location of tessdata and name of image.
Definition: mainblk.cpp:53
const ERRCODE NO_PATH
Definition: mainblk.cpp:33
STRING imagebasename
Definition: ccutil.h:65
STRING datadir
Definition: ccutil.h:64
const char * string() const
Definition: strngs.cpp:198
int32_t length() const
Definition: strngs.cpp:193