3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Random portability stuff
23 See comments in porting.h
31 #include "utility_string.h"
34 #include "CoreFoundation/CoreFoundation.h"
41 Signal handler (grabs Ctrl-C on POSIX systems)
44 bool g_killed = false;
46 bool * signal_handler_killstatus(void)
51 #if !defined(_WIN32) // POSIX
54 void sigint_handler(int sig)
58 dstream<<DTIME<<"INFO: sigint_handler(): "
59 <<"Ctrl-C pressed, shutting down."<<std::endl;
61 // Comment out for less clutter when testing scripts
62 /*dstream<<DTIME<<"INFO: sigint_handler(): "
63 <<"Printing debug stacks"<<std::endl;
64 debug_stacks_print();*/
70 (void)signal(SIGINT, SIG_DFL);
74 void signal_handler_init(void)
76 (void)signal(SIGINT, sigint_handler);
83 BOOL WINAPI event_handler(DWORD sig)
88 case CTRL_CLOSE_EVENT:
89 case CTRL_LOGOFF_EVENT:
90 case CTRL_SHUTDOWN_EVENT:
94 dstream<<DTIME<<"INFO: event_handler(): "
95 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
96 // Comment out for less clutter when testing scripts
97 /*dstream<<DTIME<<"INFO: event_handler(): "
98 <<"Printing debug stacks"<<std::endl;
99 debug_stacks_print();*/
105 (void)signal(SIGINT, SIG_DFL);
109 case CTRL_BREAK_EVENT:
116 void signal_handler_init(void)
118 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
127 // Default to RUN_IN_PLACE style relative paths
128 std::string path_share = "..";
129 std::string path_user = "..";
131 std::string getDataPath(const char *subpath)
133 return path_share + DIR_DELIM + subpath;
136 void pathRemoveFile(char *path, char delim)
138 // Remove filename and path delimiter
140 for(i = strlen(path)-1; i>=0; i--)
148 bool detectMSVCBuildDir(char *c_path)
150 std::string path(c_path);
151 const char *ends[] = {"bin\\Release", "bin\\Build", NULL};
152 return (removeStringEnd(path, ends) != "");
155 void initializePaths()
159 Use relative paths if RUN_IN_PLACE
162 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
170 const DWORD buflen = 1000;
174 // Find path of executable and set path_share relative to it
175 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
176 assert(len < buflen);
177 pathRemoveFile(buf, '\\');
179 if(detectMSVCBuildDir(buf)){
180 infostream<<"MSVC build directory detected"<<std::endl;
181 path_share = std::string(buf) + "\\..\\..";
182 path_user = std::string(buf) + "\\..\\..";
185 path_share = std::string(buf) + "\\..";
186 path_user = std::string(buf) + "\\..";
196 memset(buf, 0, BUFSIZ);
197 // Get path to executable
198 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
200 pathRemoveFile(buf, '/');
202 path_share = std::string(buf) + "/..";
203 path_user = std::string(buf) + "/..";
208 #elif defined(__APPLE__) || defined(__FreeBSD__)
210 //TODO: Get path of executable. This assumes working directory is bin/
211 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
213 path_share = std::string("..");
214 path_user = std::string("..");
218 #else // RUN_IN_PLACE
221 Use platform-specific paths otherwise
224 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
232 const DWORD buflen = 1000;
236 // Find path of executable and set path_share relative to it
237 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
238 assert(len < buflen);
239 pathRemoveFile(buf, '\\');
242 path_share = std::string(buf) + "\\..";
244 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
245 len = GetEnvironmentVariable("APPDATA", buf, buflen);
246 assert(len < buflen);
247 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
256 memset(buf, 0, BUFSIZ);
257 // Get path to executable
258 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
260 pathRemoveFile(buf, '/');
262 path_share = std::string(buf) + "/../share/" + PROJECT_NAME;
263 //path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
264 if (!fs::PathExists(path_share)) {
265 dstream<<"WARNING: system-wide share not found at \""<<path_share<<"\"";
266 path_share = std::string(buf) + "/..";
267 dstream<<"WARNING: Using \""<<path_share<<"\" instead."<<std::endl;
270 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
275 #elif defined(__APPLE__)
279 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
280 CFBundleRef main_bundle = CFBundleGetMainBundle();
281 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
283 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
285 dstream<<"Bundle resource path: "<<path<<std::endl;
287 path_share = std::string(path) + "/share";
292 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
294 CFRelease(resources_url);
296 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
298 #elif defined(__FreeBSD__)
300 path_share = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
301 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
305 #endif // RUN_IN_PLACE
308 } //namespace porting