1 #include "creaSystem.h"
7 #include <windows.h> /* GetModuleFileName */
11 #ifdef __APPLE__ /* assume this is OSX */
12 #include <sys/param.h>
13 #include <mach-o/dyld.h> /* _NSGetExecutablePath : must add -framework
14 CoreFoundation to link line */
17 # define PATH_MAX MAXPATHLEN
22 # define PATH_MAX 2048
38 int System::HasTTY() { return false; }
43 return isatty(fileno(stdin));
46 std::string System::GetDllAppPath(std::string nom){
47 std::string path = ".";
52 int System::GetAppPath(char *pname, size_t pathsize)
55 /* Oddly, the readlink(2) man page says no NULL is appended. */
56 /* So you have to do it yourself, based on the return value: */
57 pathsize --; /* Preserve a space to add the trailing NULL */
58 long result = readlink("/proc/self/exe", pname, pathsize);
61 pname[result] = 0; /* add the #@!%ing NULL */
63 if ((access(pname, 0) == 0))
64 return 0; /* file exists, return OK */
65 /*else name doesn't seem to exist, return FAIL (falls
71 long result = GetModuleFileName(NULL, pname, pathsize);
74 /* fix up the dir slashes... */
75 int len = strlen(pname);
77 for (idx = 0; idx < len; idx++)
79 if (pname[idx] == '\\') pname[idx] = '/';
82 for (idx = len-1; idx >=0 ; idx--)
84 if (pname[idx] == '/')
91 if ((access(pname, 0) == 0))
92 return 0; /* file exists, return OK */
93 /*else name doesn't seem to exist, return FAIL (falls
99 char *p = getexecname();
102 /* According to the Sun manpages, getexecname will
103 "normally" return an */
104 /* absolute path - BUT might not... AND that IF it is not,
106 /* getcwd() will "usually" be the correct thing... Urgh!
109 /* check pathname is absolute (begins with a / ???) */
110 if (p[0] == '/') /* assume this means we have an
113 strncpy(pname, p, pathsize);
114 if ((access(pname, 0) == 0))
115 return 0; /* file exists, return OK */
117 else /* if not, prepend getcwd() then check if file
120 getcwd(pname, pathsize);
121 long result = strlen(pname);
122 strncat(pname, "/", (pathsize - result));
124 strncat(pname, p, (pathsize - result));
126 if ((access(pname, 0) == 0))
127 return 0; /* file exists, return OK */
128 /*else name doesn't seem to exist, return FAIL
134 #ifdef MACOSX /* assume this is OSX */
136 from http://www.hmug.org/man/3/NSModule.html
138 extern int _NSGetExecutablePath(char *buf, unsigned long
141 _NSGetExecutablePath copies the path of the executable
142 into the buffer and returns 0 if the path was successfully
143 copied in the provided buffer. If the buffer is not large
144 enough, -1 is returned and the expected buffer size is
145 copied in *bufsize. Note that _NSGetExecutablePath will
146 return "a path" to the executable not a "real path" to the
147 executable. That is the path may be a symbolic link and
148 not the real file. And with deep directories the total
149 bufsize needed could be more than MAXPATHLEN.
153 char *given_path = (char*)malloc(MAXPATHLEN * 2);
154 if (!given_path) return status;
156 uint32_t npathsize = MAXPATHLEN * 2;
157 long result = _NSGetExecutablePath(given_path, &npathsize);
159 { /* OK, we got something - now try and resolve the real path...
161 if (realpath(given_path, pname) != NULL)
163 if ((access(pname, 0) == 0))
164 status = 0; /* file exists, return OK */
171 return -1; /* Path Lookup Failed */
174 std::string System::GetDllAppPath(std::string nomdll){
175 std::string path = ".";
177 char currentPath[_MAX_PATH];
178 HMODULE hand = GetModuleHandle(nomdll.c_str());
179 GetModuleFileName(hand, currentPath, _MAX_PATH);
183 path = path.substr(0,path.find_last_of("\\"));