Tesseract  3.02
tesseract-ocr/ccutil/basedir.cpp File Reference
#include "mfcpch.h"
#include "strngs.h"
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include "basedir.h"
#include "params.h"
#include "notdll.h"

Go to the source code of this file.

Functions

DLLSYM inT8 getpath (const char *code, const STRING &dll_module_name, STRING &path)

Function Documentation

DLLSYM inT8 getpath ( const char *  code,
const STRING dll_module_name,
STRING path 
)

Definition at line 40 of file basedir.cpp.

                     {
  char directory[MAX_PATH];      //main directory
  #ifdef __UNIX__
  inT16 dirind;                  //index in directory
  register char *pathlist;       //$PATH
  int fd;                        //file descriptor

  strcpy(directory, code);  //get demo directory
  dirind = strlen (directory);
  while (dirind > 0 && directory[dirind - 1] != '/')
    dirind--;                    //look back for dirname
  directory[dirind] = '\0';      //end at directory
  if (dirind != 0) {
    path = directory;            //had it in arg
    return 0;
  }
  pathlist = getenv ("PATH");    //find search path
  while (pathlist != NULL && *pathlist) {
    for (dirind = 0; *pathlist != '\0' && *pathlist != ':';)
                                 //copy a directory
      directory[dirind++] = *pathlist++;
    if (*pathlist == ':')
      pathlist++;
    if (dirind == 0)
      continue;
    if (directory[dirind - 1] != '/');
    directory[dirind++] = '/';   //add ending slash
    directory[dirind++] = '\0';
    path = directory;            //try this path
    strcat(directory, code);
    fd = open (directory, 0);
    if (fd >= 0) {
      close(fd);  //found it
      return 0;
    }
  }
  strcpy (directory, "./");
  path = directory;              //in current?
  strcat(directory, code);
  fd = open (directory, 0);
  if (fd >= 0) {
    close(fd);
    return 0;                    //in current after all
  }
  return -1;
  #endif
  #ifdef _WIN32
  char *path_end;                //end of dir

  if (code == NULL) {
    // Attempt to get the path of the most relevant module. If the dll
    // is being used, this will be the dll. Otherwise GetModuleHandle will
    // return NULL and default to the path of the executable.
    if (GetModuleFileName(GetModuleHandle(dll_module_name.string()),
                          directory, MAX_PATH - 1) == 0) {
      return -1;
    }
  } else {
    strncpy(directory, code, MAX_PATH - 1);
  }
  while ((path_end = strchr (directory, '\\')) != NULL)
    *path_end = '/';
  path_end = strrchr (directory, '/');
  if (path_end != NULL)
    path_end[1] = '\0';
  else
    strcpy (directory, "./");
  path = directory;
  return 0;
  #endif
}