2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
8 # This software is governed by the CeCILL-B license under French law and
9 # abiding by the rules of distribution of free software. You can use,
10 # modify and/ or redistribute the software under the terms of the CeCILL-B
11 # license as circulated by CEA, CNRS and INRIA at the following URL
12 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
13 # or in the file LICENSE.txt.
15 # As a counterpart to the access to the source code and rights to copy,
16 # modify and redistribute granted by the license, users are provided only
17 # with a limited warranty and the software's author, the holder of the
18 # economic rights, and the successive licensors have only limited
21 # The fact that you are presently reading this means that you have had
22 # knowledge of the CeCILL-B license and that you accept its terms.
23 # ------------------------------------------------------------------------ */
25 #include "creaSystem.h"
31 #include <windows.h> /* GetModuleFileName */
37 #include <sys/types.h>
42 #ifdef __APPLE__ /* assume this is OSX */
43 #include <sys/param.h>
44 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
45 CoreFoundation to link line */
48 # define PATH_MAX MAXPATHLEN
53 # define PATH_MAX 2048
69 int System::HasTTY() { return false; }
74 return isatty(fileno(stdin));
78 int System::GetAppPath(char *pname, size_t pathsize)
81 /* Oddly, the readlink(2) man page says no NULL is appended. */
82 /* So you have to do it yourself, based on the return value: */
83 pathsize --; /* Preserve a space to add the trailing NULL */
84 long result = readlink("/proc/self/exe", pname, pathsize);
87 pname[result] = 0; /* add the #@!%ing NULL */
89 if ((access(pname, 0) == 0))
90 return 0; /* file exists, return OK */
91 /*else name doesn't seem to exist, return FAIL (falls
97 long result = GetModuleFileName(NULL, pname, pathsize);
100 /* fix up the dir slashes... */
101 int len = strlen(pname);
103 for (idx = 0; idx < len; idx++)
105 if (pname[idx] == '\\') pname[idx] = '/';
108 for (idx = len-1; idx >=0 ; idx--)
110 if (pname[idx] == '/')
117 if ((access(pname, 0) == 0))
118 return 0; /* file exists, return OK */
119 /*else name doesn't seem to exist, return FAIL (falls
125 char *p = getexecname();
128 /* According to the Sun manpages, getexecname will
129 "normally" return an */
130 /* absolute path - BUT might not... AND that IF it is not,
132 /* getcwd() will "usually" be the correct thing... Urgh!
135 /* check pathname is absolute (begins with a / ???) */
136 if (p[0] == '/') /* assume this means we have an
139 strncpy(pname, p, pathsize);
140 if ((access(pname, 0) == 0))
141 return 0; /* file exists, return OK */
143 else /* if not, prepend getcwd() then check if file
146 getcwd(pname, pathsize);
147 long result = strlen(pname);
148 strncat(pname, "/", (pathsize - result));
150 strncat(pname, p, (pathsize - result));
152 if ((access(pname, 0) == 0))
153 return 0; /* file exists, return OK */
154 /*else name doesn't seem to exist, return FAIL
160 #ifdef MACOSX /* assume this is OSX */
162 from http://www.hmug.org/man/3/NSModule.html
164 extern int _NSGetExecutablePath(char *buf, unsigned long
167 _NSGetExecutablePath copies the path of the executable
168 into the buffer and returns 0 if the path was successfully
169 copied in the provided buffer. If the buffer is not large
170 enough, -1 is returned and the expected buffer size is
171 copied in *bufsize. Note that _NSGetExecutablePath will
172 return "a path" to the executable not a "real path" to the
173 executable. That is the path may be a symbolic link and
174 not the real file. And with deep directories the total
175 bufsize needed could be more than MAXPATHLEN.
179 char *given_path = (char*)malloc(MAXPATHLEN * 2);
180 if (!given_path) return status;
182 uint32_t npathsize = MAXPATHLEN * 2;
183 long result = _NSGetExecutablePath(given_path, &npathsize);
185 { /* OK, we got something - now try and resolve the real path...
187 if (realpath(given_path, pname) != NULL)
189 if ((access(pname, 0) == 0))
190 status = 0; /* file exists, return OK */
197 return -1; /* Path Lookup Failed */
200 std::string System::GetDllAppPath(std::string &nomdll){
201 std::string path = ".";
203 char currentPath[_MAX_PATH];
204 HMODULE hand = GetModuleHandle(nomdll.c_str());
205 GetModuleFileName(hand, currentPath, _MAX_PATH);
209 path = path.substr(0,path.find_last_of("\\"));
215 std::string System::GetDllAppPath(const char *nomdll){
216 std::string path = ".";
218 char currentPath[_MAX_PATH];
219 HMODULE hand = GetModuleHandle(nomdll);
220 GetModuleFileName(hand, currentPath, _MAX_PATH);
224 path = path.substr(0,path.find_last_of("\\"));
231 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
233 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
236 //=========================================================================
237 std::string System::GetExecutablePath(){
239 //EED int err = get_app_path(name, PATH_MAX);
240 int err = System::GetAppPath(name,PATH_MAX);
243 printf("Could not determine current executable path ? ");
245 // remove the exe name
247 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
255 void System::createDirectory(const char* directorypath){
257 if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS)
259 std::cout<<"directory already exists "<<directorypath<<std::endl;
260 }else if(CreateDirectory(directorypath, NULL) == ERROR_PATH_NOT_FOUND){
261 std::string error = "Directory could not be created ";
262 error.append(directorypath);
267 //! include sys/types.h
268 //! include sys/stat.h
269 //! int mkdir(const char *path, mode_t mode);
270 //! read/write/search permissions for owner and group, and with read/search permissions for others S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
271 int returnval = mkdir(directorypath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
275 std::cout<<"directory already exists "<<directorypath<<std::endl;
277 std::string error = "Directory could not be created ";
278 error.append(directorypath);