Debian Codegraph @

tools/quake2/extra/qe4/cmdlib.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools 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 2 Tools 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 2 Tools 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 // cmdlib.c
24
25 #include "cmdlib.h"
26 #include "globaldefs.h"
27
28 #define PATHSEPERATOR '/'
29
30 char com_token[1024];
31 qboolean com_eof;
32
33 /*
34 ================
35 I_FloatTime
36 ================
37 */
38 double I_FloatTime (void)
39 {
40 time_t t;
41
42 time (&t);
43
44 return t;
45 #if 0
46 // more precise, less portable
47 struct timeval tp;
48 struct timezone tzp;
49 static int secbase;
50
51 gettimeofday(&tp, &tzp);
52
53 if (!secbase)
54 {
55 secbase = tp.tv_sec;
56 return tp.tv_usec/1000000.0;
57 }
58
59 return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
60 #endif
61 }
62
63
64 /*
65 ==============
66 COM_Parse
67
68 Parse a token out of a string
69 ==============
70 */
71 char *COM_Parse (char *data)
72 {
73 int c;
74 int len;
75
76 len = 0;
77 com_token[0] = 0;
78
79 if (!data)
80 return NULL;
81
82 // skip whitespace
83 skipwhite:
84 while ( (c = *data) <= ' ')
85 {
86 if (c == 0)
87 {
88 com_eof = true;
89 return NULL; // end of file;
90 }
91 data++;
92 }
93
94 // skip // comments
95 if (c=='/' && data[1] == '/')
96 {
97 while (*data && *data != '\n')
98 data++;
99 goto skipwhite;
100 }
101
102
103 // handle quoted strings specially
104 if (c == '\"')
105 {
106 data++;
107 do
108 {
109 c = *data++;
110 if (c=='\"')
111 {
112 com_token[len] = 0;
113 return data;
114 }
115 com_token[len] = c;
116 len++;
117 } while (1);
118 }
119
120 // parse single characters
121 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
122 {
123 com_token[len] = c;
124 len++;
125 com_token[len] = 0;
126 return data+1;
127 }
128
129 // parse a regular word
130 do
131 {
132 com_token[len] = c;
133 data++;
134 len++;
135 c = *data;
136 if (c=='{' || c=='}'|| c==')'|| c=='(' || c=='\'' || c==':')
137 break;
138 } while (c>32);
139
140 com_token[len] = 0;
141 return data;
142 }
143
144
145 int Q_strncasecmp (char *s1, char *s2, int n)
146 {
147 int c1, c2;
148
149 while (1)
150 {
151 c1 = *s1++;
152 c2 = *s2++;
153
154 if (!n--)
155 return 0; // strings are equal until end point
156
157 if (c1 != c2)
158 {
159 if (c1 >= 'a' && c1 <= 'z')
160 c1 -= ('a' - 'A');
161 if (c2 >= 'a' && c2 <= 'z')
162 c2 -= ('a' - 'A');
163 if (c1 != c2)
164 return -1; // strings not equal
165 }
166 if (!c1)
167 return 0; // strings are equal
168 }
169
170 return -1;
171 }
172
173 int Q_strcasecmp (char *s1, char *s2)
174 {
175 return Q_strncasecmp (s1, s2, 99999);
176 }
177
178
179
180 /*
181 =============================================================================
182
183 MISC FUNCTIONS
184
185 =============================================================================
186 */
187
188
189 int argc;
190 char *argv[MAX_NUM_ARGVS];
191
192 /*
193 ============
194 ParseCommandLine
195 ============
196 */
197 void ParseCommandLine (char *lpCmdLine)
198 {
199 argc = 1;
200 argv[0] = "programname";
201
202 while (*lpCmdLine && (argc < MAX_NUM_ARGVS))
203 {
204 while (*lpCmdLine && ((*lpCmdLine <= 32) || (*lpCmdLine > 126)))
205 lpCmdLine++;
206
207 if (*lpCmdLine)
208 {
209 argv[argc] = lpCmdLine;
210 argc++;
211
212 while (*lpCmdLine && ((*lpCmdLine > 32) && (*lpCmdLine <= 126)))
213 lpCmdLine++;
214
215 if (*lpCmdLine)
216 {
217 *lpCmdLine = 0;
218 lpCmdLine++;
219 }
220
221 }
222 }
223 }
224
225
226
227 /*
228 =================
229 CheckParm
230
231 Checks for the given parameter in the program's command line arguments
232 Returns the argument number (1 to argc-1) or 0 if not present
233 =================
234 */
235 int CheckParm (char *check)
236 {
237 int i;
238
239 for (i = 1;i<argc;i++)
240 {
241 if ( !Q_strcasecmp(check, argv[i]) )
242 return i;
243 }
244
245 return 0;
246 }
247
248
249
250 /*
251 ================
252 Q_filelength
253 ================
254 */
255 int Q_filelength (FILE *f)
256 {
257 int pos;
258 int end;
259
260 pos = ftell (f);
261 fseek (f, 0, SEEK_END);
262 end = ftell (f);
263 fseek (f, pos, SEEK_SET);
264
265 return end;
266 }
267
268
269 FILE *SafeOpenWrite (char *filename)
270 {
271 FILE *f;
272
273 f = fopen(filename, "wb");
274
275 if (!f)
276 Error ("Error opening %s: %s",filename,strerror(errno));
277
278 return f;
279 }
280
281 FILE *SafeOpenRead (char *filename)
282 {
283 FILE *f;
284
285 f = fopen(filename, "rb");
286
287 if (!f)
288 Error ("Error opening %s: %s",filename,strerror(errno));
289
290 return f;
291 }
292
293
294 void SafeRead (FILE *f, void *buffer, int count)
295 {
296 if ( (int)fread (buffer, 1, count, f) != count)
297 Error ("File read failure");
298 }
299
300
301 void SafeWrite (FILE *f, void *buffer, int count)
302 {
303 if ( (int)fwrite (buffer, 1, count, f) != count)
304 Error ("File read failure");
305 }
306
307
308
309 /*
310 ==============
311 LoadFile
312 ==============
313 */
314 int LoadFile (char *filename, void **bufferptr)
315 {
316 FILE *f;
317 int length;
318 void *buffer;
319 extern void *qmalloc( size_t size );
320
321 f = fopen (filename, "rb");
322 if (!f)
323 {
324 *bufferptr = NULL;
325 return -1;
326 }
327 length = Q_filelength (f);
328 buffer = qmalloc (length+1);
329 ((char *)buffer)[length] = 0;
330 SafeRead (f, buffer, length);
331 fclose (f);
332
333 *bufferptr = buffer;
334 return length;
335 }
336
337
338 /*
339 ==============
340 LoadFileNoCrash
341
342 returns -1 length if not present
343 ==============
344 */
345 int LoadFileNoCrash (char *filename, void **bufferptr)
346 {
347 FILE *f;
348 int length;
349 void *buffer;
350
351 f = fopen (filename, "rb");
352 if (!f)
353 return -1;
354 length = Q_filelength (f);
355 buffer = qmalloc (length+1);
356 ((char *)buffer)[length] = 0;
357 SafeRead (f, buffer, length);
358 fclose (f);
359
360 *bufferptr = buffer;
361 return length;
362 }
363
364
365 /*
366 ==============
367 SaveFile
368 ==============
369 */
370 void SaveFile (char *filename, void *buffer, int count)
371 {
372 FILE *f;
373
374 f = SafeOpenWrite (filename);
375 SafeWrite (f, buffer, count);
376 fclose (f);
377 }
378
379
380
381 void DefaultExtension (char *path, char *extension)
382 {
383 char *src;
384 //
385 // if path doesn't have a .EXT, append extension
386 // (extension should include the .)
387 //
388 src = path + strlen(path) - 1;
389
390 while (*src != PATHSEPERATOR && src != path)
391 {
392 if (*src == '.')
393 return; // it has an extension
394 src--;
395 }
396
397 strcat (path, extension);
398 }
399
400
401 void DefaultPath (char *path, char *basepath)
402 {
403 char temp[128];
404
405 if (path[0] == PATHSEPERATOR)
406 return; // absolute path location
407 strcpy (temp,path);
408 strcpy (path,basepath);
409 strcat (path,temp);
410 }
411
412
413 void StripFilename (char *path)
414 {
415 int length;
416
417 length = strlen(path)-1;
418 while (length > 0 && path[length] != PATHSEPERATOR)
419 length--;
420 path[length] = 0;
421 }
422
423 void StripExtension (char *path)
424 {
425 int length;
426
427 length = strlen(path)-1;
428 while (length > 0 && path[length] != '.')
429 {
430 length--;
431 if (path[length] == '/')
432 return; // no extension
433 }
434 if (length)
435 path[length] = 0;
436 }
437
438
439 /*
440 ====================
441 Extract file parts
442 ====================
443 */
444 void ExtractFilePath (char *path, char *dest)
445 {
446 char *src;
447
448 src = path + strlen(path) - 1;
449
450 //
451 // back up until a \ or the start
452 //
453 while (src != path && *(src-1) != PATHSEPERATOR)
454 src--;
455
456 memcpy (dest, path, src-path);
457 dest[src-path] = 0;
458 }
459
460 void ExtractFileName (char *path, char *dest)
461 {
462 char *src;
463
464 src = path + strlen(path) - 1;
465
466 //
467 // back up until a \ or the start
468 //
469 while (src != path && *(src-1) != '/'
470 && *(src-1) != '\\' )
471 src--;
472
473 while (*src)
474 {
475 *dest++ = *src++;
476 }
477 *dest = 0;
478 }
479
480 void ExtractFileBase (char *path, char *dest)
481 {
482 char *src;
483
484 src = path + strlen(path) - 1;
485
486 //
487 // back up until a \ or the start
488 //
489 while (src != path && *(src-1) != '/'
490 && *(src-1) != '\\' )
491 src--;
492
493 while (*src && *src != '.')
494 {
495 *dest++ = *src++;
496 }
497 *dest = 0;
498 }
499
500 void ExtractFileExtension (char *path, char *dest)
501 {
502 char *src;
503
504 src = path + strlen(path) - 1;
505
506 //
507 // back up until a . or the start
508 //
509 while (src != path && *(src-1) != '.')
510 src--;
511 if (src == path)
512 {
513 *dest = 0; // no extension
514 return;
515 }
516
517 strcpy (dest,src);
518 }
519
520
521 /*
522 ==============
523 ParseNum / ParseHex
524 ==============
525 */
526 int ParseHex (char *hex)
527 {
528 char *str;
529 int num;
530
531 num = 0;
532 str = hex;
533
534 while (*str)
535 {
536 num <<= 4;
537 if (*str >= '0' && *str <= '9')
538 num += *str-'0';
539 else if (*str >= 'a' && *str <= 'f')
540 num += 10 + *str-'a';
541 else if (*str >= 'A' && *str <= 'F')
542 num += 10 + *str-'A';
543 else
544 Error ("Bad hex number: %s",hex);
545 str++;
546 }
547
548 return num;
549 }
550
551
552 int ParseNum (char *str)
553 {
554 if (str[0] == '$')
555 return ParseHex (str+1);
556 if (str[0] == '0' && str[1] == 'x')
557 return ParseHex (str+2);
558 return atol (str);
559 }
560
561
562
563 /*
564 ============================================================================
565
566 BYTE ORDER FUNCTIONS
567
568 ============================================================================
569 */
570
571 #if GDEF_ARCH_ENDIAN_BIG
572
573 short LittleShort (short l)
574 {
575 byte b1,b2;
576
577 b1 = l&255;
578 b2 = (l>>8)&255;
579
580 return (b1<<8) + b2;
581 }
582
583 short BigShort (short l)
584 {
585 return l;
586 }
587
588
589 int LittleLong (int l)
590 {
591 byte b1,b2,b3,b4;
592
593 b1 = l&255;
594 b2 = (l>>8)&255;
595 b3 = (l>>16)&255;
596 b4 = (l>>24)&255;
597
598 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
599 }
600
601 int BigLong (int l)
602 {
603 return l;
604 }
605
606
607 float LittleFloat (float l)
608 {
609 union {byte b[4]; float f;} in, out;
610
611 in.f = l;
612 out.b[0] = in.b[3];
613 out.b[1] = in.b[2];
614 out.b[2] = in.b[1];
615 out.b[3] = in.b[0];
616
617 return out.f;
618 }
619
620 float BigFloat (float l)
621 {
622 return l;
623 }
624
625
626 #else
627
628
629 short BigShort (short l)
630 {
631 byte b1,b2;
632
633 b1 = l&255;
634 b2 = (l>>8)&255;
635
636 return (b1<<8) + b2;
637 }
638
639 short LittleShort (short l)
640 {
641 return l;
642 }
643
644
645 int BigLong (int l)
646 {
647 byte b1,b2,b3,b4;
648
649 b1 = l&255;
650 b2 = (l>>8)&255;
651 b3 = (l>>16)&255;
652 b4 = (l>>24)&255;
653
654 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
655 }
656
657 int LittleLong (int l)
658 {
659 return l;
660 }
661
662 float BigFloat (float l)
663 {
664 union {byte b[4]; float f;} in, out;
665
666 in.f = l;
667 out.b[0] = in.b[3];
668 out.b[1] = in.b[2];
669 out.b[2] = in.b[1];
670 out.b[3] = in.b[0];
671
672 return out.f;
673 }
674
675 float LittleFloat (float l)
676 {
677 return l;
678 }
679
680
681
682 #endif
683