1 /*
2 Copyright (C) 1996-2001 Id Software, Inc.
3 Copyright (C) 2002-2009 John Fitzgibbons and others
4 Copyright (C) 2010-2014 QuakeSpasm developers
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 
15 See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 
21 */
22 
23 #ifndef _Q_COMMON_H
24 #define _Q_COMMON_H
25 
26 // comndef.h  -- general definitions
27 
28 #if defined(_WIN32)
29 #ifdef _MSC_VER
30 #  pragma warning(disable:4244)
31 	/* 'argument'	: conversion from 'type1' to 'type2',
32 			  possible loss of data */
33 #  pragma warning(disable:4305)
34 	/* 'identifier'	: truncation from 'type1' to 'type2' */
35 	/*  in our case, truncation from 'double' to 'float' */
36 #  pragma warning(disable:4267)
37 	/* 'var'	: conversion from 'size_t' to 'type',
38 			  possible loss of data (/Wp64 warning) */
39 #endif	/* _MSC_VER */
40 #endif	/* _WIN32 */
41 
42 #undef	min
43 #undef	max
44 #define	q_min(a, b)	(((a) < (b)) ? (a) : (b))
45 #define	q_max(a, b)	(((a) > (b)) ? (a) : (b))
46 #define	CLAMP(_minval, x, _maxval)		\
47 	((x) < (_minval) ? (_minval) :		\
48 	 (x) > (_maxval) ? (_maxval) : (x))
49 
50 #define countof(x) (sizeof(x)/sizeof((x)[0]))
51 
52 typedef struct sizebuf_s
53 {
54 	qboolean	allowoverflow;	// if false, do a Sys_Error
55 	qboolean	overflowed;		// set to true if the buffer size failed
56 	byte		*data;
57 	int		maxsize;
58 	int		cursize;
59 } sizebuf_t;
60 
61 void SZ_Alloc (sizebuf_t *buf, int startsize);
62 void SZ_Free (sizebuf_t *buf);
63 void SZ_Clear (sizebuf_t *buf);
64 void *SZ_GetSpace (sizebuf_t *buf, int length);
65 void SZ_Write (sizebuf_t *buf, const void *data, int length);
66 void SZ_Print (sizebuf_t *buf, const char *data);	// strcats onto the sizebuf
67 
68 //============================================================================
69 
70 typedef struct link_s
71 {
72 	struct link_s	*prev, *next;
73 } link_t;
74 
75 
76 void ClearLink (link_t *l);
77 void RemoveLink (link_t *l);
78 void InsertLinkBefore (link_t *l, link_t *before);
79 void InsertLinkAfter (link_t *l, link_t *after);
80 
81 // (type *)STRUCT_FROM_LINK(link_t *link, type, member)
82 // ent = STRUCT_FROM_LINK(link,entity_t,order)
83 // FIXME: remove this mess!
84 #define	STRUCT_FROM_LINK(l,t,m) ((t *)((byte *)l - offsetof(t, m)))
85 
86 //============================================================================
87 
88 extern	qboolean		host_bigendian;
89 
90 extern	short	(*BigShort) (short l);
91 extern	short	(*LittleShort) (short l);
92 extern	int	(*BigLong) (int l);
93 extern	int	(*LittleLong) (int l);
94 extern	float	(*BigFloat) (float l);
95 extern	float	(*LittleFloat) (float l);
96 
97 //============================================================================
98 
99 void MSG_WriteChar (sizebuf_t *sb, int c);
100 void MSG_WriteByte (sizebuf_t *sb, int c);
101 void MSG_WriteShort (sizebuf_t *sb, int c);
102 void MSG_WriteLong (sizebuf_t *sb, int c);
103 void MSG_WriteFloat (sizebuf_t *sb, float f);
104 void MSG_WriteStringUnterminated (sizebuf_t *sb, const char *s);
105 void MSG_WriteString (sizebuf_t *sb, const char *s);
106 void MSG_WriteCoord (sizebuf_t *sb, float f, unsigned int flags);
107 void MSG_WriteAngle (sizebuf_t *sb, float f, unsigned int flags);
108 void MSG_WriteAngle16 (sizebuf_t *sb, float f, unsigned int flags); //johnfitz
109 void MSG_WriteEntity(sizebuf_t *sb, unsigned int index, unsigned int pext2); //spike
110 struct entity_state_s;
111 void MSG_WriteStaticOrBaseLine(sizebuf_t *buf, int idx, struct entity_state_s *state, unsigned int protocol_pext2, unsigned int protocol, unsigned int protocolflags); //spike
112 
113 extern	int			msg_readcount;
114 extern	qboolean	msg_badread;		// set if a read goes beyond end of message
115 
116 void MSG_BeginReading (void);
117 int MSG_ReadChar (void);
118 int MSG_ReadByte (void);
119 int MSG_ReadShort (void);
120 int MSG_ReadLong (void);
121 float MSG_ReadFloat (void);
122 const char *MSG_ReadString (void);
123 
124 float MSG_ReadCoord (unsigned int flags);
125 float MSG_ReadAngle (unsigned int flags);
126 float MSG_ReadAngle16 (unsigned int flags); //johnfitz
127 byte *MSG_ReadData (unsigned int length); // spike
128 unsigned int MSG_ReadEntity(unsigned int pext2); //spike
129 
130 void COM_Effectinfo_Enumerate(int (*cb)(const char *pname));	//spike -- for dp compat
131 
132 //============================================================================
133 
134 void Q_memset (void *dest, int fill, size_t count);
135 void Q_memcpy (void *dest, const void *src, size_t count);
136 int Q_memcmp (const void *m1, const void *m2, size_t count);
137 void Q_strcpy (char *dest, const char *src);
138 void Q_strncpy (char *dest, const char *src, int count);
139 int Q_strlen (const char *str);
140 char *Q_strrchr (const char *s, char c);
141 void Q_strcat (char *dest, const char *src);
142 int Q_strcmp (const char *s1, const char *s2);
143 int Q_strncmp (const char *s1, const char *s2, int count);
144 int	Q_atoi (const char *str);
145 float Q_atof (const char *str);
146 void Q_ftoa(char *str, float in);
147 int wildcmp(const char *wild, const char *string);
148 void Info_RemoveKey(char *info, const char *key);
149 void Info_SetKey(char *info, size_t infosize, const char *key, const char *val);
150 const char *Info_GetKey(const char *info, const char *key, char *out, size_t outsize);
151 void Info_Print(const char *info);
152 void Info_Enumerate(const char *info, void(*cb)(void *ctx, const char *key, const char *value), void *cbctx);
153 
154 
155 #include "strl_fn.h"
156 
157 /* locale-insensitive strcasecmp replacement functions: */
158 extern int q_strcasecmp (const char * s1, const char * s2);
159 extern int q_strncasecmp (const char *s1, const char *s2, size_t n);
160 
161 /* locale-insensitive case-insensitive alternative to strstr */
162 extern char *q_strcasestr(const char *haystack, const char *needle);
163 
164 /* locale-insensitive strlwr/upr replacement functions: */
165 extern char *q_strlwr (char *str);
166 extern char *q_strupr (char *str);
167 
168 /* snprintf, vsnprintf : always use our versions. */
169 extern int q_snprintf (char *str, size_t size, const char *format, ...) FUNC_PRINTF(3,4);
170 extern int q_vsnprintf(char *str, size_t size, const char *format, va_list args) FUNC_PRINTF(3,0);
171 
172 //============================================================================
173 
174 extern	char		com_token[1024];
175 extern	qboolean	com_eof;
176 
177 const char *COM_Parse (const char *data);
178 
179 
180 extern	int		com_argc;
181 extern	char	**com_argv;
182 
183 extern	int		safemode;
184 /* safe mode: in true, the engine will behave as if one
185    of these arguments were actually on the command line:
186    -nosound, -nocdaudio, -nomidi, -stdvid, -dibonly,
187    -nomouse, -nojoy, -nolan
188  */
189 
190 int COM_CheckParm (const char *parm);
191 
192 void COM_Init (void);
193 void COM_InitArgv (int argc, char **argv);
194 void COM_InitFilesystem (void);
195 
196 const char *COM_SkipPath (const char *pathname);
197 void COM_StripExtension (const char *in, char *out, size_t outsize);
198 void COM_FileBase (const char *in, char *out, size_t outsize);
199 void COM_AddExtension (char *path, const char *extension, size_t len);
200 #if 0 /* COM_DefaultExtension can be dangerous */
201 void COM_DefaultExtension (char *path, const char *extension, size_t len);
202 #endif
203 const char *COM_FileGetExtension (const char *in); /* doesn't return NULL */
204 void COM_ExtractExtension (const char *in, char *out, size_t outsize);
205 void COM_CreatePath (char *path);
206 
207 char *va (const char *format, ...) FUNC_PRINTF(1,2);
208 // does a varargs printf into a temp buffer
209 
210 unsigned COM_HashString (const char *str);
211 
212 // localization support for 2021 rerelease version:
213 void LOC_Init (void);
214 void LOC_Shutdown (void);
215 const char* LOC_GetRawString (const char *key);
216 const char* LOC_GetString (const char *key);
217 qboolean LOC_HasPlaceholders (const char *str);
218 size_t LOC_Format (const char *format, const char* (*getarg_fn)(int idx, void* userdata), void* userdata, char* out, size_t len);
219 
220 //============================================================================
221 
222 // QUAKEFS
223 typedef struct
224 {
225 	char	name[MAX_QPATH];
226 	int		filepos, filelen;
227 } packfile_t;
228 
229 typedef struct pack_s
230 {
231 	char	filename[MAX_OSPATH];
232 	int		handle;
233 	int		numfiles;
234 	packfile_t	*files;
235 } pack_t;
236 
237 typedef struct searchpath_s
238 {
239 	unsigned int path_id;	// identifier assigned to the game directory
240 					// Note that <install_dir>/game1 and
241 					// <userdir>/game1 have the same id.
242 	char	filename[MAX_OSPATH];
243 	pack_t	*pack;			// only one of filename / pack will be used
244 	struct searchpath_s	*next;
245 } searchpath_t;
246 
247 extern searchpath_t *com_searchpaths;
248 extern searchpath_t *com_base_searchpaths;
249 
250 extern int com_filesize;
251 struct cache_user_s;
252 
253 extern	char	com_basedir[MAX_OSPATH];
254 extern	char	com_gamedir[MAX_OSPATH];
255 extern	int	file_from_pak;	// global indicating that file came from a pak
256 
257 const char *COM_GetGameNames(qboolean full);
258 qboolean COM_GameDirMatches(const char *tdirs);
259 
260 void COM_WriteFile (const char *filename, const void *data, int len);
261 int COM_OpenFile (const char *filename, int *handle, unsigned int *path_id);
262 int COM_FOpenFile (const char *filename, FILE **file, unsigned int *path_id);
263 qboolean COM_FileExists (const char *filename, unsigned int *path_id);
264 void COM_CloseFile (int h);
265 
266 // these procedures open a file using COM_FindFile and loads it into a proper
267 // buffer. the buffer is allocated with a total size of com_filesize + 1. the
268 // procedures differ by their buffer allocation method.
269 byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize,
270 						unsigned int *path_id);
271 	// uses the specified stack stack buffer with the specified size
272 	// of bufsize. if bufsize is too short, uses temp hunk. the bufsize
273 	// must include the +1
274 byte *COM_LoadTempFile (const char *path, unsigned int *path_id);
275 	// allocates the buffer on the temp hunk.
276 byte *COM_LoadHunkFile (const char *path, unsigned int *path_id);
277 	// allocates the buffer on the hunk.
278 byte *COM_LoadZoneFile (const char *path, unsigned int *path_id);
279 	// allocates the buffer on the zone.
280 void COM_LoadCacheFile (const char *path, struct cache_user_s *cu,
281 						unsigned int *path_id);
282 	// uses cache mem for allocating the buffer.
283 byte *COM_LoadMallocFile (const char *path, unsigned int *path_id);
284 	// allocates the buffer on the system mem (malloc).
285 
286 // Opens the given path directly, ignoring search paths.
287 // Returns NULL on failure, or else a '\0'-terminated malloc'ed buffer.
288 // Loads in "t" mode so CRLF to LF translation is performed on Windows.
289 byte *COM_LoadMallocFile_TextMode_OSPath (const char *path, long *len_out);
290 
291 // Attempts to parse an int, followed by a newline.
292 // Returns advanced buffer position.
293 // Doesn't signal parsing failure, but this is not needed for savegame loading.
294 const char *COM_ParseIntNewline(const char *buffer, int *value);
295 
296 // Attempts to parse a float followed by a newline.
297 // Returns advanced buffer position.
298 const char *COM_ParseFloatNewline(const char *buffer, float *value);
299 
300 // Parse a string of non-whitespace into com_token, then tries to consume a
301 // newline. Returns advanced buffer position.
302 const char *COM_ParseStringNewline(const char *buffer);
303 
304 /* The following FS_*() stdio replacements are necessary if one is
305  * to perform non-sequential reads on files reopened on pak files
306  * because we need the bookkeeping about file start/end positions.
307  * Allocating and filling in the fshandle_t structure is the users'
308  * responsibility when the file is initially opened. */
309 
310 typedef struct _fshandle_t
311 {
312 	FILE *file;
313 	qboolean pak;	/* is the file read from a pak */
314 	long start;	/* file or data start position */
315 	long length;	/* file or data size */
316 	long pos;	/* current position relative to start */
317 } fshandle_t;
318 
319 size_t FS_fread(void *ptr, size_t size, size_t nmemb, fshandle_t *fh);
320 int FS_fseek(fshandle_t *fh, long offset, int whence);
321 long FS_ftell(fshandle_t *fh);
322 void FS_rewind(fshandle_t *fh);
323 int FS_feof(fshandle_t *fh);
324 int FS_ferror(fshandle_t *fh);
325 int FS_fclose(fshandle_t *fh);
326 int FS_fgetc(fshandle_t *fh);
327 char *FS_fgets(char *s, int size, fshandle_t *fh);
328 long FS_filelength (fshandle_t *fh);
329 
330 
331 extern struct cvar_s	registered;
332 extern qboolean		standard_quake, rogue, hipnotic;
333 extern qboolean		fitzmode;
334 	/* if true, run in fitzquake mode disabling custom quakespasm hacks */
335 
336 #endif	/* _Q_COMMON_H */
337 
338