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