2 Copyright (C) 2003, 2010 - Wolfire Games
4 This file is part of Lugaru.
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.
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.
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/>.
20 /**> HEADER FILES <**/
21 #include "MacCompatibility.h"
38 typedef long long __int64;
39 typedef __int64 LARGE_INTEGER;
40 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
42 assert(sizeof (__int64) == 8);
43 assert(sizeof (LARGE_INTEGER) == 8);
48 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
51 gettimeofday(&tv, NULL);
52 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
53 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
64 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
65 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
67 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
70 static AppTime g_appTime;
73 void CopyCStringToPascal( const char* src, unsigned char dst[256])
75 int len = strlen( src);
77 memcpy( dst + 1, src, len);
81 void CopyPascalStringToC( const unsigned char* src, char* dst)
84 memcpy( dst, src + 1, len);
92 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
94 counter -= g_appTime.baseCounter;
97 time.lo = (unsigned long)counter;
98 time.hi = (unsigned long)(counter >> 32);
103 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
105 __int64 value = a.hi;
108 __int64 value2 = b.hi;
114 return durationImmediate;
116 __int64 frac = value % g_appTime.counterRate;
117 value /= g_appTime.counterRate;
123 frac /= g_appTime.counterRate;
124 time = (Duration)frac;
127 frac /= g_appTime.counterRate;
130 time = (Duration)value;
138 #include <sys/types.h>
144 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
145 // the zlib license on physfs allows this cut-and-pasting.
146 static int locateOneElement(char *buf)
151 //if (PHYSFS_exists(buf))
152 if (access(buf, F_OK) == 0)
153 return(1); /* quick rejection: exists in current case. */
155 ptr = strrchr(buf, '/'); /* find entry at end of path. */
164 ptr++; /* point past dirsep to entry itself. */
168 while ((dent = readdir(dirp)) != NULL) {
169 if (strcasecmp(dent->d_name, ptr) == 0) {
170 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
176 /* no match at all... */
179 } /* locateOneElement */
182 static inline const char *getUserDirByUID(void)
184 struct passwd *pw = getpwuid(getuid());
188 } /* getUserDirByUID */
191 static inline const char *getPrefPath(void)
193 static char *prefpath = NULL;
194 if (prefpath == NULL) {
195 const char *homedir = getenv("HOME");
197 homedir = getUserDirByUID();
199 homedir = "."; // oh well.
201 #if (defined(__APPLE__) && defined(__MACH__))
202 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
204 const char *PREFPATHNAME = ".lugaru";
206 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
207 prefpath = new char[len];
208 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
213 static int locateCorrectCase(char *buf, bool makedirs)
218 while (ptr = strchr(ptr + 1, '/')) {
219 *ptr = '\0'; /* block this path section off */
220 rc = locateOneElement(buf);
222 if (makedirs) /* normal if we're writing; build dirs! */
225 *ptr = '/'; /* restore path separator */
226 return(-2); /* missing element in path. */
229 *ptr = '/'; /* restore path separator */
232 /* check final element... */
233 return(locateOneElement(buf) ? 0 : -1);
237 static int locateCorrectFile(char *buf, const char *mode)
240 return(0); /* Uh...I guess that's failure. */
242 assert((mode[0] == 'w') || (mode[0] == 'r'));
244 bool iswriting = (mode[0] == 'w');
245 const char *prefpath = getPrefPath();
246 size_t len = strlen(buf) + strlen(prefpath) + 2;
247 char *prefpathfile = (char *) alloca(len);
248 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
250 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
251 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
252 strcpy(buf, prefpathfile);
253 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
254 rc = locateCorrectCase(buf, iswriting);
257 } /* locateCorrectFile */
261 static char g_filename[4096];
262 char* ConvertFileName( const char* orgfilename, const char *mode)
264 if (orgfilename == g_filename) // recursion?
267 // translate filename into proper path name
268 if (orgfilename[ 0] == ':')
270 strcpy( g_filename, orgfilename);
272 for (int n = 0; g_filename[ n]; n++) {
273 if (g_filename[ n] == ':')
274 g_filename[ n] = '/';
276 else if (g_filename[ n] == '\\')
277 g_filename[ n] = '/';
281 locateCorrectFile(g_filename, mode);