Tesseract  3.02
tesseract-ocr/ccutil/basedir.cpp
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        basedir.c  (Formerly getpath.c)
00003  * Description: Find the directory location of the current executable using PATH.
00004  * Author:      Ray Smith
00005  * Created:     Mon Jul 09 09:06:39 BST 1990
00006  *
00007  * (C) Copyright 1990, 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          "mfcpch.h"     //precompiled headers
00021 #include          "strngs.h"
00022 #ifdef __UNIX__
00023 #include          <unistd.h>
00024 #include                    <fcntl.h>
00025 #else
00026 #include          <io.h>
00027 #endif
00028 #include          <stdlib.h>
00029 #include          "basedir.h"
00030 #include          "params.h"
00031 #include          "notdll.h"     //must be last include
00032 
00033 /**********************************************************************
00034  * getpath
00035  *
00036  * Find the directory of the given executable using the usual path rules.
00037  * This enables data to be located relative to the code.
00038  **********************************************************************/
00039 
00040 DLLSYM inT8 getpath(                   //get dir name of code
00041                     const char *code,  //executable to locate
00042                     const STRING &dll_module_name,
00043                     STRING &path       //output path name
00044                    ) {
00045   char directory[MAX_PATH];      //main directory
00046   #ifdef __UNIX__
00047   inT16 dirind;                  //index in directory
00048   register char *pathlist;       //$PATH
00049   int fd;                        //file descriptor
00050 
00051   strcpy(directory, code);  //get demo directory
00052   dirind = strlen (directory);
00053   while (dirind > 0 && directory[dirind - 1] != '/')
00054     dirind--;                    //look back for dirname
00055   directory[dirind] = '\0';      //end at directory
00056   if (dirind != 0) {
00057     path = directory;            //had it in arg
00058     return 0;
00059   }
00060   pathlist = getenv ("PATH");    //find search path
00061   while (pathlist != NULL && *pathlist) {
00062     for (dirind = 0; *pathlist != '\0' && *pathlist != ':';)
00063                                  //copy a directory
00064       directory[dirind++] = *pathlist++;
00065     if (*pathlist == ':')
00066       pathlist++;
00067     if (dirind == 0)
00068       continue;
00069     if (directory[dirind - 1] != '/');
00070     directory[dirind++] = '/';   //add ending slash
00071     directory[dirind++] = '\0';
00072     path = directory;            //try this path
00073     strcat(directory, code);
00074     fd = open (directory, 0);
00075     if (fd >= 0) {
00076       close(fd);  //found it
00077       return 0;
00078     }
00079   }
00080   strcpy (directory, "./");
00081   path = directory;              //in current?
00082   strcat(directory, code);
00083   fd = open (directory, 0);
00084   if (fd >= 0) {
00085     close(fd);
00086     return 0;                    //in current after all
00087   }
00088   return -1;
00089   #endif
00090   #ifdef _WIN32
00091   char *path_end;                //end of dir
00092 
00093   if (code == NULL) {
00094     // Attempt to get the path of the most relevant module. If the dll
00095     // is being used, this will be the dll. Otherwise GetModuleHandle will
00096     // return NULL and default to the path of the executable.
00097     if (GetModuleFileName(GetModuleHandle(dll_module_name.string()),
00098                           directory, MAX_PATH - 1) == 0) {
00099       return -1;
00100     }
00101   } else {
00102     strncpy(directory, code, MAX_PATH - 1);
00103   }
00104   while ((path_end = strchr (directory, '\\')) != NULL)
00105     *path_end = '/';
00106   path_end = strrchr (directory, '/');
00107   if (path_end != NULL)
00108     path_end[1] = '\0';
00109   else
00110     strcpy (directory, "./");
00111   path = directory;
00112   return 0;
00113   #endif
00114 }