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 "CoreFoundation/CoreFoundation.h"
38 Signal handler (grabs Ctrl-C on POSIX systems)
41 bool g_killed = false;
43 bool * signal_handler_killstatus(void)
48 #if !defined(_WIN32) // POSIX
51 void sigint_handler(int sig)
55 dstream<<DTIME<<"INFO: sigint_handler(): "
56 <<"Ctrl-C pressed, shutting down."<<std::endl;
58 dstream<<DTIME<<"INFO: sigint_handler(): "
59 <<"Printing debug stacks"<<std::endl;
66 (void)signal(SIGINT, SIG_DFL);
70 void signal_handler_init(void)
72 dstream<<"signal_handler_init()"<<std::endl;
73 (void)signal(SIGINT, sigint_handler);
78 void signal_handler_init(void)
89 std::string path_data = "../data";
90 std::string path_userdata = "../";
92 void pathRemoveFile(char *path, char delim)
94 // Remove filename and path delimiter
96 for(i = strlen(path)-1; i>=0; i--)
104 void initializePaths()
108 Use relative paths if RUN_IN_PLACE
111 dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
119 const DWORD buflen = 1000;
123 // Find path of executable and set path_data relative to it
124 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
125 assert(len < buflen);
126 pathRemoveFile(buf, '\\');
128 // Use "./bin/../data"
129 path_data = std::string(buf) + "/../data";
132 path_userdata = std::string(buf) + "/../";
141 memset(buf, 0, BUFSIZ);
142 // Get path to executable
143 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
145 pathRemoveFile(buf, '/');
147 // Use "./bin/../data"
148 path_data = std::string(buf) + "/../data";
151 path_userdata = std::string(buf) + "/../";
156 #elif defined(__APPLE__)
158 //TODO: Get path of executable. This assumes working directory is bin/
159 dstream<<"WARNING: Relative path not properly supported on OS X"
161 path_data = std::string("../data");
162 path_userdata = std::string("../");
166 #else // RUN_IN_PLACE
169 Use platform-specific paths otherwise
172 dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
180 const DWORD buflen = 1000;
184 // Find path of executable and set path_data relative to it
185 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
186 assert(len < buflen);
187 pathRemoveFile(buf, '\\');
189 // Use "./bin/../data"
190 path_data = std::string(buf) + "/../data";
191 //path_data = std::string(buf) + "/../share/" + APPNAME;
193 // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
194 len = GetEnvironmentVariable("APPDATA", buf, buflen);
195 assert(len < buflen);
196 path_userdata = std::string(buf) + "/" + APPNAME;
205 memset(buf, 0, BUFSIZ);
206 // Get path to executable
207 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
209 pathRemoveFile(buf, '/');
211 path_data = std::string(buf) + "/../share/" + APPNAME;
212 //path_data = std::string(INSTALL_PREFIX) + "/share/" + APPNAME;
214 path_userdata = std::string(getenv("HOME")) + "/." + APPNAME;
219 #elif defined(__APPLE__)
223 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
224 CFBundleRef main_bundle = CFBundleGetMainBundle();
225 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
227 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
229 dstream<<"Bundle resource path: "<<path<<std::endl;
231 path_data = std::string(path) + "/data";
236 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
238 CFRelease(resources_url);
240 path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + APPNAME;
244 #endif // RUN_IN_PLACE
246 dstream<<"path_data = "<<path_data<<std::endl;
247 dstream<<"path_userdata = "<<path_userdata<<std::endl;
250 } //namespace porting