This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/common/infostring.c

288 lines
5.4 KiB
C
Raw Normal View History

2008-08-03 22:00:00 +02:00
//=======================================================================
// Copyright XashXT Group 2008 <20>
// infostring.c - network info strings
//=======================================================================
#include "common.h"
2009-11-02 22:00:00 +01:00
#define MAX_INFO_KEY 64
#define MAX_INFO_VALUE 64
2009-11-23 22:00:00 +01:00
static char infostring[MAX_INFO_STRING*4];
2008-08-03 22:00:00 +02:00
/*
=======================================================================
INFOSTRING STUFF
=======================================================================
*/
/*
===============
Info_Print
printing current key-value pair
===============
*/
2009-11-23 22:00:00 +01:00
void Info_Print( const char *s )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
char key[MAX_INFO_STRING];
char value[MAX_INFO_STRING];
2008-08-03 22:00:00 +02:00
char *o;
int l;
2009-11-23 22:00:00 +01:00
if( *s == '\\' ) s++;
2008-08-03 22:00:00 +02:00
2009-11-23 22:00:00 +01:00
while( *s )
2008-08-03 22:00:00 +02:00
{
o = key;
2009-11-23 22:00:00 +01:00
while( *s && *s != '\\' )
*o++ = *s++;
2008-08-03 22:00:00 +02:00
l = o - key;
2009-11-23 22:00:00 +01:00
if( l < 20 )
2008-08-03 22:00:00 +02:00
{
2011-03-10 22:00:00 +01:00
Q_memset( o, ' ', 20 - l );
2008-08-03 22:00:00 +02:00
key[20] = 0;
}
else *o = 0;
2009-11-23 22:00:00 +01:00
Msg( "%s", key );
2008-08-03 22:00:00 +02:00
2009-11-23 22:00:00 +01:00
if( !*s )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
Msg( "(null)\n" );
2008-08-03 22:00:00 +02:00
return;
}
o = value;
s++;
2009-11-23 22:00:00 +01:00
while( *s && *s != '\\' )
*o++ = *s++;
2008-08-03 22:00:00 +02:00
*o = 0;
2009-11-23 22:00:00 +01:00
if( *s ) s++;
Msg( "%s\n", value );
2008-08-03 22:00:00 +02:00
}
}
/*
===============
Info_ValueForKey
Searches the string for the given
key and returns the associated value, or an empty string.
===============
*/
2009-11-10 22:00:00 +01:00
char *Info_ValueForKey( const char *s, const char *key )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
char pkey[MAX_INFO_STRING];
static char value[2][MAX_INFO_STRING]; // use two buffers so compares work without stomping on each other
2008-08-03 22:00:00 +02:00
static int valueindex;
char *o;
valueindex ^= 1;
if( *s == '\\' ) s++;
while( 1 )
{
o = pkey;
while( *s != '\\' && *s != '\n' )
{
2009-11-23 22:00:00 +01:00
if( !*s ) return "";
2008-08-03 22:00:00 +02:00
*o++ = *s++;
}
2009-11-23 22:00:00 +01:00
2008-08-03 22:00:00 +02:00
*o = 0;
s++;
o = value[valueindex];
2009-11-23 22:00:00 +01:00
while( *s != '\\' && *s != '\n' && *s )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
if( !*s ) return "";
2008-08-03 22:00:00 +02:00
*o++ = *s++;
}
*o = 0;
2011-03-09 22:00:00 +01:00
if( !Q_strcmp( key, pkey ))
2009-11-23 22:00:00 +01:00
return value[valueindex];
if( !*s ) return "";
2008-08-03 22:00:00 +02:00
s++;
}
}
2010-10-26 22:00:00 +02:00
qboolean Info_RemoveKey( char *s, const char *key )
2008-08-03 22:00:00 +02:00
{
char *start;
2009-11-23 22:00:00 +01:00
char pkey[MAX_INFO_STRING];
char value[MAX_INFO_STRING];
2008-08-03 22:00:00 +02:00
char *o;
2011-03-09 22:00:00 +01:00
if( Q_strstr( key, "\\" ))
2009-11-23 22:00:00 +01:00
return false;
2008-08-03 22:00:00 +02:00
2009-11-23 22:00:00 +01:00
while( 1 )
2008-08-03 22:00:00 +02:00
{
start = s;
2009-11-23 22:00:00 +01:00
if( *s == '\\' ) s++;
2008-08-03 22:00:00 +02:00
o = pkey;
2009-11-23 22:00:00 +01:00
while( *s != '\\' )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
if( !*s ) return false;
2008-08-03 22:00:00 +02:00
*o++ = *s++;
}
*o = 0;
s++;
o = value;
2009-11-23 22:00:00 +01:00
while( *s != '\\' && *s )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
if( !*s ) return false;
2008-08-03 22:00:00 +02:00
*o++ = *s++;
}
*o = 0;
2011-03-09 22:00:00 +01:00
if( !Q_strcmp( key, pkey ))
2008-08-03 22:00:00 +02:00
{
2011-03-09 22:00:00 +01:00
Q_strcpy( start, s ); // remove this part
2009-11-23 22:00:00 +01:00
return true;
2008-08-03 22:00:00 +02:00
}
2009-11-23 22:00:00 +01:00
if( !*s ) return false;
2008-08-03 22:00:00 +02:00
}
}
2009-12-04 22:00:00 +01:00
void Info_RemovePrefixedKeys( char *start, char prefix )
{
char *s, *o;
char pkey[MAX_INFO_STRING];
char value[MAX_INFO_STRING];
s = start;
while( 1 )
{
if( *s == '\\' )
s++;
o = pkey;
while( *s != '\\' )
{
if( !*s ) return;
*o++ = *s++;
}
*o = 0;
s++;
o = value;
while( *s != '\\' && *s )
{
if( !*s ) return;
*o++ = *s++;
}
*o = 0;
if( pkey[0] == prefix )
{
Info_RemoveKey( start, pkey );
s = start;
}
if( !*s ) return;
}
}
2008-08-03 22:00:00 +02:00
/*
==================
Info_Validate
Some characters are illegal in info strings because they
can mess up the server's parsing
==================
*/
2010-10-26 22:00:00 +02:00
qboolean Info_Validate( const char *s )
2008-08-03 22:00:00 +02:00
{
2011-03-09 22:00:00 +01:00
if( Q_strstr( s, "\"" )) return false;
if( Q_strstr( s, ";" )) return false;
2008-08-03 22:00:00 +02:00
return true;
}
2010-10-26 22:00:00 +02:00
qboolean Info_SetValueForKey( char *s, const char *key, const char *value )
2008-08-03 22:00:00 +02:00
{
char newi[MAX_INFO_STRING], *v;
int c, maxsize = MAX_INFO_STRING;
2011-03-09 22:00:00 +01:00
if( Q_strstr( key, "\\" ) || Q_strstr( value, "\\" ))
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
MsgDev( D_ERROR, "SetValueForKey: can't use keys or values with a \\\n" );
return false;
2008-08-03 22:00:00 +02:00
}
2011-03-09 22:00:00 +01:00
if( Q_strstr( key, ";" ))
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
MsgDev( D_ERROR, "SetValueForKey: can't use keys or values with a semicolon\n" );
return false;
2008-08-03 22:00:00 +02:00
}
2009-11-23 22:00:00 +01:00
2011-03-09 22:00:00 +01:00
if( Q_strstr( key, "\"" ) || Q_strstr( value, "\"" ))
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
MsgDev( D_ERROR, "SetValueForKey: can't use keys or values with a \"\n" );
return false;
2008-08-03 22:00:00 +02:00
}
2009-11-23 22:00:00 +01:00
2011-03-09 22:00:00 +01:00
if( Q_strlen( key ) > MAX_INFO_KEY - 1 || Q_strlen( value ) > MAX_INFO_KEY - 1 )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
MsgDev( D_ERROR, "SetValueForKey: keys and values must be < %i characters.\n", MAX_INFO_KEY );
return false;
2008-08-03 22:00:00 +02:00
}
2009-11-23 22:00:00 +01:00
Info_RemoveKey( s, key );
2011-03-09 22:00:00 +01:00
if( !value || !Q_strlen( value ))
2009-11-23 22:00:00 +01:00
return true; // just clear variable
2008-08-03 22:00:00 +02:00
2011-03-09 22:00:00 +01:00
Q_sprintf( newi, "\\%s\\%s", key, value );
if( Q_strlen( newi ) + Q_strlen( s ) > maxsize )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
MsgDev( D_ERROR, "SetValueForKey: info string length exceeded\n" );
return true; // info changed, new value can't saved
2008-08-03 22:00:00 +02:00
}
// only copy ascii values
2011-03-09 22:00:00 +01:00
s += Q_strlen( s );
2008-08-03 22:00:00 +02:00
v = newi;
2009-11-23 22:00:00 +01:00
while( *v )
2008-08-03 22:00:00 +02:00
{
c = *v++;
2009-11-25 22:00:00 +01:00
c &= 255; // strip high bits
if( c >= 32 && c <= 255 )
2009-11-23 22:00:00 +01:00
*s++ = c;
2008-08-03 22:00:00 +02:00
}
*s = 0;
2009-11-23 22:00:00 +01:00
// all done
return true;
2008-08-03 22:00:00 +02:00
}
2008-12-15 22:00:00 +01:00
static void Cvar_LookupBitInfo( const char *name, const char *string, const char *info, void *unused )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
Info_SetValueForKey( (char *)info, name, string );
2008-08-03 22:00:00 +02:00
}
2008-12-15 22:00:00 +01:00
char *Cvar_Userinfo( void )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
infostring[0] = 0; // clear previous calls
Cvar_LookupVars( CVAR_USERINFO, infostring, NULL, Cvar_LookupBitInfo );
return infostring;
2008-08-03 22:00:00 +02:00
}
2008-12-15 22:00:00 +01:00
char *Cvar_Serverinfo( void )
2008-08-03 22:00:00 +02:00
{
2009-11-23 22:00:00 +01:00
infostring[0] = 0; // clear previous calls
Cvar_LookupVars( CVAR_SERVERINFO, infostring, NULL, Cvar_LookupBitInfo );
return infostring;
2010-02-07 22:00:00 +01:00
}
char *Cvar_Physicinfo( void )
{
infostring[0] = 0; // clear previous calls
Cvar_LookupVars( CVAR_PHYSICINFO, infostring, NULL, Cvar_LookupBitInfo );
return infostring;
2008-08-03 22:00:00 +02:00
}