4 #include "MacCompatibility.h"
20 typedef long long __int64;
21 typedef __int64 LARGE_INTEGER;
22 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
24 assert(sizeof (__int64) == 8);
25 assert(sizeof (LARGE_INTEGER) == 8);
30 static int QueryPerformanceCounter(LARGE_INTEGER *liptr)
33 gettimeofday(&tv, NULL);
34 *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
35 (((LARGE_INTEGER) tv.tv_usec) / 1000) );
47 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
48 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
50 __int64 counterRate; // LARGE_INTEGER type has no math functions so use int64
53 static AppTime g_appTime;
56 void CopyCStringToPascal( const char* src, unsigned char dst[256])
58 int len = strlen( src);
60 memcpy( dst + 1, src, len);
64 void CopyPascalStringToC( const unsigned char* src, char* dst)
67 memcpy( dst, src + 1, len);
75 QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
77 counter -= g_appTime.baseCounter;
80 time.lo = (unsigned long)counter;
81 time.hi = (unsigned long)(counter >> 32);
86 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
91 __int64 value2 = b.hi;
97 return durationImmediate;
99 __int64 frac = value % g_appTime.counterRate;
100 value /= g_appTime.counterRate;
107 frac /= g_appTime.counterRate;
108 time = (Duration)frac;
113 frac /= g_appTime.counterRate;
116 time = (Duration)value;
124 #include <sys/types.h>
130 // some but not all of this is code from PhysicsFS: http://icculus.org/physfs/
131 // the zlib license on physfs allows this cut-and-pasting.
132 static int locateOneElement(char *buf)
139 //if (PHYSFS_exists(buf))
140 if (access(buf, F_OK) == 0)
141 return(1); /* quick rejection: exists in current case. */
143 ptr = strrchr(buf, '/'); /* find entry at end of path. */
154 ptr++; /* point past dirsep to entry itself. */
158 while ((dent = readdir(dirp)) != NULL)
160 if (stricmp(dent->d_name, ptr) == 0)
162 strcpy(ptr, dent->d_name); /* found a match. Overwrite with this case. */
168 /* no match at all... */
171 } /* locateOneElement */
174 static inline const char *getUserDirByUID(void)
176 struct passwd *pw = getpwuid(getuid());
180 } /* getUserDirByUID */
183 static inline const char *getPrefPath(void)
185 static char *prefpath = NULL;
186 if (prefpath == NULL)
188 const char *homedir = getenv("HOME");
190 homedir = getUserDirByUID();
192 homedir = "."; // oh well.
194 const char *PREFPATHNAME = ".lugaru";
195 size_t len = strlen(homedir) + strlen(PREFPATHNAME) + 2;
196 prefpath = new char[len];
197 snprintf(prefpath, len, "%s/%s", homedir, PREFPATHNAME);
202 static int locateCorrectCase(char *buf, bool makedirs)
209 while (ptr = strchr(ptr + 1, '/'))
211 *ptr = '\0'; /* block this path section off */
212 rc = locateOneElement(buf);
215 if (makedirs) /* normal if we're writing; build dirs! */
219 *ptr = '/'; /* restore path separator */
220 return(-2); /* missing element in path. */
223 *ptr = '/'; /* restore path separator */
226 /* check final element... */
227 return(locateOneElement(buf) ? 0 : -1);
231 static int locateCorrectFile(char *buf, const char *mode)
234 return(0); /* Uh...I guess that's failure. */
236 assert((mode[0] == 'w') || (mode[0] == 'r'));
238 bool iswriting = (mode[0] == 'w');
239 const char *prefpath = getPrefPath();
240 size_t len = strlen(buf) + strlen(prefpath) + 2;
241 char *prefpathfile = (char *) alloca(len);
242 snprintf(prefpathfile, len, "%s/%s", prefpath, buf);
244 int rc = locateCorrectCase(prefpathfile, iswriting); /* favor prefpath. */
245 if ( (rc == 0) || ((rc == -1) && (iswriting)) ) // found or create?
246 strcpy(buf, prefpathfile);
247 else if ((rc < 0) && (!iswriting)) /* not writing? Try game dir... */
248 rc = locateCorrectCase(buf, iswriting);
251 } /* locateCorrectFile */
255 static char g_filename[4096];
256 char* ConvertFileName( const char* orgfilename, const char *mode)
258 if (orgfilename == g_filename) // recursion?
261 // translate filename into proper path name
262 if (orgfilename[ 0] == ':')
264 strcpy( g_filename, orgfilename);
266 for (int n = 0; g_filename[ n]; n++)
268 if (g_filename[ n] == ':')
269 g_filename[ n] = '/';
271 else if (g_filename[ n] == '\\')
272 g_filename[ n] = '/';
276 locateCorrectFile(g_filename, mode);