Debian Codegraph @

code/qcommon/q_shared.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 // q_shared.c -- stateless support routines that are included in each code dll
24 #include "q_shared.h"
25
26 // ^[0-9a-zA-Z]
27 qboolean Q_IsColorString(const char *p) {
28 if (!p)
29 return qfalse;
30
31 if (p[0] != Q_COLOR_ESCAPE)
32 return qfalse;
33
34 if (p[1] == 0)
35 return qfalse;
36
37 // isalnum expects a signed integer in the range -1 (EOF) to 255, or it might assert on undefined behaviour
38 // a dereferenced char pointer has the range -128 to 127, so we just need to rangecheck the negative part
39 if (p[1] < 0)
40 return qfalse;
41
42 if (isalnum(p[1]) == 0)
43 return qfalse;
44
45 return qtrue;
46 }
47
48 float Com_Clamp( float min, float max, float value ) {
49 if ( value < min ) {
50 return min;
51 }
52 if ( value > max ) {
53 return max;
54 }
55 return value;
56 }
57
58
59 /*
60 ============
61 COM_SkipPath
62 ============
63 */
64 char *COM_SkipPath (char *pathname)
65 {
66 char *last;
67
68 last = pathname;
69 while (*pathname)
70 {
71 if (*pathname=='/')
72 last = pathname+1;
73 pathname++;
74 }
75 return last;
76 }
77
78 /*
79 ============
80 COM_GetExtension
81 ============
82 */
83 const char *COM_GetExtension( const char *name )
84 {
85 const char *dot = strrchr(name, '.'), *slash;
86 if (dot && (!(slash = strrchr(name, '/')) || slash < dot))
87 return dot + 1;
88 else
89 return "";
90 }
91
92
93 /*
94 ============
95 COM_StripExtension
96 ============
97 */
98 void COM_StripExtension( const char *in, char *out, int destsize )
99 {
100 const char *dot = strrchr(in, '.'), *slash;
101
102 if (dot && (!(slash = strrchr(in, '/')) || slash < dot))
103 destsize = (destsize < dot-in+1 ? destsize : dot-in+1);
104
105 if ( in == out && destsize > 1 )
106 out[destsize-1] = '\0';
107 else
108 Q_strncpyz(out, in, destsize);
109 }
110
111 /*
112 ============
113 COM_CompareExtension
114
115 string compare the end of the strings and return qtrue if strings match
116 ============
117 */
118 qboolean COM_CompareExtension(const char *in, const char *ext)
119 {
120 int inlen, extlen;
121
122 inlen = strlen(in);
123 extlen = strlen(ext);
124
125 if(extlen <= inlen)
126 {
127 in += inlen - extlen;
128
129 if(!Q_stricmp(in, ext))
130 return qtrue;
131 }
132
133 return qfalse;
134 }
135
136 /*
137 ==================
138 COM_DefaultExtension
139
140 if path doesn't have an extension, then append
141 the specified one (which should include the .)
142 ==================
143 */
144 void COM_DefaultExtension( char *path, int maxSize, const char *extension )
145 {
146 const char *dot = strrchr(path, '.'), *slash;
147 if (dot && (!(slash = strrchr(path, '/')) || slash < dot))
148 return;
149 else
150 Q_strcat(path, maxSize, extension);
151 }
152
153 /*
154 ============================================================================
155
156 BYTE ORDER FUNCTIONS
157
158 ============================================================================
159 */
160 /*
161 // can't just use function pointers, or dll linkage can
162 // mess up when qcommon is included in multiple places
163 static short (*_BigShort) (short l);
164 static short (*_LittleShort) (short l);
165 static int (*_BigLong) (int l);
166 static int (*_LittleLong) (int l);
167 static qint64 (*_BigLong64) (qint64 l);
168 static qint64 (*_LittleLong64) (qint64 l);
169 static float (*_BigFloat) (const float *l);
170 static float (*_LittleFloat) (const float *l);
171
172 short BigShort(short l){return _BigShort(l);}
173 short LittleShort(short l) {return _LittleShort(l);}
174 int BigLong (int l) {return _BigLong(l);}
175 int LittleLong (int l) {return _LittleLong(l);}
176 qint64 BigLong64 (qint64 l) {return _BigLong64(l);}
177 qint64 LittleLong64 (qint64 l) {return _LittleLong64(l);}
178 float BigFloat (const float *l) {return _BigFloat(l);}
179 float LittleFloat (const float *l) {return _LittleFloat(l);}
180 */
181
182 void CopyShortSwap(void *dest, void *src)
183 {
184 byte *to = dest, *from = src;
185
186 to[0] = from[1];
187 to[1] = from[0];
188 }
189
190 void CopyLongSwap(void *dest, void *src)
191 {
192 byte *to = dest, *from = src;
193
194 to[0] = from[3];
195 to[1] = from[2];
196 to[2] = from[1];
197 to[3] = from[0];
198 }
199
200 short ShortSwap (short l)
201 {
202 byte b1,b2;
203
204 b1 = l&255;
205 b2 = (l>>8)&255;
206
207 return (b1<<8) + b2;
208 }
209
210 short ShortNoSwap (short l)
211 {
212 return l;
213 }
214
215 int LongSwap (int l)
216 {
217 byte b1,b2,b3,b4;
218
219 b1 = l&255;
220 b2 = (l>>8)&255;
221 b3 = (l>>16)&255;
222 b4 = (l>>24)&255;
223
224 return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
225 }
226
227 int LongNoSwap (int l)
228 {
229 return l;
230 }
231
232 qint64 Long64Swap (qint64 ll)
233 {
234 qint64 result;
235
236 result.b0 = ll.b7;
237 result.b1 = ll.b6;
238 result.b2 = ll.b5;
239 result.b3 = ll.b4;
240 result.b4 = ll.b3;
241 result.b5 = ll.b2;
242 result.b6 = ll.b1;
243 result.b7 = ll.b0;
244
245 return result;
246 }
247
248 qint64 Long64NoSwap (qint64 ll)
249 {
250 return ll;
251 }
252
253 float FloatSwap (const float *f) {
254 floatint_t out;
255
256 out.f = *f;
257 out.ui = LongSwap(out.ui);
258
259 return out.f;
260 }
261
262 float FloatNoSwap (const float *f)
263 {
264 return *f;
265 }
266
267 /*
268 ================
269 Swap_Init
270 ================
271 */
272 /*
273 void Swap_Init (void)
274 {
275 byte swaptest[2] = {1,0};
276
277 // set the byte swapping variables in a portable manner
278 if ( *(short *)swaptest == 1)
279 {
280 _BigShort = ShortSwap;
281 _LittleShort = ShortNoSwap;
282 _BigLong = LongSwap;
283 _LittleLong = LongNoSwap;
284 _BigLong64 = Long64Swap;
285 _LittleLong64 = Long64NoSwap;
286 _BigFloat = FloatSwap;
287 _LittleFloat = FloatNoSwap;
288 }
289 else
290 {
291 _BigShort = ShortNoSwap;
292 _LittleShort = ShortSwap;
293 _BigLong = LongNoSwap;
294 _LittleLong = LongSwap;
295 _BigLong64 = Long64NoSwap;
296 _LittleLong64 = Long64Swap;
297 _BigFloat = FloatNoSwap;
298 _LittleFloat = FloatSwap;
299 }
300
301 }
302 */
303
304 /*
305 ============================================================================
306
307 PARSING
308
309 ============================================================================
310 */
311
312 static char com_token[MAX_TOKEN_CHARS];
313 static char com_parsename[MAX_TOKEN_CHARS];
314 static int com_lines;
315 static int com_tokenline;
316
317 void COM_BeginParseSession( const char *name )
318 {
319 com_lines = 1;
320 com_tokenline = 0;
321 Com_sprintf(com_parsename, sizeof(com_parsename), "%s", name);
322 }
323
324 int COM_GetCurrentParseLine( void )
325 {
326 if ( com_tokenline )
327 {
328 return com_tokenline;
329 }
330
331 return com_lines;
332 }
333
334 char *COM_Parse( char **data_p )
335 {
336 return COM_ParseExt( data_p, qtrue );
337 }
338
339 void COM_ParseError( char *format, ... )
340 {
341 va_list argptr;
342 static char string[4096];
343
344 va_start (argptr, format);
345 Q_vsnprintf (string, sizeof(string), format, argptr);
346 va_end (argptr);
347
348 Com_Printf("ERROR: %s, line %d: %s\n", com_parsename, COM_GetCurrentParseLine(), string);
349 }
350
351 void COM_ParseWarning( char *format, ... )
352 {
353 va_list argptr;
354 static char string[4096];
355
356 va_start (argptr, format);
357 Q_vsnprintf (string, sizeof(string), format, argptr);
358 va_end (argptr);
359
360 Com_Printf("WARNING: %s, line %d: %s\n", com_parsename, COM_GetCurrentParseLine(), string);
361 }
362
363 /*
364 ==============
365 COM_Parse
366
367 Parse a token out of a string
368 Will never return NULL, just empty strings
369
370 If "allowLineBreaks" is qtrue then an empty
371 string will be returned if the next token is
372 a newline.
373 ==============
374 */
375 static char *SkipWhitespace( char *data, qboolean *hasNewLines ) {
376 int c;
377
378 while( (c = *data) <= ' ') {
379 if( !c ) {
380 return NULL;
381 }
382 if( c == '\n' ) {
383 com_lines++;
384 *hasNewLines = qtrue;
385 }
386 data++;
387 }
388
389 return data;
390 }
391
392 int COM_Compress( char *data_p ) {
393 char *in, *out;
394 int c;
395 qboolean newline = qfalse, whitespace = qfalse;
396
397 in = out = data_p;
398 if (in) {
399 while ((c = *in) != 0) {
400 // skip double slash comments
401 if ( c == '/' && in[1] == '/' ) {
402 while (*in && *in != '\n') {
403 in++;
404 }
405 // skip /* */ comments
406 } else if ( c == '/' && in[1] == '*' ) {
407 while ( *in && ( *in != '*' || in[1] != '/' ) )
408 in++;
409 if ( *in )
410 in += 2;
411 // record when we hit a newline
412 } else if ( c == '\n' || c == '\r' ) {
413 newline = qtrue;
414 in++;
415 // record when we hit whitespace
416 } else if ( c == ' ' || c == '\t') {
417 whitespace = qtrue;
418 in++;
419 // an actual token
420 } else {
421 // if we have a pending newline, emit it (and it counts as whitespace)
422 if (newline) {
423 *out++ = '\n';
424 newline = qfalse;
425 whitespace = qfalse;
426 } if (whitespace) {
427 *out++ = ' ';
428 whitespace = qfalse;
429 }
430
431 // copy quoted strings unmolested
432 if (c == '"') {
433 *out++ = c;
434 in++;
435 while (1) {
436 c = *in;
437 if (c && c != '"') {
438 *out++ = c;
439 in++;
440 } else {
441 break;
442 }
443 }
444 if (c == '"') {
445 *out++ = c;
446 in++;
447 }
448 } else {
449 *out = c;
450 out++;
451 in++;
452 }
453 }
454 }
455
456 *out = 0;
457 }
458 return out - data_p;
459 }
460
461 char *COM_ParseExt( char **data_p, qboolean allowLineBreaks )
462 {
463 int c = 0, len;
464 qboolean hasNewLines = qfalse;
465 char *data;
466
467 data = *data_p;
468 len = 0;
469 com_token[0] = 0;
470 com_tokenline = 0;
471
472 // make sure incoming data is valid
473 if ( !data )
474 {
475 *data_p = NULL;
476 return com_token;
477 }
478
479 while ( 1 )
480 {
481 // skip whitespace
482 data = SkipWhitespace( data, &hasNewLines );
483 if ( !data )
484 {
485 *data_p = NULL;
486 return com_token;
487 }
488 if ( hasNewLines && !allowLineBreaks )
489 {
490 *data_p = data;
491 return com_token;
492 }
493
494 c = *data;
495
496 // skip double slash comments
497 if ( c == '/' && data[1] == '/' )
498 {
499 data += 2;
500 while (*data && *data != '\n') {
501 data++;
502 }
503 }
504 // skip /* */ comments
505 else if ( c=='/' && data[1] == '*' )
506 {
507 data += 2;
508 while ( *data && ( *data != '*' || data[1] != '/' ) )
509 {
510 if ( *data == '\n' )
511 {
512 com_lines++;
513 }
514 data++;
515 }
516 if ( *data )
517 {
518 data += 2;
519 }
520 }
521 else
522 {
523 break;
524 }
525 }
526
527 // token starts on this line
528 com_tokenline = com_lines;
529
530 // handle quoted strings
531 if (c == '\"')
532 {
533 data++;
534 while (1)
535 {
536 c = *data++;
537 if (c=='\"' || !c)
538 {
539 com_token[len] = 0;
540 *data_p = ( char * ) data;
541 return com_token;
542 }
543 if ( c == '\n' )
544 {
545 com_lines++;
546 }
547 if (len < MAX_TOKEN_CHARS - 1)
548 {
549 com_token[len] = c;
550 len++;
551 }
552 }
553 }
554
555 // parse a regular word
556 do
557 {
558 if (len < MAX_TOKEN_CHARS - 1)
559 {
560 com_token[len] = c;
561 len++;
562 }
563 data++;
564 c = *data;
565 } while (c>32);
566
567 com_token[len] = 0;
568
569 *data_p = ( char * ) data;
570 return com_token;
571 }
572
573 /*
574 ==================
575 COM_MatchToken
576 ==================
577 */
578 void COM_MatchToken( char **buf_p, char *match ) {
579 char *token;
580
581 token = COM_Parse( buf_p );
582 if ( strcmp( token, match ) ) {
583 Com_Error( ERR_DROP, "MatchToken: %s != %s", token, match );
584 }
585 }
586
587
588 /*
589 =================
590 SkipBracedSection
591
592 The next token should be an open brace or set depth to 1 if already parsed it.
593 Skips until a matching close brace is found.
594 Internal brace depths are properly skipped.
595 =================
596 */
597 qboolean SkipBracedSection (char **program, int depth) {
598 char *token;
599
600 do {
601 token = COM_ParseExt( program, qtrue );
602 if( token[1] == 0 ) {
603 if( token[0] == '{' ) {
604 depth++;
605 }
606 else if( token[0] == '}' ) {
607 depth--;
608 }
609 }
610 } while( depth && *program );
611
612 return ( depth == 0 );
613 }
614
615 /*
616 =================
617 SkipRestOfLine
618 =================
619 */
620 void SkipRestOfLine ( char **data ) {
621 char *p;
622 int c;
623
624 p = *data;
625
626 if ( !*p )
627 return;
628
629 while ( (c = *p++) != 0 ) {
630 if ( c == '\n' ) {
631 com_lines++;
632 break;
633 }
634 }
635
636 *data = p;
637 }
638
639
640 void Parse1DMatrix (char **buf_p, int x, float *m) {
641 char *token;
642 int i;
643
644 COM_MatchToken( buf_p, "(" );
645
646 for (i = 0 ; i < x ; i++) {
647 token = COM_Parse(buf_p);
648 m[i] = atof(token);
649 }
650
651 COM_MatchToken( buf_p, ")" );
652 }
653
654 void Parse2DMatrix (char **buf_p, int y, int x, float *m) {
655 int i;
656
657 COM_MatchToken( buf_p, "(" );
658
659 for (i = 0 ; i < y ; i++) {
660 Parse1DMatrix (buf_p, x, m + i * x);
661 }
662
663 COM_MatchToken( buf_p, ")" );
664 }
665
666 void Parse3DMatrix (char **buf_p, int z, int y, int x, float *m) {
667 int i;
668
669 COM_MatchToken( buf_p, "(" );
670
671 for (i = 0 ; i < z ; i++) {
672 Parse2DMatrix (buf_p, y, x, m + i * x*y);
673 }
674
675 COM_MatchToken( buf_p, ")" );
676 }
677
678 /*
679 ===================
680 Com_HexStrToInt
681 ===================
682 */
683 int Com_HexStrToInt( const char *str )
684 {
685 if ( !str )
686 return -1;
687
688 // check for hex code
689 if( str[ 0 ] == '0' && str[ 1 ] == 'x' && str[ 2 ] != '\0' )
690 {
691 int i, n = 0, len = strlen( str );
692
693 for( i = 2; i < len; i++ )
694 {
695 char digit;
696
697 n *= 16;
698
699 digit = tolower( str[ i ] );
700
701 if( digit >= '0' && digit <= '9' )
702 digit -= '0';
703 else if( digit >= 'a' && digit <= 'f' )
704 digit = digit - 'a' + 10;
705 else
706 return -1;
707
708 n += digit;
709 }
710
711 return n;
712 }
713
714 return -1;
715 }
716
717 /*
718 ============================================================================
719
720 LIBRARY REPLACEMENT FUNCTIONS
721
722 ============================================================================
723 */
724
725 int Q_isprint( int c )
726 {
727 if ( c >= 0x20 && c <= 0x7E )
728 return ( 1 );
729 return ( 0 );
730 }
731
732 int Q_islower( int c )
733 {
734 if (c >= 'a' && c <= 'z')
735 return ( 1 );
736 return ( 0 );
737 }
738
739 int Q_isupper( int c )
740 {
741 if (c >= 'A' && c <= 'Z')
742 return ( 1 );
743 return ( 0 );
744 }
745
746 int Q_isalpha( int c )
747 {
748 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
749 return ( 1 );
750 return ( 0 );
751 }
752
753 qboolean Q_isanumber( const char *s )
754 {
755 char *p;
756 double Q_UNUSED_VAR d;
757
758 if( *s == '\0' )
759 return qfalse;
760
761 d = strtod( s, &p );
762
763 return *p == '\0';
764 }
765
766 qboolean Q_isintegral( float f )
767 {
768 return (int)f == f;
769 }
770
771 #ifdef _WIN32
772 /*
773 =============
774 Q_vsnprintf
775
776 Special wrapper function for Microsoft's broken _vsnprintf() function.
777 MinGW comes with its own vsnprintf() which is not broken. mingw-w64
778 however, uses Microsoft's broken _vsnprintf() function.
779 =============
780 */
781
782 int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
783 {
784 int retval;
785
786 retval = _vsnprintf(str, size, format, ap);
787
788 if(retval < 0 || retval == size)
789 {
790 // Microsoft doesn't adhere to the C99 standard of vsnprintf,
791 // which states that the return value must be the number of
792 // bytes written if the output string had sufficient length.
793 //
794 // Obviously we cannot determine that value from Microsoft's
795 // implementation, so we have no choice but to return size.
796
797 str[size - 1] = '\0';
798 return size;
799 }
800
801 return retval;
802 }
803 #endif
804
805 /*
806 =============
807 Q_strncpyz
808
809 Safe strncpy that ensures a trailing zero
810 =============
811 */
812 void Q_strncpyz( char *dest, const char *src, int destsize ) {
813 if ( !dest ) {
814 Com_Error( ERR_FATAL, "Q_strncpyz: NULL dest" );
815 }
816 if ( !src ) {
817 Com_Error( ERR_FATAL, "Q_strncpyz: NULL src" );
818 }
819 if ( destsize < 1 ) {
820 Com_Error(ERR_FATAL,"Q_strncpyz: destsize < 1" );
821 }
822
823 strncpy( dest, src, destsize-1 );
824 dest[destsize-1] = 0;
825 }
826
827 int Q_stricmpn (const char *s1, const char *s2, int n) {
828 int c1, c2;
829
830 if ( s1 == NULL ) {
831 if ( s2 == NULL )
832 return 0;
833 else
834 return -1;
835 }
836 else if ( s2==NULL )
837 return 1;
838
839
840
841 do {
842 c1 = *s1++;
843 c2 = *s2++;
844
845 if (!n--) {
846 return 0; // strings are equal until end point
847 }
848
849 if (c1 != c2) {
850 if (c1 >= 'a' && c1 <= 'z') {
851 c1 -= ('a' - 'A');
852 }
853 if (c2 >= 'a' && c2 <= 'z') {
854 c2 -= ('a' - 'A');
855 }
856 if (c1 != c2) {
857 return c1 < c2 ? -1 : 1;
858 }
859 }
860 } while (c1);
861
862 return 0; // strings are equal
863 }
864
865 int Q_strncmp (const char *s1, const char *s2, int n) {
866 int c1, c2;
867
868 do {
869 c1 = *s1++;
870 c2 = *s2++;
871
872 if (!n--) {
873 return 0; // strings are equal until end point
874 }
875
876 if (c1 != c2) {
877 return c1 < c2 ? -1 : 1;
878 }
879 } while (c1);
880
881 return 0; // strings are equal
882 }
883
884 int Q_stricmp (const char *s1, const char *s2) {
885 return (s1 && s2) ? Q_stricmpn (s1, s2, 99999) : -1;
886 }
887
888
889 char *Q_strlwr( char *s1 ) {
890 char *s;
891
892 s = s1;
893 while ( *s ) {
894 *s = tolower(*s);
895 s++;
896 }
897 return s1;
898 }
899
900 char *Q_strupr( char *s1 ) {
901 char *s;
902
903 s = s1;
904 while ( *s ) {
905 *s = toupper(*s);
906 s++;
907 }
908 return s1;
909 }
910
911
912 // never goes past bounds or leaves without a terminating 0
913 void Q_strcat( char *dest, int size, const char *src ) {
914 int l1;
915
916 l1 = strlen( dest );
917 if ( l1 >= size ) {
918 Com_Error( ERR_FATAL, "Q_strcat: already overflowed" );
919 }
920 Q_strncpyz( dest + l1, src, size - l1 );
921 }
922
923 /*
924 * Find the first occurrence of find in s.
925 */
926 const char *Q_stristr( const char *s, const char *find)
927 {
928 char c, sc;
929 size_t len;
930
931 if ((c = *find++) != 0)
932 {
933 if (c >= 'a' && c <= 'z')
934 {
935 c -= ('a' - 'A');
936 }
937 len = strlen(find);
938 do
939 {
940 do
941 {
942 if ((sc = *s++) == 0)
943 return NULL;
944 if (sc >= 'a' && sc <= 'z')
945 {
946 sc -= ('a' - 'A');
947 }
948 } while (sc != c);
949 } while (Q_stricmpn(s, find, len) != 0);
950 s--;
951 }
952 return s;
953 }
954
955
956 int Q_PrintStrlen( const char *string ) {
957 int len;
958 const char *p;
959
960 if( !string ) {
961 return 0;
962 }
963
964 len = 0;
965 p = string;
966 while( *p ) {
967 if( Q_IsColorString( p ) ) {
968 p += 2;
969 continue;
970 }
971 p++;
972 len++;
973 }
974
975 return len;
976 }
977
978
979 char *Q_CleanStr( char *string ) {
980 char* d;
981 char* s;
982 int c;
983
984 s = string;
985 d = string;
986 while ((c = *s) != 0 ) {
987 if ( Q_IsColorString( s ) ) {
988 s++;
989 }
990 else if ( c >= 0x20 && c <= 0x7E ) {
991 *d++ = c;
992 }
993 s++;
994 }
995 *d = '\0';
996
997 return string;
998 }
999
1000 int Q_CountChar(const char *string, char tocount)
1001 {
1002 int count;
1003
1004 for(count = 0; *string; string++)
1005 {
1006 if(*string == tocount)
1007 count++;
1008 }
1009
1010 return count;
1011 }
1012
1013 int QDECL Com_sprintf(char *dest, int size, const char *fmt, ...)
1014 {
1015 int len;
1016 va_list argptr;
1017
1018 va_start (argptr,fmt);
1019 len = Q_vsnprintf(dest, size, fmt, argptr);
1020 va_end (argptr);
1021
1022 if(len >= size)
1023 Com_Printf("Com_sprintf: Output length %d too short, require %d bytes.\n", size, len + 1);
1024
1025 return len;
1026 }
1027
1028 /*
1029 ============
1030 va
1031
1032 does a varargs printf into a temp buffer, so I don't need to have
1033 varargs versions of all text functions.
1034 ============
1035 */
1036 char * QDECL va( char *format, ... ) {
1037 va_list argptr;
1038 static char string[2][32000]; // in case va is called by nested functions
1039 static int index = 0;
1040 char *buf;
1041
1042 buf = string[index & 1];
1043 index++;
1044
1045 va_start (argptr, format);
1046 Q_vsnprintf (buf, sizeof(*string), format, argptr);
1047 va_end (argptr);
1048
1049 return buf;
1050 }
1051
1052 /*
1053 ============
1054 Com_TruncateLongString
1055
1056 Assumes buffer is atleast TRUNCATE_LENGTH big
1057 ============
1058 */
1059 void Com_TruncateLongString( char *buffer, const char *s )
1060 {
1061 int length = strlen( s );
1062
1063 if( length <= TRUNCATE_LENGTH )
1064 Q_strncpyz( buffer, s, TRUNCATE_LENGTH );
1065 else
1066 {
1067 Q_strncpyz( buffer, s, ( TRUNCATE_LENGTH / 2 ) - 3 );
1068 Q_strcat( buffer, TRUNCATE_LENGTH, " ... " );
1069 Q_strcat( buffer, TRUNCATE_LENGTH, s + length - ( TRUNCATE_LENGTH / 2 ) + 3 );
1070 }
1071 }
1072
1073 /*
1074 =====================================================================
1075
1076 INFO STRINGS
1077
1078 =====================================================================
1079 */
1080
1081 /*
1082 ===============
1083 Info_ValueForKey
1084
1085 Searches the string for the given
1086 key and returns the associated value, or an empty string.
1087 FIXME: overflow check?
1088 ===============
1089 */
1090 char *Info_ValueForKey( const char *s, const char *key ) {
1091 char pkey[BIG_INFO_KEY];
1092 static char value[2][BIG_INFO_VALUE]; // use two buffers so compares
1093 // work without stomping on each other
1094 static int valueindex = 0;
1095 char *o;
1096
1097 if ( !s || !key ) {
1098 return "";
1099 }
1100
1101 if ( strlen( s ) >= BIG_INFO_STRING ) {
1102 Com_Error( ERR_DROP, "Info_ValueForKey: oversize infostring" );
1103 }
1104
1105 valueindex ^= 1;
1106 if (*s == '\\')
1107 s++;
1108 while (1)
1109 {
1110 o = pkey;
1111 while (*s != '\\')
1112 {
1113 if (!*s)
1114 return "";
1115 *o++ = *s++;
1116 }
1117 *o = 0;
1118 s++;
1119
1120 o = value[valueindex];
1121
1122 while (*s != '\\' && *s)
1123 {
1124 *o++ = *s++;
1125 }
1126 *o = 0;
1127
1128 if (!Q_stricmp (key, pkey) )
1129 return value[valueindex];
1130
1131 if (!*s)
1132 break;
1133 s++;
1134 }
1135
1136 return "";
1137 }
1138
1139
1140 /*
1141 ===================
1142 Info_NextPair
1143
1144 Used to itterate through all the key/value pairs in an info string
1145 ===================
1146 */
1147 void Info_NextPair( const char **head, char *key, char *value ) {
1148 char *o;
1149 const char *s;
1150
1151 s = *head;
1152
1153 if ( *s == '\\' ) {
1154 s++;
1155 }
1156 key[0] = 0;
1157 value[0] = 0;
1158
1159 o = key;
1160 while ( *s != '\\' ) {
1161 if ( !*s ) {
1162 *o = 0;
1163 *head = s;
1164 return;
1165 }
1166 *o++ = *s++;
1167 }
1168 *o = 0;
1169 s++;
1170
1171 o = value;
1172 while ( *s != '\\' && *s ) {
1173 *o++ = *s++;
1174 }
1175 *o = 0;
1176
1177 *head = s;
1178 }
1179
1180
1181 /*
1182 ===================
1183 Info_RemoveKey
1184 ===================
1185 */
1186 void Info_RemoveKey( char *s, const char *key ) {
1187 char *start;
1188 char pkey[MAX_INFO_KEY];
1189 char value[MAX_INFO_VALUE];
1190 char *o;
1191
1192 if ( strlen( s ) >= MAX_INFO_STRING ) {
1193 Com_Error( ERR_DROP, "Info_RemoveKey: oversize infostring" );
1194 }
1195
1196 if (strchr (key, '\\')) {
1197 return;
1198 }
1199
1200 while (1)
1201 {
1202 start = s;
1203 if (*s == '\\')
1204 s++;
1205 o = pkey;
1206 while (*s != '\\')
1207 {
1208 if (!*s)
1209 return;
1210 *o++ = *s++;
1211 }
1212 *o = 0;
1213 s++;
1214
1215 o = value;
1216 while (*s != '\\' && *s)
1217 {
1218 if (!*s)
1219 return;
1220 *o++ = *s++;
1221 }
1222 *o = 0;
1223
1224 if (!Q_stricmp (key, pkey) )
1225 {
1226 memmove(start, s, strlen(s) + 1); // remove this part
1227
1228 return;
1229 }
1230
1231 if (!*s)
1232 return;
1233 }
1234
1235 }
1236
1237 /*
1238 ===================
1239 Info_RemoveKey_Big
1240 ===================
1241 */
1242 void Info_RemoveKey_Big( char *s, const char *key ) {
1243 char *start;
1244 char pkey[BIG_INFO_KEY];
1245 char value[BIG_INFO_VALUE];
1246 char *o;
1247
1248 if ( strlen( s ) >= BIG_INFO_STRING ) {
1249 Com_Error( ERR_DROP, "Info_RemoveKey_Big: oversize infostring" );
1250 }
1251
1252 if (strchr (key, '\\')) {
1253 return;
1254 }
1255
1256 while (1)
1257 {
1258 start = s;
1259 if (*s == '\\')
1260 s++;
1261 o = pkey;
1262 while (*s != '\\')
1263 {
1264 if (!*s)
1265 return;
1266 *o++ = *s++;
1267 }
1268 *o = 0;
1269 s++;
1270
1271 o = value;
1272 while (*s != '\\' && *s)
1273 {
1274 if (!*s)
1275 return;
1276 *o++ = *s++;
1277 }
1278 *o = 0;
1279
1280 if (!Q_stricmp (key, pkey) )
1281 {
1282 memmove(start, s, strlen(s) + 1); // remove this part
1283 return;
1284 }
1285
1286 if (!*s)
1287 return;
1288 }
1289
1290 }
1291
1292
1293
1294
1295 /*
1296 ==================
1297 Info_Validate
1298
1299 Some characters are illegal in info strings because they
1300 can mess up the server's parsing
1301 ==================
1302 */
1303 qboolean Info_Validate( const char *s ) {
1304 const char* ch = s;
1305
1306 while ( *ch != '\0' )
1307 {
1308 if( !Q_isprint( *ch ) )
1309 return qfalse;
1310
1311 if( *ch == '\"' )
1312 return qfalse;
1313
1314 if( *ch == ';' )
1315 return qfalse;
1316
1317 ++ch;
1318 }
1319
1320 return qtrue;
1321 }
1322
1323 /*
1324 ==================
1325 Info_SetValueForKey
1326
1327 Changes or adds a key/value pair
1328 ==================
1329 */
1330 void Info_SetValueForKey( char *s, const char *key, const char *value ) {
1331 char newi[MAX_INFO_STRING];
1332 const char* blacklist = "\\;\"";
1333
1334 if ( strlen( s ) >= MAX_INFO_STRING ) {
1335 Com_Error( ERR_DROP, "Info_SetValueForKey: oversize infostring" );
1336 }
1337
1338 for(; *blacklist; ++blacklist)
1339 {
1340 if (strchr (key, *blacklist) || strchr (value, *blacklist))
1341 {
1342 Com_Printf (S_COLOR_YELLOW "Can't use keys or values with a '%c': %s = %s\n", *blacklist, key, value);
1343 return;
1344 }
1345 }
1346
1347 Info_RemoveKey (s, key);
1348 if (!value || !strlen(value))
1349 return;
1350
1351 Com_sprintf (newi, sizeof(newi), "\\%s\\%s", key, value);
1352
1353 if (strlen(newi) + strlen(s) >= MAX_INFO_STRING)
1354 {
1355 Com_Printf ("Info string length exceeded\n");
1356 return;
1357 }
1358
1359 strcat (newi, s);
1360 strcpy (s, newi);
1361 }
1362
1363 /*
1364 ==================
1365 Info_SetValueForKey_Big
1366
1367 Changes or adds a key/value pair
1368 Includes and retains zero-length values
1369 ==================
1370 */
1371 void Info_SetValueForKey_Big( char *s, const char *key, const char *value ) {
1372 char newi[BIG_INFO_STRING];
1373 const char* blacklist = "\\;\"";
1374
1375 if ( strlen( s ) >= BIG_INFO_STRING ) {
1376 Com_Error( ERR_DROP, "Info_SetValueForKey: oversize infostring" );
1377 }
1378
1379 for(; *blacklist; ++blacklist)
1380 {
1381 if (strchr (key, *blacklist) || strchr (value, *blacklist))
1382 {
1383 Com_Printf (S_COLOR_YELLOW "Can't use keys or values with a '%c': %s = %s\n", *blacklist, key, value);
1384 return;
1385 }
1386 }
1387
1388 Info_RemoveKey_Big (s, key);
1389 if (!value)
1390 return;
1391
1392 Com_sprintf (newi, sizeof(newi), "\\%s\\%s", key, value);
1393
1394 if (strlen(newi) + strlen(s) >= BIG_INFO_STRING)
1395 {
1396 Com_Printf ("BIG Info string length exceeded\n");
1397 return;
1398 }
1399
1400 strcat (s, newi);
1401 }
1402
1403
1404
1405
1406 //====================================================================
1407
1408 /*
1409 ==================
1410 Com_CharIsOneOfCharset
1411 ==================
1412 */
1413 static qboolean Com_CharIsOneOfCharset( char c, char *set )
1414 {
1415 int i;
1416
1417 for( i = 0; i < strlen( set ); i++ )
1418 {
1419 if( set[ i ] == c )
1420 return qtrue;
1421 }
1422
1423 return qfalse;
1424 }
1425
1426 /*
1427 ==================
1428 Com_SkipCharset
1429 ==================
1430 */
1431 char *Com_SkipCharset( char *s, char *sep )
1432 {
1433 char *p = s;
1434
1435 while( p )
1436 {
1437 if( Com_CharIsOneOfCharset( *p, sep ) )
1438 p++;
1439 else
1440 break;
1441 }
1442
1443 return p;
1444 }
1445
1446 /*
1447 ==================
1448 Com_SkipTokens
1449 ==================
1450 */
1451 char *Com_SkipTokens( char *s, int numTokens, char *sep )
1452 {
1453 int sepCount = 0;
1454 char *p = s;
1455
1456 while( sepCount < numTokens )
1457 {
1458 if( Com_CharIsOneOfCharset( *p++, sep ) )
1459 {
1460 sepCount++;
1461 while( Com_CharIsOneOfCharset( *p, sep ) )
1462 p++;
1463 }
1464 else if( *p == '\0' )
1465 break;
1466 }
1467
1468 if( sepCount == numTokens )
1469 return p;
1470 else
1471 return s;
1472 }