ccutil/basedir.cpp File Reference

#include "mfcpch.h"
#include "strngs.h"
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "basedir.h"
#include "notdll.h"

Go to the source code of this file.

Functions


Function Documentation

DLLSYM INT8 getpath ( const char *  code,
STRING path 
)

Get dir name of code.

Find the directory of the given executable using the usual path rules. This enables data to be located relative to the code.

Definition at line 38 of file basedir.cpp.

References directory, MAX_PATH, and NULL.

Referenced by init_tesseract(), and main_setup().

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:16 2007 for Tesseract by  doxygen 1.5.1