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
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
11 This program 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.
15 See the GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 /**> HEADER FILES <**/
25 #include "MacCompatibility.h"
42 typedef long long __int64;
43 typedef __int64 LARGE_INTEGER;
44 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
46 assert(sizeof (__int64) == 8);
47 assert(sizeof (LARGE_INTEGER) == 8);
52 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
55 gettimeofday(&tv, NULL);
56 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
57 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
69 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
70 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
72 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
75 static AppTime g_appTime;
78 void CopyCStringToPascal( const char* src, unsigned char dst[256])
80 int len = strlen( src);
82 memcpy( dst + 1, src, len);
86 void CopyPascalStringToC( const unsigned char* src, char* dst)
89 memcpy( dst, src + 1, len);
97 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
99 counter -= g_appTime.baseCounter;
102 time.lo = (unsigned long)counter;
103 time.hi = (unsigned long)(counter >> 32);
108 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
110 __int64 value = a.hi;
113 __int64 value2 = b.hi;
119 return durationImmediate;
121 __int64 frac = value % g_appTime.counterRate;
122 value /= g_appTime.counterRate;
129 frac /= g_appTime.counterRate;
130 time = (Duration)frac;
135 frac /= g_appTime.counterRate;
138 time = (Duration)value;
146 #include <sys/types.h>
152 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
153 // the zlib license on physfs allows this cut-and-pasting.
154 static int locateOneElement(char *buf)
161 //if (PHYSFS_exists(buf))
162 if (access(buf, F_OK) == 0)
163 return(1); /* quick rejection: exists in current case. */
165 ptr = strrchr(buf, '/'); /* find entry at end of path. */
176 ptr++; /* point past dirsep to entry itself. */
180 while ((dent = readdir(dirp)) != NULL)
182 if (strcasecmp(dent->d_name, ptr) == 0)
184 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
190 /* no match at all... */
193 } /* locateOneElement */
196 static inline const char *getUserDirByUID(void)
198 struct passwd *pw = getpwuid(getuid());
202 } /* getUserDirByUID */
205 static inline const char *getPrefPath(void)
207 static char *prefpath = NULL;
208 if (prefpath == NULL)
210 const char *homedir = getenv("HOME");
212 homedir = getUserDirByUID();
214 homedir = "."; // oh well.
216 #if (defined(__APPLE__) && defined(__MACH__))
217 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
219 const char *PREFPATHNAME = ".lugaru";
221 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
222 prefpath = new char[len];
223 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
228 static int locateCorrectCase(char *buf, bool makedirs)
235 while (ptr = strchr(ptr + 1, '/'))
237 *ptr = '\0'; /* block this path section off */
238 rc = locateOneElement(buf);
241 if (makedirs) /* normal if we're writing; build dirs! */
245 *ptr = '/'; /* restore path separator */
246 return(-2); /* missing element in path. */
249 *ptr = '/'; /* restore path separator */
252 /* check final element... */
253 return(locateOneElement(buf) ? 0 : -1);
257 static int locateCorrectFile(char *buf, const char *mode)
260 return(0); /* Uh...I guess that's failure. */
262 assert((mode[0] == 'w') || (mode[0] == 'r'));
264 bool iswriting = (mode[0] == 'w');
265 const char *prefpath = getPrefPath();
266 size_t len = strlen(buf) + strlen(prefpath) + 2;
267 char *prefpathfile = (char *) alloca(len);
268 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
270 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
271 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
272 strcpy(buf, prefpathfile);
273 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
274 rc = locateCorrectCase(buf, iswriting);
277 } /* locateCorrectFile */
281 static char g_filename[4096];
282 char* ConvertFileName( const char* orgfilename, const char *mode)
284 if (orgfilename == g_filename) // recursion?
287 // translate filename into proper path name
288 if (orgfilename[ 0] == ':')
290 strcpy( g_filename, orgfilename);
292 for (int n = 0; g_filename[ n]; n++)
294 if (g_filename[ n] == ':')
295 g_filename[ n] = '/';
297 else if (g_filename[ n] == '\\')
298 g_filename[ n] = '/';
302 locateCorrectFile(g_filename, mode);