Debian Codegraph @

code/qcommon/q_shared.c

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