]> Creatis software - crea.git/blob - src/creaSystem.cxx
Add GetDllAppPath(const char *nomdll);
[crea.git] / src / creaSystem.cxx
1 #include "creaSystem.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <iostream>
5
6 #ifdef WIN32
7 #include <windows.h> /* GetModuleFileName */
8 #include <io.h>
9 #endif /* WIN32 */
10
11 #ifdef __APPLE__  /* assume this is OSX */
12 #include <sys/param.h>
13 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
14 CoreFoundation to link line */
15 #include <string.h>
16 # ifndef PATH_MAX
17 #  define PATH_MAX MAXPATHLEN
18 # endif
19 #endif /* APPLE */
20
21 #ifndef PATH_MAX
22 #  define PATH_MAX 2048
23 #endif
24
25 #if defined(WIN32)
26   #include <direct.h>
27 #else
28    #include <dirent.h>  
29 #endif
30
31 #include <stdlib.h>
32
33
34 namespace crea
35 {
36
37 #ifdef _WIN32
38   int System::HasTTY() { return false; }
39 #else  
40 #include <unistd.h>
41    int System::HasTTY() 
42    { 
43      return isatty(fileno(stdin));
44    }
45 #endif
46
47 int System::GetAppPath(char *pname, size_t pathsize)
48    {
49 #ifdef LINUX    
50     /* Oddly, the readlink(2) man page says no NULL is appended. */
51     /* So you have to do it yourself, based on the return value: */
52     pathsize --; /* Preserve a space to add the trailing NULL */
53     long result = readlink("/proc/self/exe", pname, pathsize);
54     if (result > 0)
55         {
56                 pname[result] = 0; /* add the #@!%ing NULL */
57                 
58                 if ((access(pname, 0) == 0))
59                         return 0; /* file exists, return OK */
60                 /*else name doesn't seem to exist, return FAIL (falls
61                  through) */
62         }
63 #endif /* LINUX */
64     
65 #ifdef WIN32
66     long result = GetModuleFileName(NULL, pname, pathsize);
67     if (result > 0)
68         {
69                 /* fix up the dir slashes... */
70                 int len = strlen(pname);
71                 int idx;
72                 for (idx = 0; idx < len; idx++)
73                 {
74                         if (pname[idx] == '\\') pname[idx] = '/';
75                 }
76                 
77                 for (idx = len-1; idx >=0 ; idx--)
78                 {
79                         if (pname[idx] == '/')
80                         { 
81                                 pname[idx+1] = '\0';
82                                 idx = -1;
83                         }
84                 }
85                 
86                 if ((access(pname, 0) == 0))
87                         return 0; /* file exists, return OK */
88                 /*else name doesn't seem to exist, return FAIL (falls
89                  through) */
90         }
91 #endif /* WIN32 */
92     
93 #ifdef SOLARIS
94     char *p = getexecname();
95     if (p)
96         {
97                 /* According to the Sun manpages, getexecname will
98                  "normally" return an */
99                 /* absolute path - BUT might not... AND that IF it is not,
100                  pre-pending */
101                 /* getcwd() will "usually" be the correct thing... Urgh!
102                  */
103                 
104                 /* check pathname is absolute (begins with a / ???) */
105                 if (p[0] == '/') /* assume this means we have an
106                  absolute path */
107                 {
108                         strncpy(pname, p, pathsize);
109                         if ((access(pname, 0) == 0))
110                                 return 0; /* file exists, return OK */
111                 }
112                 else /* if not, prepend getcwd() then check if file
113                  exists */
114                 {
115                         getcwd(pname, pathsize);
116                         long result = strlen(pname);
117                         strncat(pname, "/", (pathsize - result));
118                         result ++;
119                         strncat(pname, p, (pathsize - result));
120                         
121                         if ((access(pname, 0) == 0))
122                                 return 0; /* file exists, return OK */
123                         /*else name doesn't seem to exist, return FAIL
124                          (falls through) */
125                 }
126         }
127 #endif /* SOLARIS */
128     
129 #ifdef MACOSX /* assume this is OSX */
130     /*
131          from http://www.hmug.org/man/3/NSModule.html
132          
133          extern int _NSGetExecutablePath(char *buf, unsigned long
134          *bufsize);
135          
136          _NSGetExecutablePath  copies  the  path  of the executable
137          into the buffer and returns 0 if the path was successfully
138          copied  in the provided buffer. If the buffer is not large
139          enough, -1 is returned and the  expected  buffer  size  is
140          copied  in  *bufsize.  Note that _NSGetExecutablePath will
141          return "a path" to the executable not a "real path" to the
142          executable.  That  is  the path may be a symbolic link and
143          not the real file. And with  deep  directories  the  total
144          bufsize needed could be more than MAXPATHLEN.
145          */
146         
147     int status = -1;
148     char *given_path = (char*)malloc(MAXPATHLEN * 2);
149     if (!given_path) return status;
150     
151     uint32_t npathsize = MAXPATHLEN * 2;
152     long result = _NSGetExecutablePath(given_path, &npathsize);
153     if (result == 0)
154         { /* OK, we got something - now try and resolve the real path...
155          */
156                 if (realpath(given_path, pname) != NULL)
157                 {
158                         if ((access(pname, 0) == 0))
159                                 status = 0; /* file exists, return OK */
160                 }
161         }
162     free (given_path);
163     return status;
164 #endif /* MACOSX */
165     
166     return -1; /* Path Lookup Failed */
167 }
168
169 std::string System::GetDllAppPath(std::string &nomdll){
170         std::string path = ".";
171 #ifdef WIN32
172         char currentPath[_MAX_PATH];
173         HMODULE hand = GetModuleHandle(nomdll.c_str());
174         GetModuleFileName(hand, currentPath, _MAX_PATH);
175
176         path = currentPath;
177
178         path = path.substr(0,path.find_last_of("\\"));
179 #endif
180         return path;
181 }
182
183
184 std::string System::GetDllAppPath(const char *nomdll){
185         std::string path = ".";
186 #ifdef WIN32
187         char currentPath[_MAX_PATH];
188         HMODULE hand = GetModuleHandle(nomdll);
189         GetModuleFileName(hand, currentPath, _MAX_PATH);
190
191         path = currentPath;
192
193         path = path.substr(0,path.find_last_of("\\"));
194 #endif
195         return path;
196 }
197
198
199 #if defined(_WIN32)
200 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
201 #else
202 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
203 #endif  
204         
205         //=========================================================================
206 std::string System::GetExecutablePath(){
207                 char name[PATH_MAX];
208                 //EED    int err = get_app_path(name, PATH_MAX);
209                 int err = System::GetAppPath(name,PATH_MAX);
210                 if (err) 
211                 {
212                         printf("Could not determine current executable path ?  ");  
213                 }    
214                 // remove the exe name
215                 char *slash;
216                 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
217                 if (slash)
218                 {
219                         *slash = 0;
220                 }
221                 return name;
222         }
223
224 } // namespace crea