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