ccutil/basedir.cpp

Go to the documentation of this file.
00001 
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          "notdll.h"     //must be last include
00031 
00038 DLLSYM INT8 getpath(                   //get dir name of code
00039                     const char *code,  //executable to locate
00040                     STRING &path       //output path name
00041                    ) {
00042   char directory[MAX_PATH];      //main directory
00043   #ifdef __UNIX__
00044   INT16 dirind;                  //index in directory
00045   register char *pathlist;       //$PATH
00046   int fd;                        //file descriptor
00047 
00048   strcpy(directory, code);  //get demo directory
00049   dirind = strlen (directory);
00050   while (dirind > 0 && directory[dirind - 1] != '/')
00051     dirind--;                    //look back for dirname
00052   directory[dirind] = '\0';      //end at directory
00053   if (dirind != 0) {
00054     path = directory;            //had it in arg
00055     return 0;
00056   }
00057   pathlist = getenv ("PATH");    //find search path
00058   while (pathlist != NULL && *pathlist) {
00059     for (dirind = 0; *pathlist != '\0' && *pathlist != ':';)
00060                                  //copy a directory
00061       directory[dirind++] = *pathlist++;
00062     if (*pathlist == ':')
00063       pathlist++;
00064     if (dirind == 0)
00065       continue;
00066     if (directory[dirind - 1] != '/');
00067     directory[dirind++] = '/';   //add ending slash
00068     directory[dirind++] = '\0';
00069     path = directory;            //try this path
00070     strcat(directory, code); 
00071     fd = open (directory, 0);
00072     if (fd >= 0) {
00073       close(fd);  //found it
00074       return 0;
00075     }
00076   }
00077   strcpy (directory, "./");
00078   path = directory;              //in current?
00079   strcat(directory, code); 
00080   fd = open (directory, 0);
00081   if (fd >= 0) {
00082     close(fd); 
00083     return 0;                    //in current after all
00084   }
00085   return -1;
00086   #endif
00087   #ifdef __MSW32__
00088   char *path_end;                //end of dir
00089 
00090   if (GetModuleFileName (NULL, directory, MAX_PATH - 1) == 0) {
00091     return -1;
00092   }
00093   while ((path_end = strchr (directory, '\\')) != NULL)
00094     *path_end = '/';
00095   path_end = strrchr (directory, '/');
00096   if (path_end != NULL)
00097     path_end[1] = '\0';
00098   else
00099     strcpy (directory, "./");
00100   path = directory;
00101   return 0;
00102   #endif
00103 }

Generated on Wed Feb 28 19:49:08 2007 for Tesseract by  doxygen 1.5.1