2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------
28 #include "creaSystem.h"
34 #include <windows.h> /* GetModuleFileName */
40 #include <sys/types.h>
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 */
51 # define PATH_MAX MAXPATHLEN
56 # define PATH_MAX 2048
72 int System::HasTTY() { return false; }
77 return isatty(fileno(stdin));
81 int System::GetAppPath(char *pname, size_t pathsize)
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);
90 pname[result] = 0; /* add the #@!%ing NULL */
92 if ((access(pname, 0) == 0))
93 return 0; /* file exists, return OK */
94 /*else name doesn't seem to exist, return FAIL (falls
103 long result = GetModuleFileName(NULL, pname2, pathsize);
104 // long result = GetModuleFileName(NULL, pname, pathsize);
105 int ret = wcstombs ( pname, pname2, sizeof(pname) );
109 /* fix up the dir slashes... */
110 int len = strlen(pname);
112 for (idx = 0; idx < len; idx++)
114 if (pname[idx] == '\\') pname[idx] = '/';
117 for (idx = len-1; idx >=0 ; idx--)
119 if (pname[idx] == '/')
126 if ((_access(pname, 0) == 0))
127 return 0; /* file exists, return OK */
128 /*else name doesn't seem to exist, return FAIL (falls
134 char *p = getexecname();
137 /* According to the Sun manpages, getexecname will
138 "normally" return an */
139 /* absolute path - BUT might not... AND that IF it is not,
141 /* getcwd() will "usually" be the correct thing... Urgh!
144 /* check pathname is absolute (begins with a / ???) */
145 if (p[0] == '/') /* assume this means we have an
148 strncpy(pname, p, pathsize);
149 if ((access(pname, 0) == 0))
150 return 0; /* file exists, return OK */
152 else /* if not, prepend getcwd() then check if file
155 getcwd(pname, pathsize);
156 long result = strlen(pname);
157 strncat(pname, "/", (pathsize - result));
159 strncat(pname, p, (pathsize - result));
161 if ((access(pname, 0) == 0))
162 return 0; /* file exists, return OK */
163 /*else name doesn't seem to exist, return FAIL
169 #ifdef MACOSX /* assume this is OSX */
171 from http://www.hmug.org/man/3/NSModule.html
173 extern int _NSGetExecutablePath(char *buf, unsigned long
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.
188 char *given_path = (char*)malloc(MAXPATHLEN * 2);
189 if (!given_path) return status;
191 uint32_t npathsize = MAXPATHLEN * 2;
192 long result = _NSGetExecutablePath(given_path, &npathsize);
194 { /* OK, we got something - now try and resolve the real path...
196 if (realpath(given_path, pname) != NULL)
198 if ((access(pname, 0) == 0))
199 status = 0; /* file exists, return OK */
206 return -1; /* Path Lookup Failed */
209 std::string System::GetDllAppPath(std::string &nomdll){
210 std::string path = ".";
212 char currentPath[_MAX_PATH];
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());
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);
228 path = path.substr(0,path.find_last_of("\\"));
234 std::string System::GetDllAppPath(const char *nomdll){
235 std::string path = ".";
237 char currentPath[_MAX_PATH];
240 wchar_t nomdll2[512];
241 mbstowcs(nomdll2,nomdll,strlen(nomdll)+1);
242 HMODULE hand = GetModuleHandle(nomdll2);
243 // HMODULE hand = GetModuleHandle(nomdll);
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);
253 path = path.substr(0,path.find_last_of("\\"));
260 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
262 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
265 //=========================================================================
266 std::string System::GetExecutablePath(){
268 //EED int err = get_app_path(name, PATH_MAX);
269 int err = System::GetAppPath(name,PATH_MAX);
272 printf("Could not determine current executable path ? ");
274 // remove the exe name
276 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
284 void System::createDirectory(const char* directorypath){
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)
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);
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);
310 std::cout<<"directory already exists "<<directorypath<<std::endl;
312 std::string error = "Directory could not be created ";
313 error.append(directorypath);