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) );
68 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
69 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
71 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
74 static AppTime g_appTime;
77 void CopyCStringToPascal( const char* src, unsigned char dst[256])
79 int len = strlen( src);
81 memcpy( dst + 1, src, len);
85 void CopyPascalStringToC( const unsigned char* src, char* dst)
88 memcpy( dst, src + 1, len);
96 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
98 counter -= g_appTime.baseCounter;
101 time.lo = (unsigned long)counter;
102 time.hi = (unsigned long)(counter >> 32);
107 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
109 __int64 value = a.hi;
112 __int64 value2 = b.hi;
118 return durationImmediate;
120 __int64 frac = value % g_appTime.counterRate;
121 value /= g_appTime.counterRate;
127 frac /= g_appTime.counterRate;
128 time = (Duration)frac;
131 frac /= g_appTime.counterRate;
134 time = (Duration)value;
142 #include <sys/types.h>
148 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
149 // the zlib license on physfs allows this cut-and-pasting.
150 static int locateOneElement(char *buf)
155 //if (PHYSFS_exists(buf))
156 if (access(buf, F_OK) == 0)
157 return(1); /* quick rejection: exists in current case. */
159 ptr = strrchr(buf, '/'); /* find entry at end of path. */
168 ptr++; /* point past dirsep to entry itself. */
172 while ((dent = readdir(dirp)) != NULL) {
173 if (strcasecmp(dent->d_name, ptr) == 0) {
174 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
180 /* no match at all... */
183 } /* locateOneElement */
186 static inline const char *getUserDirByUID(void)
188 struct passwd *pw = getpwuid(getuid());
192 } /* getUserDirByUID */
195 static inline const char *getPrefPath(void)
197 static char *prefpath = NULL;
198 if (prefpath == NULL) {
199 const char *homedir = getenv("HOME");
201 homedir = getUserDirByUID();
203 homedir = "."; // oh well.
205 #if (defined(__APPLE__) && defined(__MACH__))
206 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
208 const char *PREFPATHNAME = ".lugaru";
210 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
211 prefpath = new char[len];
212 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
217 static int locateCorrectCase(char *buf, bool makedirs)
224 while (ptr = strchr(ptr + 1, '/')) {
225 *ptr = '\0'; /* block this path section off */
226 rc = locateOneElement(buf);
228 if (makedirs) /* normal if we're writing; build dirs! */
231 *ptr = '/'; /* restore path separator */
232 return(-2); /* missing element in path. */
235 *ptr = '/'; /* restore path separator */
238 /* check final element... */
239 return(locateOneElement(buf) ? 0 : -1);
243 static int locateCorrectFile(char *buf, const char *mode)
246 return(0); /* Uh...I guess that's failure. */
248 assert((mode[0] == 'w') || (mode[0] == 'r'));
250 bool iswriting = (mode[0] == 'w');
251 const char *prefpath = getPrefPath();
252 size_t len = strlen(buf) + strlen(prefpath) + 2;
253 char *prefpathfile = (char *) alloca(len);
254 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
256 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
257 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
258 strcpy(buf, prefpathfile);
259 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
260 rc = locateCorrectCase(buf, iswriting);
263 } /* locateCorrectFile */
267 static char g_filename[4096];
268 char* ConvertFileName( const char* orgfilename, const char *mode)
270 if (orgfilename == g_filename) // recursion?
273 // translate filename into proper path name
274 if (orgfilename[ 0] == ':')
276 strcpy( g_filename, orgfilename);
278 for (int n = 0; g_filename[ n]; n++) {
279 if (g_filename[ n] == ':')
280 g_filename[ n] = '/';
282 else if (g_filename[ n] == '\\')
283 g_filename[ n] = '/';
287 locateCorrectFile(g_filename, mode);