]> Creatis software - crea.git/blob - src/creaSystem.cxx
create directory in windows
[crea.git] / src / creaSystem.cxx
1 #include "creaSystem.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <iostream>
5
6 #ifdef WIN32
7         #include <windows.h> /* GetModuleFileName */
8         #include <io.h>
9         
10 #endif /* WIN32 */
11
12 #ifdef LINUX
13         #include <sys/types.h>
14         #include <sys/stat.h>
15         #include <errno.h> 
16 #endif 
17
18 #ifdef __APPLE__  /* assume this is OSX */
19 #include <sys/param.h>
20 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
21 CoreFoundation to link line */
22 #include <string.h>
23 # ifndef PATH_MAX
24 #  define PATH_MAX MAXPATHLEN
25 # endif
26 #endif /* APPLE */
27
28 #ifndef PATH_MAX
29 #  define PATH_MAX 2048
30 #endif
31
32 #if defined(WIN32)
33   #include <direct.h>
34 #else
35    #include <dirent.h>  
36 #endif
37
38 #include <stdlib.h>
39
40
41 namespace crea
42 {
43
44 #ifdef _WIN32
45   int System::HasTTY() { return false; }
46 #else  
47 #include <unistd.h>
48    int System::HasTTY() 
49    { 
50      return isatty(fileno(stdin));
51    }
52 #endif
53
54 int System::GetAppPath(char *pname, size_t pathsize)
55    {
56 #ifdef LINUX    
57     /* Oddly, the readlink(2) man page says no NULL is appended. */
58     /* So you have to do it yourself, based on the return value: */
59     pathsize --; /* Preserve a space to add the trailing NULL */
60     long result = readlink("/proc/self/exe", pname, pathsize);
61     if (result > 0)
62         {
63                 pname[result] = 0; /* add the #@!%ing NULL */
64                 
65                 if ((access(pname, 0) == 0))
66                         return 0; /* file exists, return OK */
67                 /*else name doesn't seem to exist, return FAIL (falls
68                  through) */
69         }
70 #endif /* LINUX */
71     
72 #ifdef WIN32
73     long result = GetModuleFileName(NULL, pname, pathsize);
74     if (result > 0)
75         {
76                 /* fix up the dir slashes... */
77                 int len = strlen(pname);
78                 int idx;
79                 for (idx = 0; idx < len; idx++)
80                 {
81                         if (pname[idx] == '\\') pname[idx] = '/';
82                 }
83                 
84                 for (idx = len-1; idx >=0 ; idx--)
85                 {
86                         if (pname[idx] == '/')
87                         { 
88                                 pname[idx+1] = '\0';
89                                 idx = -1;
90                         }
91                 }
92                 
93                 if ((access(pname, 0) == 0))
94                         return 0; /* file exists, return OK */
95                 /*else name doesn't seem to exist, return FAIL (falls
96                  through) */
97         }
98 #endif /* WIN32 */
99     
100 #ifdef SOLARIS
101     char *p = getexecname();
102     if (p)
103         {
104                 /* According to the Sun manpages, getexecname will
105                  "normally" return an */
106                 /* absolute path - BUT might not... AND that IF it is not,
107                  pre-pending */
108                 /* getcwd() will "usually" be the correct thing... Urgh!
109                  */
110                 
111                 /* check pathname is absolute (begins with a / ???) */
112                 if (p[0] == '/') /* assume this means we have an
113                  absolute path */
114                 {
115                         strncpy(pname, p, pathsize);
116                         if ((access(pname, 0) == 0))
117                                 return 0; /* file exists, return OK */
118                 }
119                 else /* if not, prepend getcwd() then check if file
120                  exists */
121                 {
122                         getcwd(pname, pathsize);
123                         long result = strlen(pname);
124                         strncat(pname, "/", (pathsize - result));
125                         result ++;
126                         strncat(pname, p, (pathsize - result));
127                         
128                         if ((access(pname, 0) == 0))
129                                 return 0; /* file exists, return OK */
130                         /*else name doesn't seem to exist, return FAIL
131                          (falls through) */
132                 }
133         }
134 #endif /* SOLARIS */
135     
136 #ifdef MACOSX /* assume this is OSX */
137     /*
138          from http://www.hmug.org/man/3/NSModule.html
139          
140          extern int _NSGetExecutablePath(char *buf, unsigned long
141          *bufsize);
142          
143          _NSGetExecutablePath  copies  the  path  of the executable
144          into the buffer and returns 0 if the path was successfully
145          copied  in the provided buffer. If the buffer is not large
146          enough, -1 is returned and the  expected  buffer  size  is
147          copied  in  *bufsize.  Note that _NSGetExecutablePath will
148          return "a path" to the executable not a "real path" to the
149          executable.  That  is  the path may be a symbolic link and
150          not the real file. And with  deep  directories  the  total
151          bufsize needed could be more than MAXPATHLEN.
152          */
153         
154     int status = -1;
155     char *given_path = (char*)malloc(MAXPATHLEN * 2);
156     if (!given_path) return status;
157     
158     uint32_t npathsize = MAXPATHLEN * 2;
159     long result = _NSGetExecutablePath(given_path, &npathsize);
160     if (result == 0)
161         { /* OK, we got something - now try and resolve the real path...
162          */
163                 if (realpath(given_path, pname) != NULL)
164                 {
165                         if ((access(pname, 0) == 0))
166                                 status = 0; /* file exists, return OK */
167                 }
168         }
169     free (given_path);
170     return status;
171 #endif /* MACOSX */
172     
173     return -1; /* Path Lookup Failed */
174 }
175
176 std::string System::GetDllAppPath(std::string &nomdll){
177         std::string path = ".";
178 #ifdef WIN32
179         char currentPath[_MAX_PATH];
180         HMODULE hand = GetModuleHandle(nomdll.c_str());
181         GetModuleFileName(hand, currentPath, _MAX_PATH);
182
183         path = currentPath;
184
185         path = path.substr(0,path.find_last_of("\\"));
186 #endif
187         return path;
188 }
189
190
191 std::string System::GetDllAppPath(const char *nomdll){
192         std::string path = ".";
193 #ifdef WIN32
194         char currentPath[_MAX_PATH];
195         HMODULE hand = GetModuleHandle(nomdll);
196         GetModuleFileName(hand, currentPath, _MAX_PATH);
197
198         path = currentPath;
199
200         path = path.substr(0,path.find_last_of("\\"));
201 #endif
202         return path;
203 }
204
205
206 #if defined(_WIN32)
207 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
208 #else
209 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
210 #endif  
211         
212         //=========================================================================
213 std::string System::GetExecutablePath(){
214                 char name[PATH_MAX];
215                 //EED    int err = get_app_path(name, PATH_MAX);
216                 int err = System::GetAppPath(name,PATH_MAX);
217                 if (err) 
218                 {
219                         printf("Could not determine current executable path ?  ");  
220                 }    
221                 // remove the exe name
222                 char *slash;
223                 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
224                 if (slash)
225                 {
226                         *slash = 0;
227                 }
228                 return name;
229         }
230         
231         void System::createDirectory(const char* directorypath){
232                 #ifdef WIN32
233                         if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS) 
234                         { 
235                                 std::cout<<"directory already exists "<<directorypath<<std::endl;
236                         }else if(CreateDirectory(directorypath, NULL) == ERROR_PATH_NOT_FOUND){
237                                 std::string error = "Directory could not be created ";
238                                 error.append(directorypath);
239                                 throw error.c_str();
240                         }
241                 #endif
242                 #ifdef LINUX
243                         //! include sys/types.h
244                         //! include sys/stat.h
245                         //! int mkdir(const char *path, mode_t mode);
246                         //! read/write/search permissions for owner and group, and with read/search permissions for others S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH
247                         int returnval = mkdir(directorypath, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
248                         
249                         if(returnval != 0){
250                                 if(errno == EEXIST){
251                                         std::cout<<"directory already exists "<<directorypath<<std::endl;
252                                 }else{
253                                         std::string error = "Directory could not be created ";
254                                         error.append(directorypath);
255                                         throw error.c_str();
256                                 }
257                         }
258                 #endif
259                 #ifdef MACOSX
260                 //TODO
261                 #endif
262         }
263
264 } // namespace crea