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