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
100 long result = GetModuleFileName(NULL, pname, pathsize);
103 /* fix up the dir slashes... */
104 int len = strlen(pname);
106 for (idx = 0; idx < len; idx++)
108 if (pname[idx] == '\\') pname[idx] = '/';
111 for (idx = len-1; idx >=0 ; idx--)
113 if (pname[idx] == '/')
120 if ((_access(pname, 0) == 0))
121 return 0; /* file exists, return OK */
122 /*else name doesn't seem to exist, return FAIL (falls
128 char *p = getexecname();
131 /* According to the Sun manpages, getexecname will
132 "normally" return an */
133 /* absolute path - BUT might not... AND that IF it is not,
135 /* getcwd() will "usually" be the correct thing... Urgh!
138 /* check pathname is absolute (begins with a / ???) */
139 if (p[0] == '/') /* assume this means we have an
142 strncpy(pname, p, pathsize);
143 if ((access(pname, 0) == 0))
144 return 0; /* file exists, return OK */
146 else /* if not, prepend getcwd() then check if file
149 getcwd(pname, pathsize);
150 long result = strlen(pname);
151 strncat(pname, "/", (pathsize - result));
153 strncat(pname, p, (pathsize - result));
155 if ((access(pname, 0) == 0))
156 return 0; /* file exists, return OK */
157 /*else name doesn't seem to exist, return FAIL
163 #ifdef MACOSX /* assume this is OSX */
165 from http://www.hmug.org/man/3/NSModule.html
167 extern int _NSGetExecutablePath(char *buf, unsigned long
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.
182 char *given_path = (char*)malloc(MAXPATHLEN * 2);
183 if (!given_path) return status;
185 uint32_t npathsize = MAXPATHLEN * 2;
186 long result = _NSGetExecutablePath(given_path, &npathsize);
188 { /* OK, we got something - now try and resolve the real path...
190 if (realpath(given_path, pname) != NULL)
192 if ((access(pname, 0) == 0))
193 status = 0; /* file exists, return OK */
200 return -1; /* Path Lookup Failed */
203 std::string System::GetDllAppPath(std::string &nomdll){
204 std::string path = ".";
206 char currentPath[_MAX_PATH];
207 HMODULE hand = GetModuleHandle(nomdll.c_str());
208 GetModuleFileName(hand, currentPath, _MAX_PATH);
212 path = path.substr(0,path.find_last_of("\\"));
218 std::string System::GetDllAppPath(const char *nomdll){
219 std::string path = ".";
221 char currentPath[_MAX_PATH];
222 HMODULE hand = GetModuleHandle(nomdll);
223 GetModuleFileName(hand, currentPath, _MAX_PATH);
227 path = path.substr(0,path.find_last_of("\\"));
234 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
236 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
239 //=========================================================================
240 std::string System::GetExecutablePath(){
242 //EED int err = get_app_path(name, PATH_MAX);
243 int err = System::GetAppPath(name,PATH_MAX);
246 printf("Could not determine current executable path ? ");
248 // remove the exe name
250 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
258 void System::createDirectory(const char* directorypath){
260 if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS)
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);
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);
278 std::cout<<"directory already exists "<<directorypath<<std::endl;
280 std::string error = "Directory could not be created ";
281 error.append(directorypath);