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