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