tesseract 3.04.01

ccutil/mainblk.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        mainblk.c  (Formerly main.c)
00003  * Description: Function to call from main() to setup.
00004  * Author:      Ray Smith
00005  * Created:     Tue Oct 22 11:09:40 BST 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #include          "fileerr.h"
00021 #ifdef __UNIX__
00022 #include          <unistd.h>
00023 #include          <signal.h>
00024 #else
00025 #include          <io.h>
00026 #endif
00027 #include          <stdlib.h>
00028 #include          "ccutil.h"
00029 
00030 #define VARDIR        "configs/" 
00031 #define EXTERN
00032 
00033 const ERRCODE NO_PATH =
00034 "Warning:explicit path for executable will not be used for configs";
00035 static const ERRCODE USAGE = "Usage";
00036 
00037 namespace tesseract {
00038 /**********************************************************************
00039  * main_setup
00040  *
00041  * Main for mithras demo program. Read the arguments and set up globals.
00042  **********************************************************************/
00043 
00053 void CCUtil::main_setup(const char *argv0, const char *basename) {
00054   imagebasename = basename;      
00056   char *tessdata_prefix = getenv("TESSDATA_PREFIX");
00057 
00058   if (argv0 != NULL) {
00059     /* Use tessdata prefix from the command line. */
00060     datadir = argv0;
00061   } else if (tessdata_prefix) {
00062     /* Use tessdata prefix from the environment. */
00063     datadir = tessdata_prefix;
00064 #if defined(_WIN32)
00065   } else if (datadir == NULL || access(datadir.string(), 0) != 0) {
00066     /* Look for tessdata in directory of executable. */
00067     static char dir[128];
00068     static char exe[128];
00069     DWORD length = GetModuleFileName(NULL, exe, sizeof(exe));
00070     if (length > 0 && length < sizeof(exe)) {
00071       _splitpath(exe, NULL, dir, NULL, NULL);
00072       datadir = dir;
00073     }
00074 #endif /* _WIN32 */
00075 #if defined(TESSDATA_PREFIX)
00076   } else {
00077     /* Use tessdata prefix which was compiled in. */
00078 #define _STR(a) #a
00079 #define _XSTR(a) _STR(a)
00080     datadir = _XSTR(TESSDATA_PREFIX);
00081 #undef _XSTR
00082 #undef _STR
00083 #endif
00084   }
00085 
00086   // datadir may still be empty:
00087   if (datadir.length() == 0) {
00088     datadir = "./";
00089   } else {
00090     // Remove tessdata from the end if present, as we will add it back!
00091     int length = datadir.length();
00092     if (length >= 8 && strcmp(&datadir[length - 8], "tessdata") == 0)
00093       datadir.truncate_at(length - 8);
00094     else if (length >= 9 && strcmp(&datadir[length - 9], "tessdata/") == 0)
00095       datadir.truncate_at(length - 9);
00096   }
00097 
00098   // check for missing directory separator
00099   const char *lastchar = datadir.string();
00100   lastchar += datadir.length() - 1;
00101   if ((strcmp(lastchar, "/") != 0) && (strcmp(lastchar, "\\") != 0))
00102     datadir += "/";
00103 
00104   datadir += m_data_sub_dir;     
00105 }
00106 }  // namespace tesseract
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines