1 // cmdlib.h
2
3
4 #ifndef __CMDLIB__
5 #define __CMDLIB__
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <ctype.h>
13 #include <stdarg.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16
17 #ifdef WIN32
18 #pragma warning (disable:4237)
19 #include <io.h>
20 #include <direct.h>
21 #define lseek _lseek
22 #define close _close
23 #define mkdir _mkdir
24 #else
25 #include <unistd.h>
26 #include <sys/fcntl.h>
27 #include <sys/file.h>
28 #endif
29
30 #ifdef NeXT
31 #include <libc.h>
32 #endif
33
34
35 #ifndef true
36 #define true 1
37 #endif
38 #ifndef false
39 #define false 0
40 #endif
41
42 #ifndef __BYTEBOOL__
43 #define __BYTEBOOL__
44 //typedef enum {false, true} boolean;
45 //typedef bool boolean;
46 typedef unsigned char boolean; //make windows.h compatible
47 typedef unsigned char byte;
48 #endif
49
50 // the dec offsetof macro doesn't work very well...
51 #define myoffsetof(type,identifier) ((size_t)&((type *)0)->identifier)
52
53
54 // set these before calling CheckParm
55 extern int myargc;
56 extern char **myargv;
57
58 char *strupr (char *in);
59 char *strlower (char *in);
60
61 #ifndef WIN32
62 int filelength (int handle);
63 int tell (int handle);
64 #endif
65
66 time_t I_FloatTime (void);
67
68 void EndProgram (boolean completed);
69 void Sys_Error (char *error, ...);
70 int CheckParm (char *check);
71 char *va(char *format, ...);
72
73 int SafeOpenWrite (char *filename);
74 int SafeOpenRead (char *filename);
75 void SafeRead (int handle, void *buffer, long count);
76 void SafeWrite (int handle, void *buffer, long count);
77
78
79 long LoadFile (char *filename, void **bufferptr);
80 void SaveFile (char *filename, void *buffer, long count);
81
82 void DefaultExtension (char *path, char *extension);
83 void DefaultPath (char *path, char *basepath);
84 void StripFilename (char *path);
85 void StripExtension (char *path);
86
87 void ExtractFilename (char *path, char *dest);
88
89 long ParseNum (char *str);
90
91 #ifndef INLINE
92 short BigShort (short l);
93 short LittleShort (short l);
94 long BigLong (long l);
95 long LittleLong (long l);
96 float BigFloat (float l);
97 float LittleFloat (float l);
98 #endif
99
100 char *COM_Parse (char *data);
101
102 extern char com_token[1024];
103 extern int com_eof;
104
105 #ifdef INLINE
106 #ifdef __BIG_ENDIAN__
107
108 inline static short
LittleShort(short l)109 LittleShort (short l)
110 {
111 byte b1,b2;
112
113 b1 = l&255;
114 b2 = (l>>8)&255;
115
116 return (b1<<8) + b2;
117 }
118
119 inline static short
BigShort(short l)120 BigShort (short l)
121 {
122 return l;
123 }
124
125 inline static long
LittleLong(long l)126 LittleLong (long l)
127 {
128 byte b1,b2,b3,b4;
129
130 b1 = l&255;
131 b2 = (l>>8)&255;
132 b3 = (l>>16)&255;
133 b4 = (l>>24)&255;
134
135 return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
136 }
137
138 inline static long
BigLong(long l)139 BigLong (long l)
140 {
141 return l;
142 }
143
144
145 inline static float
LittleFloat(float l)146 LittleFloat (float l)
147 {
148 union {byte b[4]; float f;} in, out;
149
150 in.f = l;
151 out.b[0] = in.b[3];
152 out.b[1] = in.b[2];
153 out.b[2] = in.b[1];
154 out.b[3] = in.b[0];
155
156 return out.f;
157 }
158
159 inline static float
BigFloat(float l)160 BigFloat (float l)
161 {
162 return l;
163 }
164
165
166 #else
167
168
169 inline static short
BigShort(short l)170 BigShort (short l)
171 {
172 byte b1,b2;
173
174 b1 = l&255;
175 b2 = (l>>8)&255;
176
177 return (b1<<8) + b2;
178 }
179
180 inline static short
LittleShort(short l)181 LittleShort (short l)
182 {
183 return l;
184 }
185
186 inline static long
BigLong(long l)187 BigLong (long l)
188 {
189 byte b1,b2,b3,b4;
190
191 b1 = l&255;
192 b2 = (l>>8)&255;
193 b3 = (l>>16)&255;
194 b4 = (l>>24)&255;
195
196 return ((long)b1<<24) + ((long)b2<<16) + ((long)b3<<8) + b4;
197 }
198
199 inline static long
LittleLong(long l)200 LittleLong (long l)
201 {
202 return l;
203 }
204
205 inline static float
BigFloat(float l)206 BigFloat (float l)
207 {
208 union {byte b[4]; float f;} in, out;
209
210 in.f = l;
211 out.b[0] = in.b[3];
212 out.b[1] = in.b[2];
213 out.b[2] = in.b[1];
214 out.b[3] = in.b[0];
215
216 return out.f;
217 }
218
219 inline static float
LittleFloat(float l)220 LittleFloat (float l)
221 {
222 return l;
223 }
224
225 #endif
226
227 #endif
228
229 #endif
230
231 #ifndef O_BINARY
232 #define O_BINARY 0
233 #endif
234