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/>.
22 /**> HEADER FILES <**/
23 #include "MacCompatibility.h"
40 typedef long long __int64;
41 typedef __int64 LARGE_INTEGER;
42 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
44 assert(sizeof (__int64) == 8);
45 assert(sizeof (LARGE_INTEGER) == 8);
50 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
53 gettimeofday(&tv, NULL);
54 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
55 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
66 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
67 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
69 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
72 static AppTime g_appTime;
75 void CopyCStringToPascal( const char* src, unsigned char dst[256])
77 int len = strlen( src);
79 memcpy( dst + 1, src, len);
83 void CopyPascalStringToC( const unsigned char* src, char* dst)
86 memcpy( dst, src + 1, len);
94 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
96 counter -= g_appTime.baseCounter;
99 time.lo = (unsigned long)counter;
100 time.hi = (unsigned long)(counter >> 32);
105 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
107 __int64 value = a.hi;
110 __int64 value2 = b.hi;
116 return durationImmediate;
118 __int64 frac = value % g_appTime.counterRate;
119 value /= g_appTime.counterRate;
125 frac /= g_appTime.counterRate;
126 time = (Duration)frac;
129 frac /= g_appTime.counterRate;
132 time = (Duration)value;
140 #include <sys/types.h>
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)
153 //if (PHYSFS_exists(buf))
154 if (access(buf, F_OK) == 0)
155 return(1); /* quick rejection: exists in current case. */
157 ptr = strrchr(buf, '/'); /* find entry at end of path. */
166 ptr++; /* point past dirsep to entry itself. */
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. */
178 /* no match at all... */
181 } /* locateOneElement */
184 static inline const char *getUserDirByUID(void)
186 struct passwd *pw = getpwuid(getuid());
190 } /* getUserDirByUID */
193 static inline const char *getPrefPath(void)
195 static char *prefpath = NULL;
196 if (prefpath == NULL) {
197 const char *homedir = getenv("HOME");
199 homedir = getUserDirByUID();
201 homedir = "."; // oh well.
203 #if (defined(__APPLE__) && defined(__MACH__))
204 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
206 const char *PREFPATHNAME = ".lugaru";
208 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
209 prefpath = new char[len];
210 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
215 static int locateCorrectCase(char *buf, bool makedirs)
220 while (ptr = strchr(ptr + 1, '/')) {
221 *ptr = '\0'; /* block this path section off */
222 rc = locateOneElement(buf);
224 if (makedirs) /* normal if we're writing; build dirs! */
227 *ptr = '/'; /* restore path separator */
228 return(-2); /* missing element in path. */
231 *ptr = '/'; /* restore path separator */
234 /* check final element... */
235 return(locateOneElement(buf) ? 0 : -1);
239 static int locateCorrectFile(char *buf, const char *mode)
242 return(0); /* Uh...I guess that's failure. */
244 assert((mode[0] == 'w') || (mode[0] == 'r'));
246 bool iswriting = (mode[0] == 'w');
247 const char *prefpath = getPrefPath();
248 size_t len = strlen(buf) + strlen(prefpath) + 2;
249 char *prefpathfile = (char *) alloca(len);
250 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
252 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
253 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
254 strcpy(buf, prefpathfile);
255 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
256 rc = locateCorrectCase(buf, iswriting);
259 } /* locateCorrectFile */
263 static char g_filename[4096];
264 char* ConvertFileName( const char* orgfilename, const char *mode)
266 if (orgfilename == g_filename) // recursion?
269 // translate filename into proper path name
270 if (orgfilename[ 0] == ':')
272 strcpy( g_filename, orgfilename);
274 for (int n = 0; g_filename[ n]; n++) {
275 if (g_filename[ n] == ':')
276 g_filename[ n] = '/';
278 else if (g_filename[ n] == '\\')
279 g_filename[ n] = '/';
283 locateCorrectFile(g_filename, mode);