4 #include "MacCompatibility.h"
21 typedef long long __int64;
22 typedef __int64 LARGE_INTEGER;
23 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
25 assert(sizeof (__int64) == 8);
26 assert(sizeof (LARGE_INTEGER) == 8);
31 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
34 gettimeofday(&tv, NULL);
35 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
36 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
48 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
49 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
51 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
54 static AppTime g_appTime;
57 void CopyCStringToPascal( const char* src, unsigned char dst[256])
59 int len = strlen( src);
61 memcpy( dst + 1, src, len);
65 void CopyPascalStringToC( const unsigned char* src, char* dst)
68 memcpy( dst, src + 1, len);
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;
108 frac /= g_appTime.counterRate;
109 time = (Duration)frac;
114 frac /= g_appTime.counterRate;
117 time = (Duration)value;
125 #include <sys/types.h>
131 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
132 // the zlib license on physfs allows this cut-and-pasting.
133 static int locateOneElement(char *buf)
140 //if (PHYSFS_exists(buf))
141 if (access(buf, F_OK) == 0)
142 return(1); /* quick rejection: exists in current case. */
144 ptr = strrchr(buf, '/'); /* find entry at end of path. */
155 ptr++; /* point past dirsep to entry itself. */
159 while ((dent = readdir(dirp)) != NULL)
161 if (stricmp(dent->d_name, ptr) == 0)
163 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
169 /* no match at all... */
172 } /* locateOneElement */
175 static inline const char *getUserDirByUID(void)
177 struct passwd *pw = getpwuid(getuid());
181 } /* getUserDirByUID */
184 static inline const char *getPrefPath(void)
186 static char *prefpath = NULL;
187 if (prefpath == NULL)
189 const char *homedir = getenv("HOME");
191 homedir = getUserDirByUID();
193 homedir = "."; // oh well.
195 const char *PREFPATHNAME = ".lugaru";
196 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
197 prefpath = new char[len];
198 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
203 static int locateCorrectCase(char *buf, bool makedirs)
210 while (ptr = strchr(ptr + 1, '/'))
212 *ptr = '\0'; /* block this path section off */
213 rc = locateOneElement(buf);
216 if (makedirs) /* normal if we're writing; build dirs! */
220 *ptr = '/'; /* restore path separator */
221 return(-2); /* missing element in path. */
224 *ptr = '/'; /* restore path separator */
227 /* check final element... */
228 return(locateOneElement(buf) ? 0 : -1);
232 static int locateCorrectFile(char *buf, const char *mode)
235 return(0); /* Uh...I guess that's failure. */
237 assert((mode[0] == 'w') || (mode[0] == 'r'));
239 bool iswriting = (mode[0] == 'w');
240 const char *prefpath = getPrefPath();
241 size_t len = strlen(buf) + strlen(prefpath) + 2;
242 char *prefpathfile = (char *) alloca(len);
243 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
245 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
246 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
247 strcpy(buf, prefpathfile);
248 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
249 rc = locateCorrectCase(buf, iswriting);
252 } /* locateCorrectFile */
256 static char g_filename[4096];
257 char* ConvertFileName( const char* orgfilename, const char *mode)
259 if (orgfilename == g_filename) // recursion?
262 // translate filename into proper path name
263 if (orgfilename[ 0] == ':')
265 strcpy( g_filename, orgfilename);
267 for (int n = 0; g_filename[ n]; n++)
269 if (g_filename[ n] == ':')
270 g_filename[ n] = '/';
272 else if (g_filename[ n] == '\\')
273 g_filename[ n] = '/';
277 locateCorrectFile(g_filename, mode);