aboutsummaryrefslogtreecommitdiff
path: root/archive/include/unix_compat.h
blob: 56ec4b3ae4b2bfbdfaca33a20cb79b9e33c49983 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef _INCL_UNIX_COMPAT_H_
#define _INCL_UNIX_COMPAT_H_

#if (defined(__APPLE__) && defined(__MACH__))
#define PLATFORM_MACOSX 1
#endif

#if ( defined(unix) || PLATFORM_MACOSX )
#define PLATFORM_UNIX 1
#endif

// Useful to sprinkle around the codebase without a bunch of #ifdefs...
#ifdef _WINDOWS
#define BYTESWAP(x)
#define STUBBED(x)
#endif

// don't want rest of this header on Windows, etc.
#if (PLATFORM_UNIX)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>

#include "/usr/include/SDL/SDL.h"

#define _MAX_PATH PATH_MAX
#define MAX_PATH PATH_MAX

typedef int64_t __int64;
typedef uint32_t DWORD;
typedef uint64_t UINT64;
typedef uint8_t BYTE;
typedef void *HANDLE;
typedef HANDLE HWND;
typedef int32_t BOOL;

static inline DWORD timeGetTime(void)
{
    return SDL_GetTicks();
} // timeGetTime


// macro so I know what is still on the TODO list...
#if 1
#define STUBBED(x)
#else
void CalledSTUBBED(void);  // you can set a breakpoint on this.
#define STUBBED(x) \
do { \
    static bool seen_this = false; \
    if (!seen_this) { \
        seen_this = true; \
        fprintf(stderr, "STUBBED: %s at %s (%s:%d)\n", x, __FUNCTION__, __FILE__, __LINE__); \
        fflush(stderr); \
        CalledSTUBBED(); \
    } \
} while (false)
#endif

static inline char *itoa(const int i, char *s, const int radix)
{
    assert(radix == 10);
    sprintf(s, "%d", i);
    return s;
}

static inline char *_i64toa(const __int64 i, char *s, const int radix)
{
    assert(radix == 10);
    assert(sizeof (long long) == sizeof (__int64));
    sprintf(s, "%lld", (long long) i);
    return s;
}

static inline __int64 _atoi64(const char *str)
{
    return (__int64) strtoll(str, NULL, 10);
}

static inline void Sleep(const int ms)
{
    usleep(ms * 1000);
}

static inline char *_gcvt(const double value, const int digits, char *buffer)
{
    char fmt[32];
    snprintf(fmt, sizeof (fmt), "%%.%dg", digits);
    sprintf(buffer, fmt, value);
    return buffer;
}

#define ZeroMemory(a,b) memset(a, '\0', b)

#ifdef __cplusplus
#ifdef max
#undef max
#endif
template <class T> inline const T &max(const T &a, const T &b) { return (a > b) ? a : b; }
#ifdef min
#undef min
#endif
template <class T> inline const T &min(const T &a, const T &b) { return (a < b) ? a : b; }
#endif

// Byteswap magic...

#if SDL_BYTEORDER == SDL_BIG_ENDIAN
#define PLATFORM_BIGENDIAN 1
#define PLATFORM_LITTLEENDIAN 0
#else
#define PLATFORM_BIGENDIAN 0
#define PLATFORM_LITTLEENDIAN 1
#endif

#if PLATFORM_BIGENDIAN
    #define SWAPPER64(t) \
        inline void BYTESWAP(t &x) { \
            union { t orig; Uint64 ui; } swapper; \
            swapper.orig = x; \
            swapper.ui = SDL_SwapLE64(swapper.ui); \
            x = swapper.orig; \
        }
    #define SWAPPER32(t) \
        inline void BYTESWAP(t &x) { \
            union { t orig; Uint32 ui; } swapper; \
            swapper.orig = x; \
            swapper.ui = SDL_SwapLE32(swapper.ui); \
            x = swapper.orig; \
        }
    #define SWAPPER16(t) \
        inline void BYTESWAP(t &x) { \
            union { t orig; Uint16 ui; } swapper; \
            swapper.orig = x; \
            swapper.ui = SDL_SwapLE16(swapper.ui); \
            x = swapper.orig; \
        }
    #define SWAPPER8(t) inline void BYTESWAP(t &_x) {}
    SWAPPER64(double)
    SWAPPER32(size_t)   // !!! FIXME: this will fail on gnuc/amd64.
    SWAPPER32(int)
    SWAPPER32(float)
    SWAPPER32(DWORD)
    SWAPPER16(WORD)
    SWAPPER16(short)
    SWAPPER8(BYTE)
    #undef SWAPPER32
    #undef SWAPPER16
    #undef SWAPPER8
#else
    #define BYTESWAP(x)
#endif

#endif  // PLATFORM_UNIX

#endif  // include-once blocker.