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