1 #include "creaSystem.h"
7 #include <windows.h> /* GetModuleFileName */
13 #include <sys/types.h>
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 */
24 # define PATH_MAX MAXPATHLEN
29 # define PATH_MAX 2048
45 int System::HasTTY() { return false; }
50 return isatty(fileno(stdin));
54 int System::GetAppPath(char *pname, size_t pathsize)
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);
63 pname[result] = 0; /* add the #@!%ing NULL */
65 if ((access(pname, 0) == 0))
66 return 0; /* file exists, return OK */
67 /*else name doesn't seem to exist, return FAIL (falls
73 long result = GetModuleFileName(NULL, pname, pathsize);
76 /* fix up the dir slashes... */
77 int len = strlen(pname);
79 for (idx = 0; idx < len; idx++)
81 if (pname[idx] == '\\') pname[idx] = '/';
84 for (idx = len-1; idx >=0 ; idx--)
86 if (pname[idx] == '/')
93 if ((access(pname, 0) == 0))
94 return 0; /* file exists, return OK */
95 /*else name doesn't seem to exist, return FAIL (falls
101 char *p = getexecname();
104 /* According to the Sun manpages, getexecname will
105 "normally" return an */
106 /* absolute path - BUT might not... AND that IF it is not,
108 /* getcwd() will "usually" be the correct thing... Urgh!
111 /* check pathname is absolute (begins with a / ???) */
112 if (p[0] == '/') /* assume this means we have an
115 strncpy(pname, p, pathsize);
116 if ((access(pname, 0) == 0))
117 return 0; /* file exists, return OK */
119 else /* if not, prepend getcwd() then check if file
122 getcwd(pname, pathsize);
123 long result = strlen(pname);
124 strncat(pname, "/", (pathsize - result));
126 strncat(pname, p, (pathsize - result));
128 if ((access(pname, 0) == 0))
129 return 0; /* file exists, return OK */
130 /*else name doesn't seem to exist, return FAIL
136 #ifdef MACOSX /* assume this is OSX */
138 from http://www.hmug.org/man/3/NSModule.html
140 extern int _NSGetExecutablePath(char *buf, unsigned long
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.
155 char *given_path = (char*)malloc(MAXPATHLEN * 2);
156 if (!given_path) return status;
158 uint32_t npathsize = MAXPATHLEN * 2;
159 long result = _NSGetExecutablePath(given_path, &npathsize);
161 { /* OK, we got something - now try and resolve the real path...
163 if (realpath(given_path, pname) != NULL)
165 if ((access(pname, 0) == 0))
166 status = 0; /* file exists, return OK */
173 return -1; /* Path Lookup Failed */
176 std::string System::GetDllAppPath(std::string &nomdll){
177 std::string path = ".";
179 char currentPath[_MAX_PATH];
180 HMODULE hand = GetModuleHandle(nomdll.c_str());
181 GetModuleFileName(hand, currentPath, _MAX_PATH);
185 path = path.substr(0,path.find_last_of("\\"));
191 std::string System::GetDllAppPath(const char *nomdll){
192 std::string path = ".";
194 char currentPath[_MAX_PATH];
195 HMODULE hand = GetModuleHandle(nomdll);
196 GetModuleFileName(hand, currentPath, _MAX_PATH);
200 path = path.substr(0,path.find_last_of("\\"));
207 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '\\'
209 #define CREACONTOUR_VALID_FILE_SEPARATOR_CHAR '/'
212 //=========================================================================
213 std::string System::GetExecutablePath(){
215 //EED int err = get_app_path(name, PATH_MAX);
216 int err = System::GetAppPath(name,PATH_MAX);
219 printf("Could not determine current executable path ? ");
221 // remove the exe name
223 slash = strrchr(name, CREACONTOUR_VALID_FILE_SEPARATOR_CHAR);
231 void System::createDirectory(const char* directorypath){
233 if (CreateDirectory(directorypath, NULL) == ERROR_ALREADY_EXISTS)
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);
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);
251 std::cout<<"directory already exists "<<directorypath<<std::endl;
253 std::string error = "Directory could not be created ";
254 error.append(directorypath);