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)
157 //if (PHYSFS_exists(buf))
158 if (access(buf, F_OK) == 0)
159 return(1); /* quick rejection: exists in current case. */
161 ptr = strrchr(buf, '/'); /* find entry at end of path. */
170 ptr++; /* point past dirsep to entry itself. */
174 while ((dent = readdir(dirp)) != NULL) {
175 if (strcasecmp(dent->d_name, ptr) == 0) {
176 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
182 /* no match at all... */
185 } /* locateOneElement */
188 static inline const char *getUserDirByUID(void)
190 struct passwd *pw = getpwuid(getuid());
194 } /* getUserDirByUID */
197 static inline const char *getPrefPath(void)
199 static char *prefpath = NULL;
200 if (prefpath == NULL) {
201 const char *homedir = getenv("HOME");
203 homedir = getUserDirByUID();
205 homedir = "."; // oh well.
207 #if (defined(__APPLE__) && defined(__MACH__))
208 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
210 const char *PREFPATHNAME = ".lugaru";
212 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
213 prefpath = new char[len];
214 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
219 static int locateCorrectCase(char *buf, bool makedirs)
226 while (ptr = strchr(ptr + 1, '/')) {
227 *ptr = '\0'; /* block this path section off */
228 rc = locateOneElement(buf);
230 if (makedirs) /* normal if we're writing; build dirs! */
233 *ptr = '/'; /* restore path separator */
234 return(-2); /* missing element in path. */
237 *ptr = '/'; /* restore path separator */
240 /* check final element... */
241 return(locateOneElement(buf) ? 0 : -1);
245 static int locateCorrectFile(char *buf, const char *mode)
248 return(0); /* Uh...I guess that's failure. */
250 assert((mode[0] == 'w') || (mode[0] == 'r'));
252 bool iswriting = (mode[0] == 'w');
253 const char *prefpath = getPrefPath();
254 size_t len = strlen(buf) + strlen(prefpath) + 2;
255 char *prefpathfile = (char *) alloca(len);
256 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
258 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
259 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
260 strcpy(buf, prefpathfile);
261 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
262 rc = locateCorrectCase(buf, iswriting);
265 } /* locateCorrectFile */
269 static char g_filename[4096];
270 char* ConvertFileName( const char* orgfilename, const char *mode)
272 if (orgfilename == g_filename) // recursion?
275 // translate filename into proper path name
276 if (orgfilename[ 0] == ':')
278 strcpy( g_filename, orgfilename);
280 for (int n = 0; g_filename[ n]; n++) {
281 if (g_filename[ n] == ':')
282 g_filename[ n] = '/';
284 else if (g_filename[ n] == '\\')
285 g_filename[ n] = '/';
289 locateCorrectFile(g_filename, mode);