]> git.jsancho.org Git - lugaru.git/blob - Source/MacCompatibility.cpp
86004e08bf093d2b73d423e57c67046c4ee1905b
[lugaru.git] / Source / MacCompatibility.cpp
1 #if !PLATFORM_MACOSX
2
3 /**> HEADER FILES <**/
4 #include "MacCompatibility.h"
5 #include <windows.h>
6 #include <errno.h>
7 #include <time.h>
8 #include <stdio.h>
9
10 #if PLATFORM_UNIX
11 typedef long long __int64;
12 typedef __int64 LARGE_INTEGER;
13 static int QueryPerformanceFrequency(LARGE_INTEGER *liptr)
14 {
15     assert(sizeof (__int64) == 8);
16     assert(sizeof (LARGE_INTEGER) == 8);
17     *liptr = 1000;
18     return(1);
19 }
20
21 static void QueryPerformanceCounter(LARGE_INTEGER *liptr)
22 {
23     struct timeval tv;
24     gettimeofday(&tv, NULL);
25     *liptr = ( (((LARGE_INTEGER) tv.tv_sec) * 1000) +
26                (((LARGE_INTEGER) tv.tv_usec) / 1000) );
27     return(1);
28 }
29 #endif
30
31 class AppTime
32 {
33 public:
34         AppTime()
35         {
36                 counterRate = 1;
37                 baseCounter = 0;
38                 QueryPerformanceFrequency( (LARGE_INTEGER*)&counterRate);
39                 QueryPerformanceCounter( (LARGE_INTEGER*)&baseCounter);
40         }
41         __int64 counterRate;            // LARGE_INTEGER type has no math functions so use int64
42         __int64 baseCounter;
43 };
44 static AppTime g_appTime;
45
46
47 void CopyCStringToPascal( const char* src, unsigned char dst[256])
48 {
49         int len = strlen( src);
50         dst[ 0] = len;
51         memcpy( dst + 1, src, len);
52 }
53
54
55 void CopyPascalStringToC( const unsigned char* src, char* dst)
56 {
57         int len = src[ 0];
58         memcpy( dst, src + 1, len);
59         dst[ len] = 0;
60 }
61
62
63 AbsoluteTime UpTime()
64 {
65         __int64 counter;
66         QueryPerformanceCounter( (LARGE_INTEGER*)&counter);
67
68         counter -= g_appTime.baseCounter;
69
70         AbsoluteTime time;
71         time.lo = (unsigned long)counter;
72         time.hi = (unsigned long)(counter >> 32);
73         return time;
74 }
75
76
77 Duration AbsoluteDeltaToDuration( AbsoluteTime& a, AbsoluteTime& b)
78 {
79         __int64 value = a.hi;
80         value <<= 32;
81         value |= a.lo;
82         __int64 value2 = b.hi;
83         value2 <<= 32;
84         value2 |= b.lo;
85         value -= value2;
86
87         if (value <= 0)
88                 return durationImmediate;
89
90         __int64 frac = value % g_appTime.counterRate;
91         value /= g_appTime.counterRate;
92
93         Duration time;
94
95         if (value == 0)
96         {
97                 frac *= -1000000;
98                 frac /= g_appTime.counterRate;
99                 time = (Duration)frac;
100         }
101         else
102         {
103                 frac *= 1000;
104                 frac /= g_appTime.counterRate;
105                 value *= 1000;
106                 value += frac;
107                 time = (Duration)value;
108         }
109
110         return time;
111 }
112
113
114 static char g_filename[ 256];
115 char* ConvertFileName( const char* orgfilename)
116 {
117         // translate filename into proper path name
118         if (orgfilename[ 0] == ':')
119                 orgfilename++;
120         strcpy( g_filename, orgfilename);
121
122         for (int n = 0; g_filename[ n]; n++)
123         {
124                 if (g_filename[ n] == ':')
125                         g_filename[ n] = '/';
126         }
127
128         return g_filename;
129 }
130
131 #endif
132