1 /**********************************************************************
2 * File: basedir.c (Formerly getpath.c)
3 * Description: Find the directory location of the current executable using PATH.
4 * Author: Ray Smith
5 * Created: Mon Jul 09 09:06:39 BST 1990
6 *
7 * (C) Copyright 1990, Hewlett-Packard Ltd.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 **********************************************************************/
19
20 #include "mfcpch.h" //precompiled headers
21 #include "strngs.h"
22 #ifdef __UNIX__
23 #include <unistd.h>
24 #include <fcntl.h>
25 #endif
26 #include <stdlib.h>
27 #include "basedir.h"
28 #include "varable.h"
29 #include "notdll.h" //must be last include
30
31 #ifdef __MSW32__
32 STRING_VAR(tessedit_module_name, "tessdll.dll",
33 "Module colocated with tessdata dir");
34 #endif
35
36 /**********************************************************************
37 * getpath
38 *
39 * Find the directory of the given executable using the usual path rules.
40 * This enables data to be located relative to the code.
41 **********************************************************************/
42
getpath(const char * code,STRING & path)43 DLLSYM inT8 getpath( //get dir name of code
44 const char *code, //executable to locate
45 STRING &path //output path name
46 ) {
47 char directory[MAX_PATH]; //main directory
48 #ifdef __UNIX__
49 inT16 dirind; //index in directory
50 register char *pathlist; //$PATH
51 int fd; //file descriptor
52
53 strcpy(directory, code); //get demo directory
54 dirind = strlen (directory);
55 while (dirind > 0 && directory[dirind - 1] != '/')
56 dirind--; //look back for dirname
57 directory[dirind] = '\0'; //end at directory
58 if (dirind != 0) {
59 path = directory; //had it in arg
60 return 0;
61 }
62 pathlist = getenv ("PATH"); //find search path
63 while (pathlist != NULL && *pathlist) {
64 for (dirind = 0; *pathlist != '\0' && *pathlist != ':';)
65 //copy a directory
66 directory[dirind++] = *pathlist++;
67 if (*pathlist == ':')
68 pathlist++;
69 if (dirind == 0)
70 continue;
71 if (directory[dirind - 1] != '/');
72 directory[dirind++] = '/'; //add ending slash
73 directory[dirind++] = '\0';
74 path = directory; //try this path
75 strcat(directory, code);
76 fd = open (directory, 0);
77 if (fd >= 0) {
78 close(fd); //found it
79 return 0;
80 }
81 }
82 strcpy (directory, "./");
83 path = directory; //in current?
84 strcat(directory, code);
85 fd = open (directory, 0);
86 if (fd >= 0) {
87 close(fd);
88 return 0; //in current after all
89 }
90 return -1;
91 #endif
92 #ifdef __MSW32__
93 char *path_end; //end of dir
94
95 if (code == NULL) {
96 // Attempt to get the path of the most relevant module. If the dll
97 // is being used, this will be the dll. Otherwise GetModuleHandle will
98 // return NULL and default to the path of the executable.
99 if (GetModuleFileName(GetModuleHandle(tessedit_module_name.string()),
100 directory, MAX_PATH - 1) == 0) {
101 return -1;
102 }
103 } else {
104 strncpy(directory, code, MAX_PATH - 1);
105 }
106 while ((path_end = strchr (directory, '\\')) != NULL)
107 *path_end = '/';
108 path_end = strrchr (directory, '/');
109 if (path_end != NULL)
110 path_end[1] = '\0';
111 else
112 strcpy (directory, "./");
113 path = directory;
114 return 0;
115 #endif
116 }
117