2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 // q_shared.c -- stateless support routines that are included in each code dll
26 ============================================================================
30 ============================================================================
33 void Com_InitGrowList( growList_t *list, int maxElements ) {
34 list->maxElements = maxElements;
35 list->currentElements = 0;
36 list->elements = (void **)malloc( list->maxElements * sizeof( void * ) );
39 int Com_AddToGrowList( growList_t *list, void *data ) {
42 if ( list->currentElements != list->maxElements ) {
43 list->elements[list->currentElements] = data;
44 return list->currentElements++;
47 // grow, reallocate and move
50 if ( list->maxElements < 0 ) {
51 Com_Error( ERR_FATAL, "Com_AddToGrowList: maxElements = %i", list->maxElements );
54 if ( list->maxElements == 0 ) {
55 // initialize the list to hold 100 elements
56 Com_InitGrowList( list, 100 );
57 return Com_AddToGrowList( list, data );
60 list->maxElements *= 2;
62 Com_DPrintf( "Resizing growlist to %i maxElements\n", list->maxElements );
64 list->elements = (void **)malloc( list->maxElements * sizeof( void * ) );
66 if ( !list->elements ) {
67 Com_Error( ERR_DROP, "Growlist alloc failed" );
70 memcpy( list->elements, old, list->currentElements * sizeof( void * ) );
74 return Com_AddToGrowList( list, data );
77 void *Com_GrowListElement( const growList_t *list, int index ) {
78 if ( index < 0 || index >= list->currentElements ) {
79 Com_Error( ERR_DROP, "Com_GrowListElement: %i out of range of %i",
80 index, list->currentElements );
82 return list->elements[index];
85 int Com_IndexForGrowListElement( const growList_t *list, const void *element ) {
88 for ( i = 0 ; i < list->currentElements ; i++ ) {
89 if ( list->elements[i] == element ) {
96 //============================================================================
99 float Com_Clamp( float min, float max, float value ) {
114 const char *Com_StringContains( const char *str1, const char *str2, int casesensitive ) {
117 len = strlen( str1 ) - strlen( str2 );
118 for ( i = 0; i <= len; i++, str1++ ) {
119 for ( j = 0; str2[j]; j++ ) {
120 if ( casesensitive ) {
121 if ( str1[j] != str2[j] ) {
126 if ( toupper( str1[j] ) != toupper( str2[j] ) ) {
143 int Com_Filter( const char *filter, const char *name, int casesensitive ){
144 char buf[MAX_TOKEN_CHARS];
149 if ( *filter == '*' ) {
151 for ( i = 0; *filter; i++ ) {
152 if ( *filter == '*' || *filter == '?' ) {
159 if ( strlen( buf ) ) {
160 ptr = Com_StringContains( name, buf, casesensitive );
164 name = ptr + strlen( buf );
167 else if ( *filter == '?' ) {
171 else if ( *filter == '[' && *( filter + 1 ) == '[' ) {
174 else if ( *filter == '[' ) {
177 while ( *filter && !found ) {
178 if ( *filter == ']' && *( filter + 1 ) != ']' ) {
181 if ( *( filter + 1 ) == '-' && *( filter + 2 ) && ( *( filter + 2 ) != ']' || *( filter + 3 ) == ']' ) ) {
182 if ( casesensitive ) {
183 if ( *name >= *filter && *name <= *( filter + 2 ) ) {
188 if ( toupper( *name ) >= toupper( *filter ) &&
189 toupper( *name ) <= toupper( *( filter + 2 ) ) ) {
196 if ( casesensitive ) {
197 if ( *filter == *name ) {
202 if ( toupper( *filter ) == toupper( *name ) ) {
213 if ( *filter == ']' && *( filter + 1 ) != ']' ) {
222 if ( casesensitive ) {
223 if ( *filter != *name ) {
228 if ( toupper( *filter ) != toupper( *name ) ) {
246 int Com_HashString( const char *fname ) {
253 while ( fname[i] != '\0' ) {
254 letter = tolower( fname[i] );
255 if ( letter == '.' ) {
256 break; // don't include extension
258 if ( letter == '\\' ) {
259 letter = '/'; // damn path names
261 hash += (long)( letter ) * ( i + 119 );
264 hash &= ( FILE_HASH_SIZE - 1 );
274 char *Com_SkipPath( char *pathname ){
280 if ( *pathname == '/' ) {
293 void Com_StripExtension( const char *in, char *out ) {
294 while ( *in && *in != '.' ) {
306 void Com_DefaultExtension( char *path, int maxSize, const char *extension ) {
307 char oldPath[MAX_QPATH];
311 // if path doesn't have a .EXT, append extension
312 // (extension should include the .)
314 src = path + strlen( path ) - 1;
316 while ( *src != '/' && src != path ) {
318 return; // it has an extension
323 Q_strncpyz( oldPath, path, sizeof( oldPath ) );
324 Com_sprintf( path, maxSize, "%s%s", oldPath, extension );
328 ============================================================================
332 ============================================================================
335 // can't just use function pointers, or dll linkage can
336 // mess up when qcommon is included in multiple places
337 static short ( *_BigShort )( short l );
338 static short ( *_LittleShort )( short l );
339 static int ( *_BigLong )( int l );
340 static int ( *_LittleLong )( int l );
341 static float ( *_BigFloat )( float l );
342 static float ( *_LittleFloat )( float l );
344 short BigShort( short l ){return _BigShort( l ); }
345 short LittleShort( short l ) {return _LittleShort( l ); }
346 int BigLong( int l ) {return _BigLong( l ); }
347 int LittleLong( int l ) {return _LittleLong( l ); }
348 float BigFloat( float l ) {return _BigFloat( l ); }
349 float LittleFloat( float l ) {return _LittleFloat( l ); }
351 short ShortSwap( short l ){
355 b2 = ( l >> 8 ) & 255;
357 return ( b1 << 8 ) + b2;
360 short ShortNoSwap( short l ){
364 int LongSwap( int l ){
368 b2 = ( l >> 8 ) & 255;
369 b3 = ( l >> 16 ) & 255;
370 b4 = ( l >> 24 ) & 255;
372 return ( (int)b1 << 24 ) + ( (int)b2 << 16 ) + ( (int)b3 << 8 ) + b4;
375 int LongNoSwap( int l ){
379 float FloatSwap( float f ){
388 dat2.b[0] = dat1.b[3];
389 dat2.b[1] = dat1.b[2];
390 dat2.b[2] = dat1.b[1];
391 dat2.b[3] = dat1.b[0];
395 float FloatNoSwap( float f ){
404 void Swap_Init( void ){
405 byte swaptest[2] = {1,0};
407 // set the byte swapping variables in a portable manner
408 if ( *(short *)swaptest == 1 ) {
409 _BigShort = ShortSwap;
410 _LittleShort = ShortNoSwap;
412 _LittleLong = LongNoSwap;
413 _BigFloat = FloatSwap;
414 _LittleFloat = FloatNoSwap;
418 _BigShort = ShortNoSwap;
419 _LittleShort = ShortSwap;
420 _BigLong = LongNoSwap;
421 _LittleLong = LongSwap;
422 _BigFloat = FloatNoSwap;
423 _LittleFloat = FloatSwap;
433 int Com_ParseInfos( const char *buf, int max, char infos[][MAX_INFO_STRING] ) {
436 char key[MAX_TOKEN_CHARS];
441 token = Com_Parse( &buf );
445 if ( strcmp( token, "{" ) ) {
446 Com_Printf( "Missing { in info file\n" );
450 if ( count == max ) {
451 Com_Printf( "Max infos exceeded\n" );
457 token = Com_Parse( &buf );
459 Com_Printf( "Unexpected end of info file\n" );
462 if ( !strcmp( token, "}" ) ) {
465 Q_strncpyz( key, token, sizeof( key ) );
467 token = Com_ParseOnLine( &buf );
471 Info_SetValueForKey( infos[count], key, token );
482 ============================================================================
484 LIBRARY REPLACEMENT FUNCTIONS
486 ============================================================================
489 int Q_isprint( int c ){
490 if ( c >= 0x20 && c <= 0x7E ) {
496 int Q_islower( int c ){
497 if ( c >= 'a' && c <= 'z' ) {
503 int Q_isupper( int c ){
504 if ( c >= 'A' && c <= 'Z' ) {
510 int Q_isalpha( int c ){
511 if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) {
517 char* Q_strrchr( const char* string, int c ){
520 char *sp = (char *)0;
542 Safe strncpy that ensures a trailing zero
545 void Q_strncpyz( char *dest, const char *src, int destsize ) {
547 Com_Error( ERR_FATAL, "Q_strncpyz: NULL src" );
549 if ( destsize < 1 ) {
550 Com_Error( ERR_FATAL,"Q_strncpyz: destsize < 1" );
553 strncpy( dest, src, destsize - 1 );
554 dest[destsize - 1] = 0;
557 int Q_stricmpn( const char *s1, const char *s2, int n ) {
565 return 0; // strings are equal until end point
569 if ( c1 >= 'a' && c1 <= 'z' ) {
572 if ( c2 >= 'a' && c2 <= 'z' ) {
576 return c1 < c2 ? -1 : 1;
581 return 0; // strings are equal
584 int Q_strncmp( const char *s1, const char *s2, int n ) {
592 return 0; // strings are equal until end point
596 return c1 < c2 ? -1 : 1;
600 return 0; // strings are equal
603 int Q_stricmp( const char *s1, const char *s2 ) {
604 return Q_stricmpn( s1, s2, 99999 );
608 char *Q_strlwr( char *s1 ) {
619 char *Q_strupr( char *s1 ) {
631 // never goes past bounds or leaves without a terminating 0
632 void Q_strcat( char *dest, int size, const char *src ) {
637 Com_Error( ERR_FATAL, "Q_strcat: already overflowed" );
639 Q_strncpyz( dest + l1, src, size - l1 );
643 int Q_PrintStrlen( const char *string ) {
654 if ( Q_IsColorString( p ) ) {
666 char *Q_CleanStr( char *string ) {
673 while ( ( c = *s ) != 0 ) {
674 if ( Q_IsColorString( s ) ) {
677 else if ( c >= 0x20 && c <= 0x7E ) {
688 void QDECL Com_sprintf( char *dest, int size, const char *fmt, ... ) {
691 char bigbuffer[32000]; // big, but small enough to fit in PPC stack
693 va_start( argptr,fmt );
694 len = vsprintf( bigbuffer,fmt,argptr );
696 if ( len >= sizeof( bigbuffer ) ) {
697 Com_Error( ERR_FATAL, "Com_sprintf: overflowed bigbuffer" );
700 Com_Printf( "Com_sprintf: overflow of %i in %i\n", len, size );
702 Q_strncpyz( dest, bigbuffer, size );
710 does a varargs printf into a temp buffer, so I don't need to have
711 varargs versions of all text functions.
712 FIXME: make this buffer size safe someday
715 char * QDECL va( char *format, ... ) {
717 static char string[2][32000]; // in case va is called by nested functions
718 static int index = 0;
721 buf = string[index & 1];
724 va_start( argptr, format );
725 vsprintf( buf, format,argptr );
733 =====================================================================
737 =====================================================================
744 Searches the string for the given
745 key and returns the associated value, or an empty string.
746 FIXME: overflow check?
749 char *Info_ValueForKey( const char *s, const char *key ) {
750 char pkey[MAX_INFO_KEY];
751 static char value[2][MAX_INFO_VALUE]; // use two buffers so compares
752 // work without stomping on each other
753 static int valueindex = 0;
760 if ( strlen( s ) >= MAX_INFO_STRING ) {
761 Com_Error( ERR_DROP, "Info_ValueForKey: oversize infostring" );
781 o = value[valueindex];
783 while ( *s != '\\' && *s )
789 if ( !Q_stricmp( key, pkey ) ) {
790 return value[valueindex];
807 Used to itterate through all the key/value pairs in an info string
810 void Info_NextPair( const char *( *head ), char key[MAX_INFO_KEY], char value[MAX_INFO_VALUE] ) {
823 while ( *s != '\\' ) {
835 while ( *s != '\\' && *s ) {
849 void Info_RemoveKey( char *s, const char *key ) {
851 char pkey[MAX_INFO_KEY];
852 char value[MAX_INFO_VALUE];
855 if ( strlen( s ) >= MAX_INFO_STRING ) {
856 Com_Error( ERR_DROP, "Info_RemoveKey: oversize infostring" );
859 if ( strchr( key, '\\' ) ) {
881 while ( *s != '\\' && *s )
890 if ( !strcmp( key, pkey ) ) {
891 strcpy( start, s ); // remove this part
907 Some characters are illegal in info strings because they
908 can mess up the server's parsing
911 qboolean Info_Validate( const char *s ) {
912 if ( strchr( s, '\"' ) ) {
915 if ( strchr( s, ';' ) ) {
925 Changes or adds a key/value pair
928 void Info_SetValueForKey( char *s, const char *key, const char *value ) {
929 char newi[MAX_INFO_STRING];
931 if ( strlen( s ) >= MAX_INFO_STRING ) {
932 Com_Error( ERR_DROP, "Info_SetValueForKey: oversize infostring" );
935 if ( strchr( key, '\\' ) || strchr( value, '\\' ) ) {
936 Com_Printf( "Can't use keys or values with a \\\n" );
940 if ( strchr( key, ';' ) || strchr( value, ';' ) ) {
941 Com_Printf( "Can't use keys or values with a semicolon\n" );
945 if ( strchr( key, '\"' ) || strchr( value, '\"' ) ) {
946 Com_Printf( "Can't use keys or values with a \"\n" );
950 Info_RemoveKey( s, key );
951 if ( !value || !strlen( value ) ) {
955 Com_sprintf( newi, sizeof( newi ), "\\%s\\%s", key, value );
957 if ( strlen( newi ) + strlen( s ) > MAX_INFO_STRING ) {
958 Com_Printf( "Info string length exceeded\n" );
965 //====================================================================
973 int ParseHex( const char *text ) {
978 while ( ( c = *text++ ) != 0 ) {
979 if ( c >= '0' && c <= '9' ) {
980 value = value * 16 + c - '0';
983 if ( c >= 'a' && c <= 'f' ) {
984 value = value * 16 + 10 + c - 'a';
987 if ( c >= 'A' && c <= 'F' ) {
988 value = value * 16 + 10 + c - 'A';