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"
35 #include <sys/types.h>
40 #ifdef __APPLE__ /* assume this is OSX */
41 #include <sys/param.h>
42 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
43 CoreFoundation to link line */
46 # define PATH_MAX MAXPATHLEN
51 # define PATH_MAX 2048
67 int System::HasTTY() { return false; }
72 return isatty(fileno(stdin));
80 //=====================================================================================
81 /* http://forums.codeguru.com/showthread.php?336106-WCHAR*-to-CHAR*
83 || This function will convert a WCHAR string to a CHAR string.
85 || Param 1 :: Pointer to a buffer that will contain the converted string. Ensure this
86 || buffer is large enough; if not, buffer overrun errors will occur.
87 || Param 2 :: Constant pointer to a source WCHAR string to be converted to CHAR
89 void System::wtoc(const WCHAR* Source,CHAR* Dest)
92 while(Source[i] != '\0')
94 Dest[i] = (CHAR)Source[i];
104 int System::GetAppPath(char *pname, size_t pathsize)
107 /* Oddly, the readlink(2) man page says no NULL is appended. */
108 /* So you have to do it yourself, based on the return value: */
109 pathsize --; /* Preserve a space to add the trailing NULL */
110 long result = readlink("/proc/self/exe", pname, pathsize);
113 pname[result] = 0; /* add the #@!%ing NULL */
115 if ((access(pname, 0) == 0))
116 return 0; /* file exists, return OK */
117 /*else name doesn't seem to exist, return FAIL (falls
126 long result = GetModuleFileName(NULL, pname2, pathsize);
127 crea::System::wtoc(pname2,pname);
128 // long result = GetModuleFileName(NULL, pname, pathsize);
132 /* fix up the dir slashes... */
133 int len = strlen(pname);
135 for (idx = 0; idx < len; idx++)
137 if (pname[idx] == '\\') pname[idx] = '/';
140 for (idx = len-1; idx >=0 ; idx--)
142 if (pname[idx] == '/')
149 if ((_access(pname, 0) == 0))
150 return 0; /* file exists, return OK */
151 /*else name doesn't seem to exist, return FAIL (falls
157 char *p = getexecname();
160 /* According to the Sun manpages, getexecname will
161 "normally" return an */
162 /* absolute path - BUT might not... AND that IF it is not,
164 /* getcwd() will "usually" be the correct thing... Urgh!
167 /* check pathname is absolute (begins with a / ???) */
168 if (p[0] == '/') /* assume this means we have an
171 strncpy(pname, p, pathsize);
172 if ((access(pname, 0) == 0))
173 return 0; /* file exists, return OK */
175 else /* if not, prepend getcwd() then check if file
178 getcwd(pname, pathsize);
179 long result = strlen(pname);
180 strncat(pname, "/", (pathsize - result));
182 strncat(pname, p, (pathsize - result));
184 if ((access(pname, 0) == 0))
185 return 0; /* file exists, return OK */
186 /*else name doesn't seem to exist, return FAIL
192 #ifdef MACOSX /* assume this is OSX */
194 from http://www.hmug.org/man/3/NSModule.html
196 extern int _NSGetExecutablePath(char *buf, unsigned long
199 _NSGetExecutablePath copies the path of the executable
200 into the buffer and returns 0 if the path was successfully
201 copied in the provided buffer. If the buffer is not large
202 enough, -1 is returned and the expected buffer size is
203 copied in *bufsize. Note that _NSGetExecutablePath will
204 return "a path" to the executable not a "real path" to the
205 executable. That is the path may be a symbolic link and
206 not the real file. And with deep directories the total
207 bufsize needed could be more than MAXPATHLEN.
211 char *given_path = (char*)malloc(MAXPATHLEN * 2);
212 if (!given_path) return status;
214 uint32_t npathsize = MAXPATHLEN * 2;
215 long result = _NSGetExecutablePath(given_path, &npathsize);
217 { /* OK, we got something - now try and resolve the real path...
219 if (realpath(given_path, pname) != NULL)
221 if ((access(pname, 0) == 0))
222 status = 0; /* file exists, return OK */
229 return -1; /* Path Lookup Failed */
232 std::string System::GetDllAppPath(std::string &nomdll){
233 std::string path = ".";
235 char currentPath[_MAX_PATH];
238 wchar_t nomdll2[512];
239 mbstowcs(nomdll2,nomdll.c_str(),strlen(nomdll.c_str())+1);
240 HMODULE hand = GetModuleHandle(nomdll2);
241 // HMODULE hand = GetModuleHandle(nomdll.c_str());
244 wchar_t currentPath2[512];
245 GetModuleFileName(hand, currentPath2, _MAX_PATH);
246 crea::System::wtoc(currentPath2,currentPath);
247 // GetModuleFileName(hand, currentPath, _MAX_PATH);
250 path = path.substr(0,path.find_last_of("\\"));
256 std::string System::GetDllAppPath(const char *nomdll){
257 std::string path = ".";
259 char currentPath[_MAX_PATH];
262 wchar_t nomdll2[512];
263 mbstowcs(nomdll2,nomdll,strlen(nomdll)+1);
264 HMODULE hand = GetModuleHandle(nomdll2);
265 // HMODULE hand = GetModuleHandle(nomdll);
268 wchar_t currentPath2[512];
269 GetModuleFileName(hand, currentPath2, _MAX_PATH);
270 crea::System::wtoc(currentPath2,currentPath);
271 // GetModuleFileName(hand, currentPath, _MAX_PATH);
273 path = path.substr(0,path.find_last_of("\\"));
279 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
281 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
284 //=========================================================================
285 std::string System::GetExecutablePath()
288 //EED int err = get_app_path(name, PATH_MAX);
289 int err = System::GetAppPath(name,PATH_MAX);
290 printf("EED creaSystem::GetExecutablePath %s \n",name);
293 printf("Could not determine current executable path ? ");
295 // remove the exe name
297 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
305 void System::createDirectory(const char* directorypath){
309 wchar_t directorypath2[512];
310 mbstowcs(directorypath2,directorypath,strlen(directorypath)+1);
311 if (CreateDirectory(directorypath2, NULL) == ERROR_ALREADY_EXISTS)
312 // if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS)
314 std::cout<<"directory already exists "<<directorypath<<std::endl;
315 }else if(CreateDirectory(directorypath2, NULL) == ERROR_PATH_NOT_FOUND){
316 // }else if(CreateDirectory(directorypath, NULL) == ERROR_PATH_NOT_FOUND){
317 std::string error = "Directory could not be created ";
318 error.append(directorypath);
323 //! include sys/types.h
324 //! include sys/stat.h
325 //! int mkdir(const char *path, mode_t mode);
326 //! read/write/search permissions for owner and group, and with read/search permissions for others S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
327 int returnval = mkdir(directorypath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
331 std::cout<<"directory already exists "<<directorypath<<std::endl;
333 std::string error = "Directory could not be created ";
334 error.append(directorypath);