]> git.lizzy.rs Git - minetest.git/blob - src/porting.cpp
Added a more flexible path system (and fixed some minor stuff)
[minetest.git] / src / porting.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 /*
21         Random portability stuff
22
23         See comments in porting.h
24 */
25
26 #include "porting.h"
27
28 namespace porting
29 {
30
31 std::string path_data = "../data";
32 std::string path_userdata = "../";
33
34 void pathRemoveFile(char *path, char delim)
35 {
36         // Remove filename and path delimiter
37         int i;
38         for(i = strlen(path)-1; i>=0; i--)
39         {
40                 if(path[i] == delim)
41                         break;
42         }
43         path[i] = 0;
44 }
45
46 void initializePaths()
47 {
48 #ifdef RUN_IN_PLACE
49         /*
50                 Use relative paths if RUN_IN_PLACE
51         */
52
53         dstream<<"Using relative paths (RUN_IN_PLACE)"<<std::endl;
54
55         /*
56                 Windows
57         */
58         #if defined(_WIN32)
59                 #include <windows.h>
60
61         const DWORD buflen = 1000;
62         char buf[buflen];
63         DWORD len;
64         
65         // Find path of executable and set path_data relative to it
66         len = GetModuleFileName(GetModuleHandle(NULL), buf, buflen);
67         assert(len < buflen);
68         pathRemoveFile(buf, '\\');
69
70         // Use "./bin/../data"
71         path_data = std::string(buf) + "/../data";
72                 
73         // Use "./bin/../"
74         path_userdata = std::string(buf) + "/../";
75
76         /*
77                 Linux
78         */
79         #elif defined(linux)
80                 #include <unistd.h>
81         
82         char buf[BUFSIZ];
83         // Get path to executable
84         readlink("/proc/self/exe", buf, BUFSIZ);
85         
86         pathRemoveFile(buf, '/');
87
88         // Use "./bin/../data"
89         path_data = std::string(buf) + "/../data";
90                 
91         // Use "./bin/../"
92         path_userdata = std::string(buf) + "/../";
93         
94         /*
95                 OS X
96         */
97         #elif defined(__APPLE__)
98         
99         //TODO: Get path of executable. This assumes working directory is bin/
100         dstream<<"WARNING: Relative path not properly supported on OS X"
101                         <<std::endl;
102         path_data = std::string("../data");
103         path_userdata = std::string("../");
104
105         #endif
106 #else
107         /*
108                 Use platform-specific paths otherwise
109         */
110
111         dstream<<"Using system-wide paths (NOT RUN_IN_PLACE)"<<std::endl;
112
113         /*
114                 Windows
115         */
116         #if defined(_WIN32)
117                 #include <windows.h>
118
119         const DWORD buflen = 1000;
120         char buf[buflen];
121         DWORD len;
122         
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, '\\');
127         
128         // Use "./bin/../data"
129         path_data = std::string(buf) + "/../data";
130                 
131         // Use "C:\Documents and Settings\user\Application Data\<APPNAME>"
132         len = GetEnvironmentVariable("APPDATA", buf, buflen);
133         assert(len < buflen);
134         path_userdata = std::string(buf) + "/" + APPNAME;
135
136         /*
137                 Linux
138         */
139         #elif defined(linux)
140         
141         path_userdata = std::string("~/.") + APPNAME;
142         path_data = std::string("/usr/share/") + APPNAME;
143         
144         /*
145                 OS X
146         */
147         #elif defined(__APPLE__)
148
149         path_userdata = std::string("~/Library/Application Support/") + APPNAME;
150         path_data = std::string("minetest-mac.app/Contents/Resources/data/");
151
152         #endif
153 #endif
154
155         dstream<<"path_data = "<<path_data<<std::endl;
156         dstream<<"path_userdata = "<<path_userdata<<std::endl;
157 }
158
159 } //namespace porting
160