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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "util/string.h"
35 #include "CoreFoundation/CoreFoundation.h"
42 Signal handler (grabs Ctrl-C on POSIX systems)
45 bool g_killed = false;
47 bool * signal_handler_killstatus(void)
52 #if !defined(_WIN32) // POSIX
55 void sigint_handler(int sig)
59 dstream<<DTIME<<"INFO: sigint_handler(): "
60 <<"Ctrl-C pressed, shutting down."<<std::endl;
62 // Comment out for less clutter when testing scripts
63 /*dstream<<DTIME<<"INFO: sigint_handler(): "
64 <<"Printing debug stacks"<<std::endl;
65 debug_stacks_print();*/
71 (void)signal(SIGINT, SIG_DFL);
75 void signal_handler_init(void)
77 (void)signal(SIGINT, sigint_handler);
84 BOOL WINAPI event_handler(DWORD sig)
89 case CTRL_CLOSE_EVENT:
90 case CTRL_LOGOFF_EVENT:
91 case CTRL_SHUTDOWN_EVENT:
95 dstream<<DTIME<<"INFO: event_handler(): "
96 <<"Ctrl+C, Close Event, Logoff Event or Shutdown Event, shutting down."<<std::endl;
97 // Comment out for less clutter when testing scripts
98 /*dstream<<DTIME<<"INFO: event_handler(): "
99 <<"Printing debug stacks"<<std::endl;
100 debug_stacks_print();*/
106 (void)signal(SIGINT, SIG_DFL);
110 case CTRL_BREAK_EVENT:
117 void signal_handler_init(void)
119 SetConsoleCtrlHandler( (PHANDLER_ROUTINE)event_handler,TRUE);
128 // Default to RUN_IN_PLACE style relative paths
129 std::string path_share = "..";
130 std::string path_user = "..";
132 std::string getDataPath(const char *subpath)
134 return path_share + DIR_DELIM + subpath;
137 void pathRemoveFile(char *path, char delim)
139 // Remove filename and path delimiter
141 for(i = strlen(path)-1; i>=0; i--)
149 bool detectMSVCBuildDir(char *c_path)
151 std::string path(c_path);
152 const char *ends[] = {"bin\\Release", "bin\\Build", NULL};
153 return (removeStringEnd(path, ends) != "");
156 void initializePaths()
160 Use relative paths if RUN_IN_PLACE
163 infostream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
171 const DWORD buflen = 1000;
175 // Find path of executable and set path_share relative to it
176 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
177 assert(len < buflen);
178 pathRemoveFile(buf, '\\');
180 if(detectMSVCBuildDir(buf)){
181 infostream<<"MSVC build directory detected"<<std::endl;
182 path_share = std::string(buf) + "\\..\\..";
183 path_user = std::string(buf) + "\\..\\..";
186 path_share = std::string(buf) + "\\..";
187 path_user = std::string(buf) + "\\..";
197 memset(buf, 0, BUFSIZ);
198 // Get path to executable
199 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
201 pathRemoveFile(buf, '/');
203 path_share = std::string(buf) + "/..";
204 path_user = std::string(buf) + "/..";
209 #elif defined(__APPLE__) || defined(__FreeBSD__)
211 //TODO: Get path of executable. This assumes working directory is bin/
212 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
214 path_share = std::string("..");
215 path_user = std::string("..");
219 #else // RUN_IN_PLACE
222 Use platform-specific paths otherwise
225 infostream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
233 const DWORD buflen = 1000;
237 // Find path of executable and set path_share relative to it
238 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
239 assert(len < buflen);
240 pathRemoveFile(buf, '\\');
243 path_share = std::string(buf) + "\\..";
245 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
246 len = GetEnvironmentVariable("APPDATA", buf, buflen);
247 assert(len < buflen);
248 path_user = std::string(buf) + DIR_DELIM + PROJECT_NAME;
256 // Get path to executable
257 std::string bindir = "";
260 memset(buf, 0, BUFSIZ);
261 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
262 pathRemoveFile(buf, '/');
266 // Find share directory from these.
267 // It is identified by containing the subdirectory "builtin".
268 std::list<std::string> trylist;
269 std::string static_sharedir = STATIC_SHAREDIR;
270 if(static_sharedir != "" && static_sharedir != ".")
271 trylist.push_back(static_sharedir);
272 trylist.push_back(bindir + "/../share/" + PROJECT_NAME);
273 trylist.push_back(bindir + "/..");
275 for(std::list<std::string>::const_iterator i = trylist.begin();
276 i != trylist.end(); i++)
278 const std::string &trypath = *i;
279 if(!fs::PathExists(trypath) || !fs::PathExists(trypath + "/builtin")){
280 dstream<<"WARNING: system-wide share not found at \""
281 <<trypath<<"\""<<std::endl;
284 // Warn if was not the first alternative
285 if(i != trylist.begin()){
286 dstream<<"WARNING: system-wide share found at \""
287 <<trypath<<"\""<<std::endl;
289 path_share = trypath;
293 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
298 #elif defined(__APPLE__)
302 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
303 CFBundleRef main_bundle = CFBundleGetMainBundle();
304 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
306 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
308 dstream<<"Bundle resource path: "<<path<<std::endl;
310 path_share = std::string(path) + "/share";
315 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
317 CFRelease(resources_url);
319 path_user = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
321 #elif defined(__FreeBSD__)
323 path_share = STATIC_SHAREDIR;
324 path_user = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
328 #endif // RUN_IN_PLACE
331 } //namespace porting