]> Creatis software - crea.git/blob - src/creaSystem.cxx
#3204 crea Feature New Normal branch mingw64
[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
101 //2018-07-06 mingw64
102     wchar_t pname2[512];
103     long result = GetModuleFileName(NULL, pname2, pathsize);
104 //    long result = GetModuleFileName(NULL, pname, pathsize);
105         int ret = wcstombs ( pname, pname2, sizeof(pname) );    
106
107     if (result > 0)
108         {
109                 /* fix up the dir slashes... */
110                 int len = strlen(pname);
111                 int idx;
112                 for (idx = 0; idx < len; idx++)
113                 {
114                         if (pname[idx] == '\\') pname[idx] = '/';
115                 }
116                 
117                 for (idx = len-1; idx >=0 ; idx--)
118                 {
119                         if (pname[idx] == '/')
120                         { 
121                                 pname[idx+1] = '\0';
122                                 idx = -1;
123                         }
124                 }
125                 
126                 if ((_access(pname, 0) == 0))
127                         return 0; /* file exists, return OK */
128                 /*else name doesn't seem to exist, return FAIL (falls
129                  through) */
130         }
131 #endif /* WIN32 */
132     
133 #ifdef SOLARIS
134     char *p = getexecname();
135     if (p)
136         {
137                 /* According to the Sun manpages, getexecname will
138                  "normally" return an */
139                 /* absolute path - BUT might not... AND that IF it is not,
140                  pre-pending */
141                 /* getcwd() will "usually" be the correct thing... Urgh!
142                  */
143                 
144                 /* check pathname is absolute (begins with a / ???) */
145                 if (p[0] == '/') /* assume this means we have an
146                  absolute path */
147                 {
148                         strncpy(pname, p, pathsize);
149                         if ((access(pname, 0) == 0))
150                                 return 0; /* file exists, return OK */
151                 }
152                 else /* if not, prepend getcwd() then check if file
153                  exists */
154                 {
155                         getcwd(pname, pathsize);
156                         long result = strlen(pname);
157                         strncat(pname, "/", (pathsize - result));
158                         result ++;
159                         strncat(pname, p, (pathsize - result));
160                         
161                         if ((access(pname, 0) == 0))
162                                 return 0; /* file exists, return OK */
163                         /*else name doesn't seem to exist, return FAIL
164                          (falls through) */
165                 }
166         }
167 #endif /* SOLARIS */
168     
169 #ifdef MACOSX /* assume this is OSX */
170     /*
171          from http://www.hmug.org/man/3/NSModule.html
172          
173          extern int _NSGetExecutablePath(char *buf, unsigned long
174          *bufsize);
175          
176          _NSGetExecutablePath  copies  the  path  of the executable
177          into the buffer and returns 0 if the path was successfully
178          copied  in the provided buffer. If the buffer is not large
179          enough, -1 is returned and the  expected  buffer  size  is
180          copied  in  *bufsize.  Note that _NSGetExecutablePath will
181          return "a path" to the executable not a "real path" to the
182          executable.  That  is  the path may be a symbolic link and
183          not the real file. And with  deep  directories  the  total
184          bufsize needed could be more than MAXPATHLEN.
185          */
186         
187     int status = -1;
188     char *given_path = (char*)malloc(MAXPATHLEN * 2);
189     if (!given_path) return status;
190     
191     uint32_t npathsize = MAXPATHLEN * 2;
192     long result = _NSGetExecutablePath(given_path, &npathsize);
193     if (result == 0)
194         { /* OK, we got something - now try and resolve the real path...
195          */
196                 if (realpath(given_path, pname) != NULL)
197                 {
198                         if ((access(pname, 0) == 0))
199                                 status = 0; /* file exists, return OK */
200                 }
201         }
202     free (given_path);
203     return status;
204 #endif /* MACOSX */
205     
206     return -1; /* Path Lookup Failed */
207 }
208
209 std::string System::GetDllAppPath(std::string &nomdll){
210         std::string path = ".";
211 #ifdef WIN32
212         char currentPath[_MAX_PATH];
213         
214 //2018-07-06 mingw64
215     wchar_t nomdll2[512];
216         mbstowcs(nomdll2,nomdll.c_str(),strlen(nomdll.c_str())+1);
217         HMODULE hand = GetModuleHandle(nomdll2);
218 //      HMODULE hand = GetModuleHandle(nomdll.c_str());
219
220 //2018-07-06 mingw64
221     wchar_t currentPath2[512];
222         GetModuleFileName(hand, currentPath2, _MAX_PATH);
223         int ret = wcstombs ( currentPath, currentPath2, sizeof(pname) );        
224 //      GetModuleFileName(hand, currentPath, _MAX_PATH);
225
226         path = currentPath;
227
228         path = path.substr(0,path.find_last_of("\\"));
229 #endif
230         return path;
231 }
232
233
234 std::string System::GetDllAppPath(const char *nomdll){
235         std::string path = ".";
236 #ifdef WIN32
237         char currentPath[_MAX_PATH];
238
239 //2018-07-06 mingw64
240     wchar_t nomdll2[512];
241         mbstowcs(nomdll2,nomdll,strlen(nomdll)+1);
242         HMODULE hand = GetModuleHandle(nomdll2);
243 //      HMODULE hand = GetModuleHandle(nomdll);
244         
245 //2018-07-06 mingw64
246     wchar_t currentPath2[512];
247         GetModuleFileName(hand, currentPath2, _MAX_PATH);
248         int ret = wcstombs ( currentPath, currentPath2, sizeof(pname) );        
249 //      GetModuleFileName(hand, currentPath, _MAX_PATH);
250
251         path = currentPath;
252
253         path = path.substr(0,path.find_last_of("\\"));
254 #endif
255         return path;
256 }
257
258
259 #if defined(_WIN32)
260 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
261 #else
262 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
263 #endif  
264         
265         //=========================================================================
266 std::string System::GetExecutablePath(){
267                 char name[PATH_MAX];
268                 //EED    int err = get_app_path(name, PATH_MAX);
269                 int err = System::GetAppPath(name,PATH_MAX);
270                 if (err) 
271                 {
272                         printf("Could not determine current executable path ?  ");  
273                 }    
274                 // remove the exe name
275                 char *slash;
276                 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
277                 if (slash)
278                 {
279                         *slash = 0;
280                 }
281                 return name;
282         }
283         
284         void System::createDirectory(const char* directorypath){
285                 #ifdef WIN32
286                 
287 //2018-07-06 mingw64
288     wchar_t directorypath2[512];
289         mbstowcs(directorypath2,directorypath,strlen(directorypath)+1);
290                         if (CreateDirectory(directorypath2, NULL) == ERROR_ALREADY_EXISTS) 
291 //                      if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS) 
292                         { 
293                                 std::cout<<"directory already exists "<<directorypath<<std::endl;
294                         }else if(CreateDirectory(directorypath2, NULL) == ERROR_PATH_NOT_FOUND){
295 //                      }else if(CreateDirectory(directorypath, NULL) == ERROR_PATH_NOT_FOUND){
296                                 std::string error = "Directory could not be created ";
297                                 error.append(directorypath);
298                                 throw error.c_str();
299                         }
300                 #endif
301                 #ifdef LINUX
302                         //! include sys/types.h
303                         //! include sys/stat.h
304                         //! int mkdir(const char *path, mode_t mode);
305                         //! read/write/search permissions for owner and group, and with read/search permissions for others S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
306                         int returnval = mkdir(directorypath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
307                         
308                         if(returnval != 0){
309                                 if(errno == EEXIST){
310                                         std::cout<<"directory already exists "<<directorypath<<std::endl;
311                                 }else{
312                                         std::string error = "Directory could not be created ";
313                                         error.append(directorypath);
314                                         throw error.c_str();
315                                 }
316                         }
317                 #endif
318                 #ifdef MACOSX
319                 //TODO
320                 #endif
321         }
322
323 } // namespace crea