2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* File: basedir.c (Formerly getpath.c)
|
|
|
|
* Description: Find the directory location of the current executable using PATH.
|
|
|
|
* Author: Ray Smith
|
|
|
|
* Created: Mon Jul 09 09:06:39 BST 1990
|
|
|
|
*
|
|
|
|
* (C) Copyright 1990, Hewlett-Packard Ltd.
|
|
|
|
** Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
** you may not use this file except in compliance with the License.
|
|
|
|
** You may obtain a copy of the License at
|
|
|
|
** http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
** Unless required by applicable law or agreed to in writing, software
|
|
|
|
** distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
** See the License for the specific language governing permissions and
|
|
|
|
** limitations under the License.
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "mfcpch.h" //precompiled headers
|
|
|
|
#include "strngs.h"
|
|
|
|
#ifdef __UNIX__
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "basedir.h"
|
2009-06-03 06:07:35 +08:00
|
|
|
#include "varable.h"
|
2007-03-08 04:03:40 +08:00
|
|
|
#include "notdll.h" //must be last include
|
|
|
|
|
2009-06-03 06:07:35 +08:00
|
|
|
#ifdef __MSW32__
|
|
|
|
STRING_VAR(tessedit_module_name, "tessdll.dll",
|
|
|
|
"Module colocated with tessdata dir");
|
|
|
|
#endif
|
|
|
|
|
2007-03-08 04:03:40 +08:00
|
|
|
/**********************************************************************
|
|
|
|
* getpath
|
|
|
|
*
|
|
|
|
* Find the directory of the given executable using the usual path rules.
|
|
|
|
* This enables data to be located relative to the code.
|
|
|
|
**********************************************************************/
|
|
|
|
|
2008-04-22 08:23:41 +08:00
|
|
|
DLLSYM inT8 getpath( //get dir name of code
|
2007-03-08 04:03:40 +08:00
|
|
|
const char *code, //executable to locate
|
|
|
|
STRING &path //output path name
|
|
|
|
) {
|
|
|
|
char directory[MAX_PATH]; //main directory
|
|
|
|
#ifdef __UNIX__
|
2008-04-22 08:23:41 +08:00
|
|
|
inT16 dirind; //index in directory
|
2007-03-08 04:03:40 +08:00
|
|
|
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
|
2008-04-22 08:23:41 +08:00
|
|
|
strcat(directory, code);
|
2007-03-08 04:03:40 +08:00
|
|
|
fd = open (directory, 0);
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd); //found it
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
strcpy (directory, "./");
|
|
|
|
path = directory; //in current?
|
2008-04-22 08:23:41 +08:00
|
|
|
strcat(directory, code);
|
2007-03-08 04:03:40 +08:00
|
|
|
fd = open (directory, 0);
|
|
|
|
if (fd >= 0) {
|
2008-04-22 08:23:41 +08:00
|
|
|
close(fd);
|
2007-03-08 04:03:40 +08:00
|
|
|
return 0; //in current after all
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
#ifdef __MSW32__
|
|
|
|
char *path_end; //end of dir
|
|
|
|
|
2009-06-03 06:07:35 +08:00
|
|
|
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(tessedit_module_name.string()),
|
|
|
|
directory, MAX_PATH - 1) == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
2009-06-03 10:50:54 +08:00
|
|
|
strcpy(directory, code);
|
2007-03-08 04:03:40 +08:00
|
|
|
}
|
2009-06-03 10:50:54 +08:00
|
|
|
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;
|
2007-03-08 04:03:40 +08:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|