2 Copyright (C) 2003, 2010 - Wolfire Games
3 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
5 This file is part of Lugaru.
7 Lugaru is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 Lugaru is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
21 /**> HEADER FILES <**/
22 #include "MacCompatibility.h"
39 typedef long long __int64;
40 typedef __int64 LARGE_INTEGER;
41 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
43 assert(sizeof (__int64) == 8);
44 assert(sizeof (LARGE_INTEGER) == 8);
49 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
52 gettimeofday(&tv, NULL);
53 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
54 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
65 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
66 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
68 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
71 static AppTime g_appTime;
76 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
78 counter -= g_appTime.baseCounter;
81 time.lo = (unsigned long)counter;
82 time.hi = (unsigned long)(counter >> 32);
87 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
92 __int64 value2 = b.hi;
98 return durationImmediate;
100 __int64 frac = value % g_appTime.counterRate;
101 value /= g_appTime.counterRate;
107 frac /= g_appTime.counterRate;
108 time = (Duration)frac;
111 frac /= g_appTime.counterRate;
114 time = (Duration)value;
122 #include <sys/types.h>
128 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
129 // the zlib license on physfs allows this cut-and-pasting.
130 static int locateOneElement(char *buf)
135 if (access(buf, F_OK) == 0)
136 return(1); /* quick rejection: exists in current case. */
138 ptr = strrchr(buf, '/'); /* find entry at end of path. */
147 ptr++; /* point past dirsep to entry itself. */
151 while ((dent = readdir(dirp)) != NULL) {
152 if (strcasecmp(dent->d_name, ptr) == 0) {
153 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
159 /* no match at all... */
162 } /* locateOneElement */
165 static inline const char *getUserDirByUID(void)
167 struct passwd *pw = getpwuid(getuid());
171 } /* getUserDirByUID */
174 static inline const char *getPrefPath(void)
176 static char *prefpath = NULL;
177 if (prefpath == NULL) {
178 const char *homedir = getenv("HOME");
180 homedir = getUserDirByUID();
182 homedir = "."; // oh well.
184 #if (defined(__APPLE__) && defined(__MACH__))
185 const char *PREFPATHNAME = "Library/Application Support/Lugaru";
187 const char *PREFPATHNAME = ".lugaru";
189 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
190 prefpath = new char[len];
191 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
196 static int locateCorrectCase(char *buf, bool makedirs)
201 while (ptr = strchr(ptr + 1, '/')) {
202 *ptr = '\0'; /* block this path section off */
203 rc = locateOneElement(buf);
205 if (makedirs) /* normal if we're writing; build dirs! */
208 *ptr = '/'; /* restore path separator */
209 return(-2); /* missing element in path. */
212 *ptr = '/'; /* restore path separator */
215 /* check final element... */
216 return(locateOneElement(buf) ? 0 : -1);
220 static int locateCorrectFile(char *buf, const char *mode)
223 return(0); /* Uh...I guess that's failure. */
225 assert((mode[0] == 'w') || (mode[0] == 'r'));
227 bool iswriting = (mode[0] == 'w');
228 const char *prefpath = getPrefPath();
229 size_t len = strlen(buf) + strlen(prefpath) + 2;
230 char *prefpathfile = (char *) alloca(len);
231 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
233 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
234 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
235 strcpy(buf, prefpathfile);
236 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
237 rc = locateCorrectCase(buf, iswriting);
240 } /* locateCorrectFile */
244 static char g_filename[4096];
245 char* ConvertFileName( const char* orgfilename, const char *mode)
247 if (orgfilename == g_filename) // recursion?
250 // translate filename into proper path name
251 if (orgfilename[ 0] == ':')
253 strcpy( g_filename, orgfilename);
255 for (int n = 0; g_filename[ n]; n++) {
256 if (g_filename[ n] == ':')
257 g_filename[ n] = '/';
259 else if (g_filename[ n] == '\\')
260 g_filename[ n] = '/';
264 locateCorrectFile(g_filename, mode);