]> git.jsancho.org Git - lugaru.git/blob - Source/MacCompatibility.cpp
Add copyright notice and AUTHORS file for open source contributors
[lugaru.git] / Source / MacCompatibility.cpp
1 /*
2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4
5 This file is part of Lugaru.
6
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /**> HEADER FILES <**/
22 #include "MacCompatibility.h"
23
24 #ifdef WIN32
25 #include <windows.h>
26 #endif
27
28 #include <errno.h>
29 #include <time.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #if PLATFORM_UNIX
35 #include <unistd.h>
36 #include <sys/time.h>
37 #include <sys/stat.h>
38 #include <assert.h>
39 typedef long long __int64;
40 typedef __int64 LARGE_INTEGER;
41 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
42 {
43     assert(sizeof (__int64) == 8);
44     assert(sizeof (LARGE_INTEGER) == 8);
45     *liptr = 1000;
46     return(1);
47 }
48
49 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
50 {
51     struct timeval tv;
52     gettimeofday(&tv, NULL);
53     *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
54                (((LARGE_INTEGER) tv.tv_usec) / 1000) );
55     return(1);
56 }
57 #endif
58
59 class AppTime
60 {
61 public:
62     AppTime() {
63         counterRate = 1;
64         baseCounter = 0;
65         QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
66         QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
67     }
68     __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
69     __int64 baseCounter;
70 };
71 static AppTime g_appTime;
72
73 AbsoluteTime UpTime()
74 {
75     __int64 counter;
76     QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
77
78     counter -= g_appTime.baseCounter;
79
80     AbsoluteTime time;
81     time.lo = (unsigned long)counter;
82     time.hi = (unsigned long)(counter >> 32);
83     return time;
84 }
85
86
87 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
88 {
89     __int64 value = a.hi;
90     value <<= 32;
91     value |= a.lo;
92     __int64 value2 = b.hi;
93     value2 <<= 32;
94     value2 |= b.lo;
95     value -= value2;
96
97     if (value <= 0)
98         return durationImmediate;
99
100     __int64 frac = value % g_appTime.counterRate;
101     value /= g_appTime.counterRate;
102
103     Duration time;
104
105     if (value == 0) {
106         frac *= -1000000;
107         frac /= g_appTime.counterRate;
108         time = (Duration)frac;
109     } else {
110         frac *= 1000;
111         frac /= g_appTime.counterRate;
112         value *= 1000;
113         value += frac;
114         time = (Duration)value;
115     }
116
117     return time;
118 }
119
120
121 #if PLATFORM_UNIX
122 #include <sys/types.h>
123 #include <pwd.h>
124 #include <fcntl.h>
125 #include <unistd.h>
126 #include <dirent.h>
127
128 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
129 //  the zlib license on physfs allows this cut-and-pasting.
130 static int locateOneElement(char *buf)
131 {
132     char *ptr;
133     DIR *dirp;
134
135     //if (PHYSFS_exists(buf))
136     if (access(buf, F_OK) == 0)
137         return(1);  /* quick rejection: exists in current case. */
138
139     ptr = strrchr(buf, '/');  /* find entry at end of path. */
140     if (ptr == NULL) {
141         dirp = opendir(".");
142         ptr = buf;
143     } /* if */
144     else {
145         *ptr = '\0';
146         dirp = opendir(buf);
147         *ptr = '/';
148         ptr++;  /* point past dirsep to entry itself. */
149     } /* else */
150
151     struct dirent *dent;
152     while ((dent = readdir(dirp)) != NULL) {
153         if (strcasecmp(dent->d_name, ptr) == 0) {
154             strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
155             closedir(dirp);
156             return(1);
157         } /* if */
158     } /* for */
159
160     /* no match at all... */
161     closedir(dirp);
162     return(0);
163 } /* locateOneElement */
164
165
166 static inline const char *getUserDirByUID(void)
167 {
168     struct passwd *pw = getpwuid(getuid());
169     if (pw != NULL)
170         return(pw->pw_dir);
171     return(NULL);
172 } /* getUserDirByUID */
173
174
175 static inline const char *getPrefPath(void)
176 {
177     static char *prefpath = NULL;
178     if (prefpath == NULL) {
179         const char *homedir = getenv("HOME");
180         if (homedir == NULL)
181             homedir = getUserDirByUID();
182         if (homedir == NULL)
183             homedir = ".";  // oh well.
184
185 #if (defined(__APPLE__) && defined(__MACH__))
186         const char *PREFPATHNAME = "Library/Application Support/Lugaru";
187 #else
188         const char *PREFPATHNAME = ".lugaru";
189 #endif
190         size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
191         prefpath = new char[len];
192         snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
193     }
194     return(prefpath);
195 }
196
197 static int locateCorrectCase(char *buf, bool makedirs)
198 {
199     int rc;
200     char *ptr = buf;
201
202     while (ptr = strchr(ptr + 1, '/')) {
203         *ptr = '\0';  /* block this path section off */
204         rc = locateOneElement(buf);
205         if (!rc) {
206             if (makedirs)  /* normal if we're writing; build dirs! */
207                 mkdir(buf, S_IRWXU);
208             else {
209                 *ptr = '/'; /* restore path separator */
210                 return(-2);  /* missing element in path. */
211             } /* else */
212         } /* if */
213         *ptr = '/'; /* restore path separator */
214     } /* while */
215
216     /* check final element... */
217     return(locateOneElement(buf) ? 0 : -1);
218 }
219
220
221 static int locateCorrectFile(char *buf, const char *mode)
222 {
223     if (*buf == '\0')
224         return(0);  /* Uh...I guess that's failure. */
225
226     assert((mode[0] == 'w') || (mode[0] == 'r'));
227
228     bool iswriting = (mode[0] == 'w');
229     const char *prefpath = getPrefPath();
230     size_t len = strlen(buf) + strlen(prefpath) + 2;
231     char *prefpathfile = (char *) alloca(len);
232     snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
233
234     int rc = locateCorrectCase(prefpathfile, iswriting);  /* favor prefpath. */
235     if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
236         strcpy(buf, prefpathfile);
237     else if ((rc < 0) && (!iswriting))  /* not writing? Try game dir... */
238         rc = locateCorrectCase(buf, iswriting);
239
240     return(rc);
241 } /* locateCorrectFile */
242 #endif
243
244
245 static char g_filename[4096];
246 char* ConvertFileName( const char* orgfilename, const char *mode)
247 {
248     if (orgfilename == g_filename) // recursion?
249         return g_filename;
250
251     // translate filename into proper path name
252     if (orgfilename[ 0] == ':')
253         orgfilename++;
254     strcpy( g_filename, orgfilename);
255
256     for (int n = 0; g_filename[ n]; n++) {
257         if (g_filename[ n] == ':')
258             g_filename[ n] = '/';
259
260         else if (g_filename[ n] == '\\')
261             g_filename[ n] = '/';
262     }
263
264 #if PLATFORM_UNIX
265     locateCorrectFile(g_filename, mode);
266 #endif
267
268     return g_filename;
269 }