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