]> Creatis software - crea.git/blob - src/creaSystem.cxx
b387112c857ef24b61bd65879ba0e897b50467c5
[crea.git] / src / creaSystem.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 #
8 #  This software is governed by the CeCILL-B license under French law and 
9 #  abiding by the rules of distribution of free software. You can  use, 
10 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
11 #  license as circulated by CEA, CNRS and INRIA at the following URL 
12 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
13 #  or in the file LICENSE.txt.
14 #
15 #  As a counterpart to the access to the source code and  rights to copy,
16 #  modify and redistribute granted by the license, users are provided only
17 #  with a limited warranty  and the software's author,  the holder of the
18 #  economic rights,  and the successive licensors  have only  limited
19 #  liability. 
20 #
21 #  The fact that you are presently reading this means that you have had
22 #  knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */ 
24
25 #include "creaSystem.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <iostream>
29
30 #ifdef WIN32
31         #include <windows.h> /* GetModuleFileName */
32         #include <io.h>
33         
34 #endif /* WIN32 */
35
36 #ifdef LINUX
37         #include <sys/types.h>
38         #include <sys/stat.h>
39         #include <errno.h> 
40 #endif 
41
42 #ifdef __APPLE__  /* assume this is OSX */
43 #include <sys/param.h>
44 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
45 CoreFoundation to link line */
46 #include <string.h>
47 # ifndef PATH_MAX
48 #  define PATH_MAX MAXPATHLEN
49 # endif
50 #endif /* APPLE */
51
52 #ifndef PATH_MAX
53 #  define PATH_MAX 2048
54 #endif
55
56 #if defined(WIN32)
57   #include <direct.h>
58 #else
59    #include <dirent.h>  
60 #endif
61
62 #include <stdlib.h>
63
64
65 namespace crea
66 {
67
68 #ifdef _WIN32
69   int System::HasTTY() { return false; }
70 #else  
71 #include <unistd.h>
72    int System::HasTTY() 
73    { 
74      return isatty(fileno(stdin));
75    }
76 #endif
77
78 int System::GetAppPath(char *pname, size_t pathsize)
79    {
80 #ifdef LINUX    
81     /* Oddly, the readlink(2) man page says no NULL is appended. */
82     /* So you have to do it yourself, based on the return value: */
83     pathsize --; /* Preserve a space to add the trailing NULL */
84     long result = readlink("/proc/self/exe", pname, pathsize);
85     if (result > 0)
86         {
87                 pname[result] = 0; /* add the #@!%ing NULL */
88                 
89                 if ((access(pname, 0) == 0))
90                         return 0; /* file exists, return OK */
91                 /*else name doesn't seem to exist, return FAIL (falls
92                  through) */
93         }
94 #endif /* LINUX */
95     
96 #ifdef WIN32
97     long result = GetModuleFileName(NULL, pname, pathsize);
98     if (result > 0)
99         {
100                 /* fix up the dir slashes... */
101                 int len = strlen(pname);
102                 int idx;
103                 for (idx = 0; idx < len; idx++)
104                 {
105                         if (pname[idx] == '\\') pname[idx] = '/';
106                 }
107                 
108                 for (idx = len-1; idx >=0 ; idx--)
109                 {
110                         if (pname[idx] == '/')
111                         { 
112                                 pname[idx+1] = '\0';
113                                 idx = -1;
114                         }
115                 }
116                 
117                 if ((access(pname, 0) == 0))
118                         return 0; /* file exists, return OK */
119                 /*else name doesn't seem to exist, return FAIL (falls
120                  through) */
121         }
122 #endif /* WIN32 */
123     
124 #ifdef SOLARIS
125     char *p = getexecname();
126     if (p)
127         {
128                 /* According to the Sun manpages, getexecname will
129                  "normally" return an */
130                 /* absolute path - BUT might not... AND that IF it is not,
131                  pre-pending */
132                 /* getcwd() will "usually" be the correct thing... Urgh!
133                  */
134                 
135                 /* check pathname is absolute (begins with a / ???) */
136                 if (p[0] == '/') /* assume this means we have an
137                  absolute path */
138                 {
139                         strncpy(pname, p, pathsize);
140                         if ((access(pname, 0) == 0))
141                                 return 0; /* file exists, return OK */
142                 }
143                 else /* if not, prepend getcwd() then check if file
144                  exists */
145                 {
146                         getcwd(pname, pathsize);
147                         long result = strlen(pname);
148                         strncat(pname, "/", (pathsize - result));
149                         result ++;
150                         strncat(pname, p, (pathsize - result));
151                         
152                         if ((access(pname, 0) == 0))
153                                 return 0; /* file exists, return OK */
154                         /*else name doesn't seem to exist, return FAIL
155                          (falls through) */
156                 }
157         }
158 #endif /* SOLARIS */
159     
160 #ifdef MACOSX /* assume this is OSX */
161     /*
162          from http://www.hmug.org/man/3/NSModule.html
163          
164          extern int _NSGetExecutablePath(char *buf, unsigned long
165          *bufsize);
166          
167          _NSGetExecutablePath  copies  the  path  of the executable
168          into the buffer and returns 0 if the path was successfully
169          copied  in the provided buffer. If the buffer is not large
170          enough, -1 is returned and the  expected  buffer  size  is
171          copied  in  *bufsize.  Note that _NSGetExecutablePath will
172          return "a path" to the executable not a "real path" to the
173          executable.  That  is  the path may be a symbolic link and
174          not the real file. And with  deep  directories  the  total
175          bufsize needed could be more than MAXPATHLEN.
176          */
177         
178     int status = -1;
179     char *given_path = (char*)malloc(MAXPATHLEN * 2);
180     if (!given_path) return status;
181     
182     uint32_t npathsize = MAXPATHLEN * 2;
183     long result = _NSGetExecutablePath(given_path, &npathsize);
184     if (result == 0)
185         { /* OK, we got something - now try and resolve the real path...
186          */
187                 if (realpath(given_path, pname) != NULL)
188                 {
189                         if ((access(pname, 0) == 0))
190                                 status = 0; /* file exists, return OK */
191                 }
192         }
193     free (given_path);
194     return status;
195 #endif /* MACOSX */
196     
197     return -1; /* Path Lookup Failed */
198 }
199
200 std::string System::GetDllAppPath(std::string &nomdll){
201         std::string path = ".";
202 #ifdef WIN32
203         char currentPath[_MAX_PATH];
204         HMODULE hand = GetModuleHandle(nomdll.c_str());
205         GetModuleFileName(hand, currentPath, _MAX_PATH);
206
207         path = currentPath;
208
209         path = path.substr(0,path.find_last_of("\\"));
210 #endif
211         return path;
212 }
213
214
215 std::string System::GetDllAppPath(const char *nomdll){
216         std::string path = ".";
217 #ifdef WIN32
218         char currentPath[_MAX_PATH];
219         HMODULE hand = GetModuleHandle(nomdll);
220         GetModuleFileName(hand, currentPath, _MAX_PATH);
221
222         path = currentPath;
223
224         path = path.substr(0,path.find_last_of("\\"));
225 #endif
226         return path;
227 }
228
229
230 #if defined(_WIN32)
231 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
232 #else
233 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
234 #endif  
235         
236         //=========================================================================
237 std::string System::GetExecutablePath(){
238                 char name[PATH_MAX];
239                 //EED    int err = get_app_path(name, PATH_MAX);
240                 int err = System::GetAppPath(name,PATH_MAX);
241                 if (err) 
242                 {
243                         printf("Could not determine current executable path ?  ");  
244                 }    
245                 // remove the exe name
246                 char *slash;
247                 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
248                 if (slash)
249                 {
250                         *slash = 0;
251                 }
252                 return name;
253         }
254         
255         void System::createDirectory(const char* directorypath){
256                 #ifdef WIN32
257                         if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS) 
258                         { 
259                                 std::cout<<"directory already exists "<<directorypath<<std::endl;
260                         }else if(CreateDirectory(directorypath, NULL) == ERROR_PATH_NOT_FOUND){
261                                 std::string error = "Directory could not be created ";
262                                 error.append(directorypath);
263                                 throw error.c_str();
264                         }
265                 #endif
266                 #ifdef LINUX
267                         //! include sys/types.h
268                         //! include sys/stat.h
269                         //! int mkdir(const char *path, mode_t mode);
270                         //! read/write/search permissions for owner and group, and with read/search permissions for others S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
271                         int returnval = mkdir(directorypath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
272                         
273                         if(returnval != 0){
274                                 if(errno == EEXIST){
275                                         std::cout<<"directory already exists "<<directorypath<<std::endl;
276                                 }else{
277                                         std::string error = "Directory could not be created ";
278                                         error.append(directorypath);
279                                         throw error.c_str();
280                                 }
281                         }
282                 #endif
283                 #ifdef MACOSX
284                 //TODO
285                 #endif
286         }
287
288 } // namespace crea