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
32 #include "CoreFoundation/CoreFoundation.h"
39 Signal handler (grabs Ctrl-C on POSIX systems)
42 bool g_killed = false;
44 bool * signal_handler_killstatus(void)
49 #if !defined(_WIN32) // POSIX
52 void sigint_handler(int sig)
56 dstream<<DTIME<<"INFO: sigint_handler(): "
57 <<"Ctrl-C pressed, shutting down."<<std::endl;
59 dstream<<DTIME<<"INFO: sigint_handler(): "
60 <<"Printing debug stacks"<<std::endl;
67 (void)signal(SIGINT, SIG_DFL);
71 void signal_handler_init(void)
73 dstream<<"signal_handler_init()"<<std::endl;
74 (void)signal(SIGINT, sigint_handler);
79 void signal_handler_init(void)
90 std::string path_data = "../data";
91 std::string path_userdata = "../";
93 void pathRemoveFile(char *path, char delim)
95 // Remove filename and path delimiter
97 for(i = strlen(path)-1; i>=0; i--)
105 void initializePaths()
109 Use relative paths if RUN_IN_PLACE
112 dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
120 const DWORD buflen = 1000;
124 // Find path of executable and set path_data relative to it
125 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
126 assert(len < buflen);
127 pathRemoveFile(buf, '\\');
129 // Use "./bin/../data"
130 path_data = std::string(buf) + "/../data";
133 path_userdata = std::string(buf) + "/../";
142 memset(buf, 0, BUFSIZ);
143 // Get path to executable
144 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
146 pathRemoveFile(buf, '/');
148 // Use "./bin/../data"
149 path_data = std::string(buf) + "/../data";
152 path_userdata = std::string(buf) + "/../";
157 #elif defined(__APPLE__) || defined(__FreeBSD__)
159 //TODO: Get path of executable. This assumes working directory is bin/
160 dstream<<"WARNING: Relative path not properly supported on OS X and FreeBSD"
162 path_data = std::string("../data");
163 path_userdata = std::string("../");
167 #else // RUN_IN_PLACE
170 Use platform-specific paths otherwise
173 dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
181 const DWORD buflen = 1000;
185 // Find path of executable and set path_data relative to it
186 len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
187 assert(len < buflen);
188 pathRemoveFile(buf, '\\');
190 // Use "./bin/../data"
191 path_data = std::string(buf) + "/../data";
192 //path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
194 // Use "C:\Documents and Settings\user\Application Data\<PROJECT_NAME>"
195 len = GetEnvironmentVariable("APPDATA", buf, buflen);
196 assert(len < buflen);
197 path_userdata = std::string(buf) + "/" + PROJECT_NAME;
206 memset(buf, 0, BUFSIZ);
207 // Get path to executable
208 assert(readlink("/proc/self/exe", buf, BUFSIZ-1) != -1);
210 pathRemoveFile(buf, '/');
212 path_data = std::string(buf) + "/../share/" + PROJECT_NAME;
213 //path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
214 if (!fs::PathExists(path_data)) {
215 dstream<<"WARNING: data path " << path_data << " not found!";
216 path_data = std::string(buf) + "/../data";
217 dstream<<" Trying " << path_data << std::endl;
220 path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
225 #elif defined(__APPLE__)
229 // http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c
230 CFBundleRef main_bundle = CFBundleGetMainBundle();
231 CFURLRef resources_url = CFBundleCopyResourcesDirectoryURL(main_bundle);
233 if(CFURLGetFileSystemRepresentation(resources_url, TRUE, (UInt8 *)path, PATH_MAX))
235 dstream<<"Bundle resource path: "<<path<<std::endl;
237 path_data = std::string(path) + "/data";
242 dstream<<"WARNING: Could not determine bundle resource path"<<std::endl;
244 CFRelease(resources_url);
246 path_userdata = std::string(getenv("HOME")) + "/Library/Application Support/" + PROJECT_NAME;
248 #elif defined(__FreeBSD__)
250 path_data = std::string(INSTALL_PREFIX) + "/share/" + PROJECT_NAME;
251 path_userdata = std::string(getenv("HOME")) + "/." + PROJECT_NAME;
255 #endif // RUN_IN_PLACE
257 dstream<<"path_data = "<<path_data<<std::endl;
258 dstream<<"path_userdata = "<<path_userdata<<std::endl;
261 } //namespace porting