]> icculus.org git repositories - divverent/darkplaces.git/blob - common.h
more warnings fixed
[divverent/darkplaces.git] / common.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef COMMON_H
22 #define COMMON_H
23
24
25 // MSVC has a different name for several standard functions
26 #ifdef WIN32
27 # define strcasecmp stricmp
28 # define strncasecmp strnicmp
29 #endif
30
31 // Create our own define for Mac OS X
32 #if defined(__APPLE__) && defined(__MACH__)
33 # define MACOSX
34 #endif
35
36 // Create our own define for Solaris
37 #if defined(__sun__) && defined(__svr4__)
38 # define SUNOS
39 #endif
40
41 #ifdef SUNOS
42 #include <sys/file.h>           // Needed for FNDELAY
43 # define model_t dp_model_t // Workaround conflict with /usr/include/sys/model.h
44 #endif
45
46 //============================================================================
47
48 typedef struct sizebuf_s
49 {
50         qboolean        allowoverflow;  // if false, do a Sys_Error
51         qboolean        overflowed;             // set to true if the buffer size failed
52         qbyte           *data;
53         int                     maxsize;
54         int                     cursize;
55 } sizebuf_t;
56
57 void SZ_Clear (sizebuf_t *buf);
58 void *SZ_GetSpace (sizebuf_t *buf, int length);
59 void SZ_Write (sizebuf_t *buf, const void *data, int length);
60 void SZ_Print(sizebuf_t *buf, const char *data);        // strcats onto the sizebuf
61 void SZ_HexDumpToConsole(const sizebuf_t *buf);
62
63 void Com_HexDumpToConsole(const qbyte *data, int size);
64
65 unsigned short CRC_Block(const qbyte *data, size_t size);
66
67
68 //============================================================================
69 //                                                      Endianess handling
70 //============================================================================
71
72 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
73
74 // Initializations
75 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
76 # undef BYTE_ORDER
77 # undef LITTLE_ENDIAN
78 # undef BIG_ENDIAN
79 # define LITTLE_ENDIAN 1234
80 # define BIG_ENDIAN 4321
81 #endif
82
83 // If we still don't know the CPU endianess at this point, we try to guess
84 #ifndef BYTE_ORDER
85 # if defined(WIN32)
86 #  define BYTE_ORDER LITTLE_ENDIAN
87 # else
88 #  if defined(SUNOS)
89 #   if defined(__i386) || defined(__amd64)
90 #    define BYTE_ORDER LITTLE_ENDIAN
91 #   else
92 #    define BYTE_ORDER BIG_ENDIAN
93 #   endif
94 #  else
95 #   warning "Unable to determine the CPU endianess. Defaulting to little endian"
96 #   define BYTE_ORDER LITTLE_ENDIAN
97 #  endif
98 # endif
99 #endif
100
101
102 short ShortSwap (short l);
103 int LongSwap (int l);
104 float FloatSwap (float f);
105
106 #if BYTE_ORDER == LITTLE_ENDIAN
107 // little endian
108 #define BigShort(l) ShortSwap(l)
109 #define LittleShort(l) (l)
110 #define BigLong(l) LongSwap(l)
111 #define LittleLong(l) (l)
112 #define BigFloat(l) FloatSwap(l)
113 #define LittleFloat(l) (l)
114 #else
115 // big endian
116 #define BigShort(l) (l)
117 #define LittleShort(l) ShortSwap(l)
118 #define BigLong(l) (l)
119 #define LittleLong(l) LongSwap(l)
120 #define BigFloat(l) (l)
121 #define LittleFloat(l) FloatSwap(l)
122 #endif
123
124 unsigned int BuffBigLong (const qbyte *buffer);
125 unsigned short BuffBigShort (const qbyte *buffer);
126 unsigned int BuffLittleLong (const qbyte *buffer);
127 unsigned short BuffLittleShort (const qbyte *buffer);
128
129
130 //============================================================================
131
132 // these versions are purely for internal use, never sent in network protocol
133 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
134 typedef enum protocolversion_e
135 {
136         PROTOCOL_UNKNOWN,
137         PROTOCOL_DARKPLACES7, // added QuakeWorld-style movement protocol to allow more consistent prediction
138         PROTOCOL_DARKPLACES6, // various changes
139         PROTOCOL_DARKPLACES5, // uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
140         PROTOCOL_DARKPLACES4, // various changes
141         PROTOCOL_DARKPLACES3, // uses EntityFrame4 entity snapshot encoder/decoder which is broken, this attempted to do partial snapshot updates on a QuakeWorld-like protocol, but it is broken and impossible to fix
142         PROTOCOL_DARKPLACES2, // various changes
143         PROTOCOL_DARKPLACES1, // uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
144         PROTOCOL_QUAKEDP, // darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
145         PROTOCOL_NEHAHRAMOVIE, // Nehahra movie protocol, a big nasty hack dating back to early days of the Quake Standards Group (but only ever used by neh_gl.exe), this is potentially backwards compatible with quake protocol as long as no extended features are used (but in actuality the neh_gl.exe which wrote this protocol ALWAYS wrote the extended information)
146         PROTOCOL_QUAKE, // quake (aka netquake/normalquake/nq) protocol
147 }
148 protocolversion_t;
149
150 void MSG_WriteChar (sizebuf_t *sb, int c);
151 void MSG_WriteByte (sizebuf_t *sb, int c);
152 void MSG_WriteShort (sizebuf_t *sb, int c);
153 void MSG_WriteLong (sizebuf_t *sb, int c);
154 void MSG_WriteFloat (sizebuf_t *sb, float f);
155 void MSG_WriteString (sizebuf_t *sb, const char *s);
156 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
157 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
158 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
159 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
160 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
161 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
162 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
163 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
164 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
165
166 extern  int                     msg_readcount;
167 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
168
169 void MSG_BeginReading (void);
170 int MSG_ReadLittleShort (void);
171 int MSG_ReadBigShort (void);
172 int MSG_ReadLittleLong (void);
173 int MSG_ReadBigLong (void);
174 float MSG_ReadLittleFloat (void);
175 float MSG_ReadBigFloat (void);
176 char *MSG_ReadString (void);
177 int MSG_ReadBytes (int numbytes, unsigned char *out);
178
179 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
180 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
181 #define MSG_ReadShort MSG_ReadLittleShort
182 #define MSG_ReadLong MSG_ReadLittleLong
183 #define MSG_ReadFloat MSG_ReadLittleFloat
184
185 float MSG_ReadAngle8i (void);
186 float MSG_ReadAngle16i (void);
187 float MSG_ReadAngle32f (void);
188 float MSG_ReadCoord13i (void);
189 float MSG_ReadCoord16i (void);
190 float MSG_ReadCoord32f (void);
191 float MSG_ReadCoord (protocolversion_t protocol);
192 void MSG_ReadVector (float *v, protocolversion_t protocol);
193 float MSG_ReadAngle (protocolversion_t protocol);
194
195 //============================================================================
196
197 extern char com_token[1024];
198
199 int COM_ParseToken(const char **datapointer, int returnnewline);
200 int COM_ParseTokenConsole(const char **datapointer);
201
202 extern int com_argc;
203 extern const char **com_argv;
204
205 int COM_CheckParm (const char *parm);
206 void COM_Init (void);
207 void COM_Shutdown (void);
208 void COM_InitArgv (void);
209 void COM_InitGameType (void);
210
211 char    *va(const char *format, ...);
212 // does a varargs printf into a temp buffer
213
214
215 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
216 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
217 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
218
219 // dpsnprintf and dpvsnprintf
220 // return the number of printed characters, excluding the final '\0'
221 // or return -1 if the buffer isn't big enough to contain the entire string.
222 // buffer is ALWAYS null-terminated
223 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...);
224 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
225
226
227 //============================================================================
228
229 extern  struct cvar_s   registered;
230 extern  struct cvar_s   cmdline;
231
232 typedef enum gamemode_e
233 {
234         GAME_NORMAL,
235         GAME_HIPNOTIC,
236         GAME_ROGUE,
237         GAME_NEHAHRA,
238         GAME_NEXUIZ,
239         GAME_TRANSFUSION,
240         GAME_GOODVSBAD2,
241         GAME_TEU,
242         GAME_BATTLEMECH,
243         GAME_ZYMOTIC,
244         GAME_FNIGGIUM,
245         GAME_SETHERAL,
246         GAME_SOM,
247         GAME_TENEBRAE, // full of evil hackery
248         GAME_NEOTERIC,
249         GAME_OPENQUARTZ, //this game sucks
250         GAME_PRYDON,
251         GAME_NETHERWORLD,
252         GAME_THEHUNTED,
253 }
254 gamemode_t;
255
256 extern gamemode_t gamemode;
257 extern const char *gamename;
258 extern const char *gamedirname1;
259 extern const char *gamedirname2;
260 extern const char *gamescreenshotname;
261 extern const char *gameuserdirname;
262 extern char com_modname[MAX_OSPATH];
263
264 void COM_ToLowerString (const char *in, char *out, size_t size_out);
265 void COM_ToUpperString (const char *in, char *out, size_t size_out);
266 int COM_StringBeginsWith(const char *s, const char *match);
267
268 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
269
270 typedef struct stringlist_s
271 {
272         struct stringlist_s *next;
273         char *text;
274 } stringlist_t;
275
276 int matchpattern(char *in, char *pattern, int caseinsensitive);
277 stringlist_t *stringlistappend(stringlist_t *current, char *text);
278 void stringlistfree(stringlist_t *current);
279 stringlist_t *stringlistsort(stringlist_t *start);
280 stringlist_t *listdirectory(const char *path);
281 void freedirectory(stringlist_t *list);
282
283 char *SearchInfostring(const char *infostring, const char *key);
284
285
286 // strlcat and strlcpy, from OpenBSD
287 // Most (all?) BSDs already have them
288 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
289 # define HAVE_STRLCAT 1
290 # define HAVE_STRLCPY 1
291 #endif
292
293 #ifndef HAVE_STRLCAT
294 /*
295  * Appends src to string dst of size siz (unlike strncat, siz is the
296  * full size of dst, not space left).  At most siz-1 characters
297  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
298  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
299  * If retval >= siz, truncation occurred.
300  */
301 size_t strlcat(char *dst, const char *src, size_t siz);
302 #endif  // #ifndef HAVE_STRLCAT
303
304 #ifndef HAVE_STRLCPY
305 /*
306  * Copy src to string dst of size siz.  At most siz-1 characters
307  * will be copied.  Always NUL terminates (unless siz == 0).
308  * Returns strlen(src); if retval >= siz, truncation occurred.
309  */
310 size_t strlcpy(char *dst, const char *src, size_t siz);
311
312 #endif  // #ifndef HAVE_STRLCPY
313
314 #endif
315