]> icculus.org git repositories - divverent/darkplaces.git/blob - common.h
made quakec stuffed quake1 protocol entities work alongside newer protocols, to attem...
[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 // MSVC has a different name for several standard functions
25 #ifdef WIN32
26 # define snprintf _snprintf
27 # define vsnprintf _vsnprintf
28 # define strcasecmp stricmp
29 # define strncasecmp strnicmp
30 #endif
31
32
33 //============================================================================
34
35 typedef struct sizebuf_s
36 {
37         qboolean        allowoverflow;  // if false, do a Sys_Error
38         qboolean        overflowed;             // set to true if the buffer size failed
39         qbyte           *data;
40         mempool_t       *mempool;
41         int                     maxsize;
42         int                     cursize;
43 } sizebuf_t;
44
45 void SZ_Alloc (sizebuf_t *buf, int startsize, const char *name);
46 void SZ_Free (sizebuf_t *buf);
47 void SZ_Clear (sizebuf_t *buf);
48 void *SZ_GetSpace (sizebuf_t *buf, int length);
49 void SZ_Write (sizebuf_t *buf, const void *data, int length);
50 void SZ_Print(sizebuf_t *buf, const char *data);        // strcats onto the sizebuf
51 void SZ_HexDumpToConsole(const sizebuf_t *buf);
52
53 void Com_HexDumpToConsole(const qbyte *data, int size);
54
55
56 //============================================================================
57 //                                                      Endianess handling
58 //============================================================================
59
60 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
61
62 // Initializations
63 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
64 # undef BYTE_ORDER
65 # undef LITTLE_ENDIAN
66 # undef BIG_ENDIAN
67 # define LITTLE_ENDIAN 1234
68 # define BIG_ENDIAN 4321
69 #endif
70
71 // If we still don't know the CPU endianess at this point, we try to guess
72 #ifndef BYTE_ORDER
73 # if defined(WIN32)
74 #  define BYTE_ORDER LITTLE_ENDIAN
75 # else
76 #  warning "Unable to determine the CPU endianess. Defaulting to little endian"
77 #  define BYTE_ORDER LITTLE_ENDIAN
78 # endif
79 #endif
80
81
82 short ShortSwap (short l);
83 int LongSwap (int l);
84 float FloatSwap (float f);
85
86 #if BYTE_ORDER == LITTLE_ENDIAN
87 // little endian
88 #define BigShort(l) ShortSwap(l)
89 #define LittleShort(l) (l)
90 #define BigLong(l) LongSwap(l)
91 #define LittleLong(l) (l)
92 #define BigFloat(l) FloatSwap(l)
93 #define LittleFloat(l) (l)
94 #else
95 // big endian
96 #define BigShort(l) (l)
97 #define LittleShort(l) ShortSwap(l)
98 #define BigLong(l) (l)
99 #define LittleLong(l) LongSwap(l)
100 #define BigFloat(l) (l)
101 #define LittleFloat(l) FloatSwap(l)
102 #endif
103
104 unsigned int BuffBigLong (const qbyte *buffer);
105 unsigned short BuffBigShort (const qbyte *buffer);
106 unsigned int BuffLittleLong (const qbyte *buffer);
107 unsigned short BuffLittleShort (const qbyte *buffer);
108
109
110 //============================================================================
111
112 void MSG_WriteChar (sizebuf_t *sb, int c);
113 void MSG_WriteByte (sizebuf_t *sb, int c);
114 void MSG_WriteShort (sizebuf_t *sb, int c);
115 void MSG_WriteLong (sizebuf_t *sb, int c);
116 void MSG_WriteFloat (sizebuf_t *sb, float f);
117 void MSG_WriteString (sizebuf_t *sb, const char *s);
118 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
119 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
120 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
121 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
122 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
123 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
124 void MSG_WriteCoord (sizebuf_t *sb, float f, int protocol);
125 void MSG_WriteVector (sizebuf_t *sb, float *v, int protocol);
126 void MSG_WriteAngle (sizebuf_t *sb, float f, int protocol);
127
128 extern  int                     msg_readcount;
129 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
130
131 void MSG_BeginReading (void);
132 int MSG_ReadLittleShort (void);
133 int MSG_ReadBigShort (void);
134 int MSG_ReadLittleLong (void);
135 int MSG_ReadBigLong (void);
136 float MSG_ReadLittleFloat (void);
137 float MSG_ReadBigFloat (void);
138 char *MSG_ReadString (void);
139 int MSG_ReadBytes (int numbytes, unsigned char *out);
140
141 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
142 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
143 #define MSG_ReadShort MSG_ReadLittleShort
144 #define MSG_ReadLong MSG_ReadLittleLong
145 #define MSG_ReadFloat MSG_ReadLittleFloat
146
147 float MSG_ReadAngle8i (void);
148 float MSG_ReadAngle16i (void);
149 float MSG_ReadAngle32f (void);
150 float MSG_ReadCoord13i (void);
151 float MSG_ReadCoord16i (void);
152 float MSG_ReadCoord32f (void);
153 float MSG_ReadCoord (int protocol);
154 void MSG_ReadVector (float *v, int protocol);
155 float MSG_ReadAngle (int protocol);
156
157 //============================================================================
158
159 extern char com_token[1024];
160
161 int COM_ParseToken(const char **datapointer, int returnnewline);
162 int COM_ParseTokenConsole(const char **datapointer);
163
164 extern int com_argc;
165 extern const char **com_argv;
166
167 int COM_CheckParm (const char *parm);
168 void COM_Init (void);
169 void COM_InitArgv (void);
170 void COM_InitGameType (void);
171
172 char    *va(const char *format, ...);
173 // does a varargs printf into a temp buffer
174
175
176 //============================================================================
177
178 extern  struct cvar_s   registered;
179 extern  struct cvar_s   cmdline;
180
181 #define GAME_NORMAL 0
182 #define GAME_HIPNOTIC 1
183 #define GAME_ROGUE 2
184 #define GAME_NEHAHRA 3
185 #define GAME_NEXUIZ 4
186 #define GAME_TRANSFUSION 5
187 #define GAME_GOODVSBAD2 6
188 #define GAME_TEU 7
189 #define GAME_BATTLEMECH 8
190 #define GAME_ZYMOTIC 9
191 #define GAME_FNIGGIUM 10
192 #define GAME_SETHERAL 11
193 #define GAME_SOM 12
194 #define GAME_TENEBRAE 13 // full of evil hackery
195 #define GAME_NEOTERIC 14
196 #define GAME_OPENQUARTZ 15 //this game sucks
197 #define GAME_PRYDON 16
198 #define GAME_NETHERWORLD 17
199
200 extern int gamemode;
201 extern const char *gamename;
202 extern const char *gamedirname;
203 extern const char *gamescreenshotname;
204 extern char com_modname[MAX_OSPATH];
205
206 void COM_ToLowerString (const char *in, char *out, size_t size_out);
207 void COM_ToUpperString (const char *in, char *out, size_t size_out);
208 int COM_StringBeginsWith(const char *s, const char *match);
209
210 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
211
212 typedef struct stringlist_s
213 {
214         struct stringlist_s *next;
215         char *text;
216 } stringlist_t;
217
218 int matchpattern(char *in, char *pattern, int caseinsensitive);
219 stringlist_t *stringlistappend(stringlist_t *current, char *text);
220 void stringlistfree(stringlist_t *current);
221 stringlist_t *stringlistsort(stringlist_t *start);
222 stringlist_t *listdirectory(char *path);
223 void freedirectory(stringlist_t *list);
224
225 char *SearchInfostring(const char *infostring, const char *key);
226
227
228 // strlcat and strlcpy, from OpenBSD
229 // Most (all?) BSDs already have them
230 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || (defined(__APPLE__) && defined(__MACH__))
231 # define HAVE_STRLCAT 1
232 # define HAVE_STRLCPY 1
233 #endif
234
235 #ifndef HAVE_STRLCAT
236 /*
237  * Appends src to string dst of size siz (unlike strncat, siz is the
238  * full size of dst, not space left).  At most siz-1 characters
239  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
240  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
241  * If retval >= siz, truncation occurred.
242  */
243 size_t strlcat(char *dst, const char *src, size_t siz);
244 #endif  // #ifndef HAVE_STRLCAT
245
246 #ifndef HAVE_STRLCPY
247 /*
248  * Copy src to string dst of size siz.  At most siz-1 characters
249  * will be copied.  Always NUL terminates (unless siz == 0).
250  * Returns strlen(src); if retval >= siz, truncation occurred.
251  */
252 size_t strlcpy(char *dst, const char *src, size_t siz);
253
254 #endif  // #ifndef HAVE_STRLCPY
255
256 #endif
257