Debian Codegraph @

code/botlib/l_struct.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: l_struct.c
25 *
26 * desc: structure reading / writing
27 *
28 * $Archive: /MissionPack/CODE/botlib/l_struct.c $
29 *
30 *****************************************************************************/
31
32 #ifdef BOTLIB
33 #include "../qcommon/q_shared.h"
34 #include "botlib.h" //for the include of be_interface.h
35 #include "l_script.h"
36 #include "l_precomp.h"
37 #include "l_struct.h"
38 #include "l_utils.h"
39 #include "be_interface.h"
40 #endif //BOTLIB
41
42 #ifdef BSPC
43 //include files for usage in the BSP Converter
44 #include "../bspc/qbsp.h"
45 #include "../bspc/l_log.h"
46 #include "../bspc/l_mem.h"
47 #include "l_precomp.h"
48 #include "l_struct.h"
49
50 #define qtrue true
51 #define qfalse false
52 #endif //BSPC
53
54 //===========================================================================
55 //
56 // Parameter: -
57 // Returns: -
58 // Changes Globals: -
59 //===========================================================================
60 fielddef_t *FindField(fielddef_t *defs, char *name)
61 {
62 int i;
63
64 for (i = 0; defs[i].name; i++)
65 {
66 if (!strcmp(defs[i].name, name)) return &defs[i];
67 } //end for
68 return NULL;
69 } //end of the function FindField
70 //===========================================================================
71 //
72 // Parameter: -
73 // Returns: -
74 // Changes Globals: -
75 //===========================================================================
76 qboolean ReadNumber(source_t *source, fielddef_t *fd, void *p)
77 {
78 token_t token;
79 int negative = qfalse;
80 long int intval, intmin = 0, intmax = 0;
81 double floatval;
82
83 if (!PC_ExpectAnyToken(source, &token)) return 0;
84
85 //check for minus sign
86 if (token.type == TT_PUNCTUATION)
87 {
88 if (fd->type & FT_UNSIGNED)
89 {
90 SourceError(source, "expected unsigned value, found %s", token.string);
91 return 0;
92 } //end if
93 //if not a minus sign
94 if (strcmp(token.string, "-"))
95 {
96 SourceError(source, "unexpected punctuation %s", token.string);
97 return 0;
98 } //end if
99 negative = qtrue;
100 //read the number
101 if (!PC_ExpectAnyToken(source, &token)) return 0;
102 } //end if
103 //check if it is a number
104 if (token.type != TT_NUMBER)
105 {
106 SourceError(source, "expected number, found %s", token.string);
107 return 0;
108 } //end if
109 //check for a float value
110 if (token.subtype & TT_FLOAT)
111 {
112 if ((fd->type & FT_TYPE) != FT_FLOAT)
113 {
114 SourceError(source, "unexpected float");
115 return 0;
116 } //end if
117 floatval = token.floatvalue;
118 if (negative) floatval = -floatval;
119 if (fd->type & FT_BOUNDED)
120 {
121 if (floatval < fd->floatmin || floatval > fd->floatmax)
122 {
123 SourceError(source, "float out of range [%f, %f]", fd->floatmin, fd->floatmax);
124 return 0;
125 } //end if
126 } //end if
127 *(float *) p = (float) floatval;
128 return 1;
129 } //end if
130 //
131 intval = token.intvalue;
132 if (negative) intval = -intval;
133 //check bounds
134 if ((fd->type & FT_TYPE) == FT_CHAR)
135 {
136 if (fd->type & FT_UNSIGNED) {intmin = 0; intmax = 255;}
137 else {intmin = -128; intmax = 127;}
138 } //end if
139 if ((fd->type & FT_TYPE) == FT_INT)
140 {
141 if (fd->type & FT_UNSIGNED) {intmin = 0; intmax = 65535;}
142 else {intmin = -32768; intmax = 32767;}
143 } //end else if
144 if ((fd->type & FT_TYPE) == FT_CHAR || (fd->type & FT_TYPE) == FT_INT)
145 {
146 if (fd->type & FT_BOUNDED)
147 {
148 intmin = Maximum(intmin, fd->floatmin);
149 intmax = Minimum(intmax, fd->floatmax);
150 } //end if
151 if (intval < intmin || intval > intmax)
152 {
153 SourceError(source, "value %ld out of range [%ld, %ld]", intval, intmin, intmax);
154 return 0;
155 } //end if
156 } //end if
157 else if ((fd->type & FT_TYPE) == FT_FLOAT)
158 {
159 if (fd->type & FT_BOUNDED)
160 {
161 if (intval < fd->floatmin || intval > fd->floatmax)
162 {
163 SourceError(source, "value %ld out of range [%f, %f]", intval, fd->floatmin, fd->floatmax);
164 return 0;
165 } //end if
166 } //end if
167 } //end else if
168 //store the value
169 if ((fd->type & FT_TYPE) == FT_CHAR)
170 {
171 if (fd->type & FT_UNSIGNED) *(unsigned char *) p = (unsigned char) intval;
172 else *(char *) p = (char) intval;
173 } //end if
174 else if ((fd->type & FT_TYPE) == FT_INT)
175 {
176 if (fd->type & FT_UNSIGNED) *(unsigned int *) p = (unsigned int) intval;
177 else *(int *) p = (int) intval;
178 } //end else
179 else if ((fd->type & FT_TYPE) == FT_FLOAT)
180 {
181 *(float *) p = (float) intval;
182 } //end else
183 return 1;
184 } //end of the function ReadNumber
185 //===========================================================================
186 //
187 // Parameter: -
188 // Returns: -
189 // Changes Globals: -
190 //===========================================================================
191 qboolean ReadChar(source_t *source, fielddef_t *fd, void *p)
192 {
193 token_t token;
194
195 if (!PC_ExpectAnyToken(source, &token)) return 0;
196
197 //take literals into account
198 if (token.type == TT_LITERAL)
199 {
200 StripSingleQuotes(token.string);
201 *(char *) p = token.string[0];
202 } //end if
203 else
204 {
205 PC_UnreadLastToken(source);
206 if (!ReadNumber(source, fd, p)) return 0;
207 } //end if
208 return 1;
209 } //end of the function ReadChar
210 //===========================================================================
211 //
212 // Parameter: -
213 // Returns: -
214 // Changes Globals: -
215 //===========================================================================
216 int ReadString(source_t *source, fielddef_t *fd, void *p)
217 {
218 token_t token;
219
220 if (!PC_ExpectTokenType(source, TT_STRING, 0, &token)) return 0;
221 //remove the double quotes
222 StripDoubleQuotes(token.string);
223 //copy the string
224 Q_strncpyz((char *) p, token.string, MAX_STRINGFIELD);
225 //
226 return 1;
227 } //end of the function ReadString
228 //===========================================================================
229 //
230 // Parameter: -
231 // Returns: -
232 // Changes Globals: -
233 //===========================================================================
234 int ReadStructure(source_t *source, structdef_t *def, char *structure)
235 {
236 token_t token;
237 fielddef_t *fd;
238 void *p;
239 int num;
240
241 if (!PC_ExpectTokenString(source, "{")) return 0;
242 while(1)
243 {
244 if (!PC_ExpectAnyToken(source, &token)) return qfalse;
245 //if end of structure
246 if (!strcmp(token.string, "}")) break;
247 //find the field with the name
248 fd = FindField(def->fields, token.string);
249 if (!fd)
250 {
251 SourceError(source, "unknown structure field %s", token.string);
252 return qfalse;
253 } //end if
254 if (fd->type & FT_ARRAY)
255 {
256 num = fd->maxarray;
257 if (!PC_ExpectTokenString(source, "{")) return qfalse;
258 } //end if
259 else
260 {
261 num = 1;
262 } //end else
263 p = (void *)(structure + fd->offset);
264 while (num-- > 0)
265 {
266 if (fd->type & FT_ARRAY)
267 {
268 if (PC_CheckTokenString(source, "}")) break;
269 } //end if
270 switch(fd->type & FT_TYPE)
271 {
272 case FT_CHAR:
273 {
274 if (!ReadChar(source, fd, p)) return qfalse;
275 p = (char *) p + sizeof(char);
276 break;
277 } //end case
278 case FT_INT:
279 {
280 if (!ReadNumber(source, fd, p)) return qfalse;
281 p = (char *) p + sizeof(int);
282 break;
283 } //end case
284 case FT_FLOAT:
285 {
286 if (!ReadNumber(source, fd, p)) return qfalse;
287 p = (char *) p + sizeof(float);
288 break;
289 } //end case
290 case FT_STRING:
291 {
292 if (!ReadString(source, fd, p)) return qfalse;
293 p = (char *) p + MAX_STRINGFIELD;
294 break;
295 } //end case
296 case FT_STRUCT:
297 {
298 if (!fd->substruct)
299 {
300 SourceError(source, "BUG: no sub structure defined");
301 return qfalse;
302 } //end if
303 ReadStructure(source, fd->substruct, (char *) p);
304 p = (char *) p + fd->substruct->size;
305 break;
306 } //end case
307 } //end switch
308 if (fd->type & FT_ARRAY)
309 {
310 if (!PC_ExpectAnyToken(source, &token)) return qfalse;
311 if (!strcmp(token.string, "}")) break;
312 if (strcmp(token.string, ","))
313 {
314 SourceError(source, "expected a comma, found %s", token.string);
315 return qfalse;
316 } //end if
317 } //end if
318 } //end while
319 } //end while
320 return qtrue;
321 } //end of the function ReadStructure
322 //===========================================================================
323 //
324 // Parameter: -
325 // Returns: -
326 // Changes Globals: -
327 //===========================================================================
328 int WriteIndent(FILE *fp, int indent)
329 {
330 while(indent-- > 0)
331 {
332 if (fprintf(fp, "\t") < 0) return qfalse;
333 } //end while
334 return qtrue;
335 } //end of the function WriteIndent
336 //===========================================================================
337 //
338 // Parameter: -
339 // Returns: -
340 // Changes Globals: -
341 //===========================================================================
342 int WriteFloat(FILE *fp, float value)
343 {
344 char buf[128];
345 int l;
346
347 Com_sprintf(buf, sizeof(buf), "%f", value);
348 l = strlen(buf);
349 //strip any trailing zeros
350 while(l-- > 1)
351 {
352 if (buf[l] != '0' && buf[l] != '.') break;
353 if (buf[l] == '.')
354 {
355 buf[l] = 0;
356 break;
357 } //end if
358 buf[l] = 0;
359 } //end while
360 //write the float to file
361 if (fprintf(fp, "%s", buf) < 0) return 0;
362 return 1;
363 } //end of the function WriteFloat
364 //===========================================================================
365 //
366 // Parameter: -
367 // Returns: -
368 // Changes Globals: -
369 //===========================================================================
370 int WriteStructWithIndent(FILE *fp, structdef_t *def, char *structure, int indent)
371 {
372 int i, num;
373 void *p;
374 fielddef_t *fd;
375
376 if (!WriteIndent(fp, indent)) return qfalse;
377 if (fprintf(fp, "{\r\n") < 0) return qfalse;
378
379 indent++;
380 for (i = 0; def->fields[i].name; i++)
381 {
382 fd = &def->fields[i];
383 if (!WriteIndent(fp, indent)) return qfalse;
384 if (fprintf(fp, "%s\t", fd->name) < 0) return qfalse;
385 p = (void *)(structure + fd->offset);
386 if (fd->type & FT_ARRAY)
387 {
388 num = fd->maxarray;
389 if (fprintf(fp, "{") < 0) return qfalse;
390 } //end if
391 else
392 {
393 num = 1;
394 } //end else
395 while(num-- > 0)
396 {
397 switch(fd->type & FT_TYPE)
398 {
399 case FT_CHAR:
400 {
401 if (fprintf(fp, "%d", *(char *) p) < 0) return qfalse;
402 p = (char *) p + sizeof(char);
403 break;
404 } //end case
405 case FT_INT:
406 {
407 if (fprintf(fp, "%d", *(int *) p) < 0) return qfalse;
408 p = (char *) p + sizeof(int);
409 break;
410 } //end case
411 case FT_FLOAT:
412 {
413 if (!WriteFloat(fp, *(float *)p)) return qfalse;
414 p = (char *) p + sizeof(float);
415 break;
416 } //end case
417 case FT_STRING:
418 {
419 if (fprintf(fp, "\"%s\"", (char *) p) < 0) return qfalse;
420 p = (char *) p + MAX_STRINGFIELD;
421 break;
422 } //end case
423 case FT_STRUCT:
424 {
425 if (!WriteStructWithIndent(fp, fd->substruct, structure, indent)) return qfalse;
426 p = (char *) p + fd->substruct->size;
427 break;
428 } //end case
429 } //end switch
430 if (fd->type & FT_ARRAY)
431 {
432 if (num > 0)
433 {
434 if (fprintf(fp, ",") < 0) return qfalse;
435 } //end if
436 else
437 {
438 if (fprintf(fp, "}") < 0) return qfalse;
439 } //end else
440 } //end if
441 } //end while
442 if (fprintf(fp, "\r\n") < 0) return qfalse;
443 } //end for
444 indent--;
445
446 if (!WriteIndent(fp, indent)) return qfalse;
447 if (fprintf(fp, "}\r\n") < 0) return qfalse;
448 return qtrue;
449 } //end of the function WriteStructWithIndent
450 //===========================================================================
451 //
452 // Parameter: -
453 // Returns: -
454 // Changes Globals: -
455 //===========================================================================
456 int WriteStructure(FILE *fp, structdef_t *def, char *structure)
457 {
458 return WriteStructWithIndent(fp, def, structure, 0);
459 } //end of the function WriteStructure
460