Debian Codegraph @

code/qcommon/q_shared.c

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