2007-09-13 22:00:00 +02:00
|
|
|
|
//=======================================================================
|
|
|
|
|
// Copyright XashXT Group 2007 <20>
|
|
|
|
|
// stdlib.c - std lib portable utils
|
|
|
|
|
//=======================================================================
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
#include "launch.h"
|
2007-11-27 22:00:00 +01:00
|
|
|
|
#include "mathlib.h"
|
2007-09-13 22:00:00 +02:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
void com_strnupr(const char *in, char *out, size_t size_out)
|
2007-09-13 22:00:00 +02:00
|
|
|
|
{
|
2007-11-09 22:00:00 +01:00
|
|
|
|
if (size_out == 0) return;
|
|
|
|
|
|
|
|
|
|
while (*in && size_out > 1)
|
|
|
|
|
{
|
|
|
|
|
if (*in >= 'a' && *in <= 'z')
|
|
|
|
|
*out++ = *in++ + 'A' - 'a';
|
|
|
|
|
else *out++ = *in++;
|
|
|
|
|
size_out--;
|
|
|
|
|
}
|
|
|
|
|
*out = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
void com_strupr(const char *in, char *out)
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
com_strnupr(in, out, 99999 );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void com_strnlwr(const char *in, char *out, size_t size_out)
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
if (size_out == 0) return;
|
|
|
|
|
|
|
|
|
|
while (*in && size_out > 1)
|
|
|
|
|
{
|
|
|
|
|
if (*in >= 'A' && *in <= 'Z')
|
|
|
|
|
*out++ = *in++ + 'a' - 'A';
|
|
|
|
|
else *out++ = *in++;
|
|
|
|
|
size_out--;
|
|
|
|
|
}
|
|
|
|
|
*out = '\0';
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
void com_strlwr(const char *in, char *out)
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
com_strnlwr(in, out, 99999 );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-12 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
==============
|
|
|
|
|
isdigit
|
|
|
|
|
==============
|
|
|
|
|
*/
|
|
|
|
|
bool com_isdigit( const char *str )
|
|
|
|
|
{
|
|
|
|
|
if( str && *str )
|
|
|
|
|
{
|
|
|
|
|
while( isdigit( *str )) str++;
|
|
|
|
|
if( !*str ) return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-09 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
============
|
2007-11-10 22:00:00 +01:00
|
|
|
|
strlen
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
returned string length
|
|
|
|
|
============
|
|
|
|
|
*/
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int com_strlen( const char *string )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
|
|
if( !string ) return 0;
|
|
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
p = string;
|
|
|
|
|
while( *p )
|
2007-09-13 22:00:00 +02:00
|
|
|
|
{
|
2007-11-09 22:00:00 +01:00
|
|
|
|
p++;
|
|
|
|
|
len++;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
}
|
2007-11-09 22:00:00 +01:00
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
============
|
2007-11-10 22:00:00 +01:00
|
|
|
|
cstrlen
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
skipped color prefixes
|
|
|
|
|
============
|
|
|
|
|
*/
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int com_cstrlen( const char *string )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
|
|
if( !string ) return 0;
|
|
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
|
p = string;
|
|
|
|
|
while( *p )
|
|
|
|
|
{
|
|
|
|
|
if(IsColorString( p ))
|
|
|
|
|
{
|
|
|
|
|
p += 2;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
p++;
|
|
|
|
|
len++;
|
|
|
|
|
}
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
char com_toupper(const char in )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
char out;
|
|
|
|
|
|
|
|
|
|
if (in >= 'a' && in <= 'z')
|
|
|
|
|
out = in + 'A' - 'a';
|
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
char com_tolower(const char in )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
char out;
|
|
|
|
|
|
|
|
|
|
if (in >= 'A' && in <= 'Z')
|
|
|
|
|
out = in + 'a' - 'A';
|
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
|
|
return out;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-17 22:00:00 +01:00
|
|
|
|
size_t com_strncat(char *dst, const char *src, size_t size)
|
2007-09-13 22:00:00 +02:00
|
|
|
|
{
|
|
|
|
|
register char *d = dst;
|
|
|
|
|
register const char *s = src;
|
2007-11-17 22:00:00 +01:00
|
|
|
|
register size_t n = size;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
size_t dlen;
|
|
|
|
|
|
2007-11-17 22:00:00 +01:00
|
|
|
|
if(!dst || !src || !size) return 0;
|
|
|
|
|
|
|
|
|
|
// find the end of dst and adjust bytes left but don't go past end
|
|
|
|
|
while(n-- != 0 && *d != '\0') d++;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
dlen = d - dst;
|
2007-11-17 22:00:00 +01:00
|
|
|
|
n = size - dlen;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
if (n == 0) return(dlen + com_strlen(s));
|
2007-09-13 22:00:00 +02:00
|
|
|
|
while (*s != '\0')
|
|
|
|
|
{
|
|
|
|
|
if (n != 1)
|
|
|
|
|
{
|
|
|
|
|
*d++ = *s;
|
|
|
|
|
n--;
|
|
|
|
|
}
|
|
|
|
|
s++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*d = '\0';
|
2007-11-17 22:00:00 +01:00
|
|
|
|
return(dlen + (s - src)); // count does not include NULL
|
2007-09-13 22:00:00 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
size_t com_strcat(char *dst, const char *src )
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
return com_strncat( dst, src, 99999 );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-17 22:00:00 +01:00
|
|
|
|
size_t com_strncpy(char *dst, const char *src, size_t size)
|
2007-09-13 22:00:00 +02:00
|
|
|
|
{
|
|
|
|
|
register char *d = dst;
|
|
|
|
|
register const char *s = src;
|
2007-11-17 22:00:00 +01:00
|
|
|
|
register size_t n = size;
|
|
|
|
|
|
|
|
|
|
if(!dst || !src || !size) return 0;
|
2007-09-13 22:00:00 +02:00
|
|
|
|
|
2007-11-17 22:00:00 +01:00
|
|
|
|
// copy as many bytes as will fit
|
2007-09-13 22:00:00 +02:00
|
|
|
|
if (n != 0 && --n != 0)
|
|
|
|
|
{
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
if ((*d++ = *s++) == 0)
|
|
|
|
|
break;
|
|
|
|
|
} while (--n != 0);
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-17 22:00:00 +01:00
|
|
|
|
// not enough room in dst, add NULL and traverse rest of src
|
2007-09-13 22:00:00 +02:00
|
|
|
|
if (n == 0)
|
|
|
|
|
{
|
2007-11-17 22:00:00 +01:00
|
|
|
|
if (size != 0) *d = '\0'; // NULL-terminate dst
|
2007-09-13 22:00:00 +02:00
|
|
|
|
while (*s++);
|
|
|
|
|
}
|
2007-11-09 22:00:00 +01:00
|
|
|
|
return(s - src - 1); // count does not include NULL
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
size_t com_strcpy(char *dst, const char *src )
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
return com_strncpy( dst, src, 99999 );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-07-26 22:00:00 +02:00
|
|
|
|
char *com_stralloc( byte *mempool, const char *s, const char *filename, int fileline )
|
2007-11-10 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
char *b;
|
|
|
|
|
|
2008-07-26 22:00:00 +02:00
|
|
|
|
if( !s ) return NULL;
|
|
|
|
|
if( !mempool ) mempool = Sys.stringpool;
|
2007-11-17 22:00:00 +01:00
|
|
|
|
|
2008-08-04 22:00:00 +02:00
|
|
|
|
MsgDev( D_STRING, "new system string %p\n", s );
|
2008-07-26 22:00:00 +02:00
|
|
|
|
b = _mem_alloc( mempool, com_strlen(s) + 1, filename, fileline );
|
2008-07-19 22:00:00 +02:00
|
|
|
|
com_strcpy( b, s );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
return b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int com_atoi(const char *str)
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int val = 0;
|
|
|
|
|
int c, sign;
|
|
|
|
|
|
|
|
|
|
if( !str ) return 0;
|
|
|
|
|
|
|
|
|
|
if(*str == '-')
|
|
|
|
|
{
|
|
|
|
|
sign = -1;
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else sign = 1;
|
|
|
|
|
|
|
|
|
|
// check for hex
|
|
|
|
|
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X') )
|
|
|
|
|
{
|
|
|
|
|
str += 2;
|
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
c = *str++;
|
|
|
|
|
if (c >= '0' && c <= '9') val = (val<<4) + c - '0';
|
|
|
|
|
else if (c >= 'a' && c <= 'f') val = (val<<4) + c - 'a' + 10;
|
|
|
|
|
else if (c >= 'A' && c <= 'F') val = (val<<4) + c - 'A' + 10;
|
|
|
|
|
else return val * sign;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for character
|
|
|
|
|
if (str[0] == '\'') return sign * str[1];
|
|
|
|
|
|
|
|
|
|
// assume decimal
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
c = *str++;
|
|
|
|
|
if (c <'0' || c > '9')
|
|
|
|
|
return val * sign;
|
|
|
|
|
val = val*10 + c - '0';
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
float com_atof(const char *str)
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
double val = 0;
|
|
|
|
|
int c, sign, decimal, total;
|
|
|
|
|
|
|
|
|
|
if( !str ) return 0.0f;
|
|
|
|
|
|
|
|
|
|
if (*str == '-')
|
|
|
|
|
{
|
|
|
|
|
sign = -1;
|
|
|
|
|
str++;
|
|
|
|
|
}
|
|
|
|
|
else sign = 1;
|
|
|
|
|
|
|
|
|
|
// check for hex
|
|
|
|
|
if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
|
|
|
|
{
|
|
|
|
|
str += 2;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
c = *str++;
|
|
|
|
|
if (c >= '0' && c <= '9') val = (val * 16) + c - '0';
|
|
|
|
|
else if (c >= 'a' && c <= 'f') val = (val * 16) + c - 'a' + 10;
|
|
|
|
|
else if (c >= 'A' && c <= 'F') val = (val * 16) + c - 'A' + 10;
|
|
|
|
|
else return val * sign;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for character
|
|
|
|
|
if (str[0] == '\'') return sign * str[1];
|
|
|
|
|
|
|
|
|
|
// assume decimal
|
|
|
|
|
decimal = -1;
|
|
|
|
|
total = 0;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
c = *str++;
|
|
|
|
|
if (c == '.')
|
|
|
|
|
{
|
|
|
|
|
decimal = total;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (c <'0' || c > '9') break;
|
|
|
|
|
val = val*10 + c - '0';
|
|
|
|
|
total++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(decimal == -1) return val * sign;
|
|
|
|
|
while (total > decimal)
|
|
|
|
|
{
|
|
|
|
|
val /= 10;
|
|
|
|
|
total--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return val * sign;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
void com_atov( float *vec, const char *str, size_t siz )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2008-10-27 22:00:00 +01:00
|
|
|
|
string buffer;
|
|
|
|
|
char *pstr, *pfront;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
int j;
|
|
|
|
|
|
2008-10-27 22:00:00 +01:00
|
|
|
|
com.strncpy( buffer, str, MAX_STRING );
|
|
|
|
|
Mem_Set( vec, 0, sizeof(vec_t) * siz );
|
2007-11-09 22:00:00 +01:00
|
|
|
|
pstr = pfront = buffer;
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
for ( j = 0; j < siz; j++ )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2008-11-29 22:00:00 +01:00
|
|
|
|
vec[j] = com.atof( pfront );
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
2008-11-29 22:00:00 +01:00
|
|
|
|
// valid separator is space
|
|
|
|
|
while( *pstr && *pstr != ' ' )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
pstr++;
|
|
|
|
|
|
|
|
|
|
if (!*pstr) break;
|
|
|
|
|
pstr++;
|
|
|
|
|
pfront = pstr;
|
|
|
|
|
}
|
2007-09-13 22:00:00 +02:00
|
|
|
|
}
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
============
|
|
|
|
|
strchr
|
|
|
|
|
|
|
|
|
|
find one charcster in string
|
|
|
|
|
============
|
|
|
|
|
*/
|
2008-08-08 22:00:00 +02:00
|
|
|
|
char *com_strchr( const char *s, char c )
|
2007-11-10 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int len = com_strlen(s);
|
|
|
|
|
|
|
|
|
|
while(len--) if(*++s == c) return(char *)s;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
============
|
2007-11-10 22:00:00 +01:00
|
|
|
|
strrchr
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
find one charcster in string
|
|
|
|
|
============
|
|
|
|
|
*/
|
2008-08-08 22:00:00 +02:00
|
|
|
|
char *com_strrchr( const char *s, char c )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int len = com_strlen(s);
|
2007-11-09 22:00:00 +01:00
|
|
|
|
s += len;
|
|
|
|
|
while(len--) if(*--s == c) return (char *)s;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int com_strnicmp(const char *s1, const char *s2, int n)
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int c1, c2;
|
2007-11-15 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
if( s1 == NULL )
|
|
|
|
|
{
|
|
|
|
|
if ( s2 == NULL ) return 0;
|
|
|
|
|
else return -1;
|
|
|
|
|
}
|
2007-11-25 22:00:00 +01:00
|
|
|
|
else if ( s2 == NULL ) return 1;
|
2007-11-15 22:00:00 +01:00
|
|
|
|
|
2007-11-25 22:00:00 +01:00
|
|
|
|
do {
|
2007-11-09 22:00:00 +01:00
|
|
|
|
c1 = *s1++;
|
|
|
|
|
c2 = *s2++;
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
if(!n--) return 0; // strings are equal until end point
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
if(c1 != c2)
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2007-11-25 22:00:00 +01:00
|
|
|
|
if(c1 >= 'a' && c1 <= 'z') c1 -= ('a' - 'A');
|
|
|
|
|
if(c2 >= 'a' && c2 <= 'z') c2 -= ('a' - 'A');
|
|
|
|
|
if(c1 != c2) return c1 < c2 ? -1 : 1;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
}
|
2007-11-25 22:00:00 +01:00
|
|
|
|
} while (c1);
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
2007-11-25 22:00:00 +01:00
|
|
|
|
// strings are equal
|
|
|
|
|
return 0;
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int com_strncmp (const char *s1, const char *s2, int n)
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
int c1, c2;
|
|
|
|
|
|
|
|
|
|
if( s1 == NULL )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
if ( s2 == NULL ) return 0;
|
|
|
|
|
else return -1;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
}
|
2007-11-25 22:00:00 +01:00
|
|
|
|
else if ( s2 == NULL ) return 1;
|
2007-11-15 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
c1 = *s1++;
|
|
|
|
|
c2 = *s2++;
|
|
|
|
|
|
|
|
|
|
// strings are equal until end point
|
|
|
|
|
if (!n--) return 0;
|
|
|
|
|
if (c1 != c2) return c1 < c2 ? -1 : 1;
|
|
|
|
|
|
|
|
|
|
} while (c1);
|
|
|
|
|
|
|
|
|
|
// strings are equal
|
|
|
|
|
return 0;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-25 22:00:00 +01:00
|
|
|
|
int com_stricmp(const char *s1, const char *s2)
|
|
|
|
|
{
|
|
|
|
|
return com_strnicmp(s1, s2, 99999 );
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int com_strcmp (const char *s1, const char *s2)
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
return com_strncmp(s1, s2, 99999 );
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
2009-08-05 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
==============
|
|
|
|
|
Q_WildCmpAfterStar
|
|
|
|
|
==============
|
|
|
|
|
*/
|
|
|
|
|
static bool com_starcmp( const char *pattern, const char *text )
|
|
|
|
|
{
|
|
|
|
|
char c, c1;
|
|
|
|
|
const char *p = pattern, *t = text;
|
|
|
|
|
|
|
|
|
|
while(( c = *p++ ) == '?' || c == '*' )
|
|
|
|
|
{
|
|
|
|
|
if( c == '?' && *t++ == '\0' )
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( c == '\0' ) return true;
|
|
|
|
|
|
|
|
|
|
for( c1 = ((c == '\\') ? *p : c); ; )
|
|
|
|
|
{
|
|
|
|
|
if( com_tolower( *t ) == c1 && com_stricmpext( p - 1, t ))
|
|
|
|
|
return true;
|
|
|
|
|
if( *t++ == '\0' ) return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
==============
|
|
|
|
|
stricmpext
|
|
|
|
|
==============
|
|
|
|
|
*/
|
|
|
|
|
bool com_stricmpext( const char *pattern, const char *text )
|
|
|
|
|
{
|
|
|
|
|
char c;
|
|
|
|
|
|
|
|
|
|
while( (c = *pattern++) != '\0' )
|
|
|
|
|
{
|
|
|
|
|
switch( c )
|
|
|
|
|
{
|
|
|
|
|
case '?':
|
|
|
|
|
if( *text++ == '\0' )
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case '\\':
|
|
|
|
|
if( com_tolower( *pattern++ ) != com_tolower( *text++ ))
|
|
|
|
|
return false;
|
|
|
|
|
break;
|
|
|
|
|
case '*':
|
|
|
|
|
return com_starcmp( pattern, text );
|
|
|
|
|
default:
|
|
|
|
|
if( com_tolower( c ) != com_tolower( *text++ ))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (*text == '\0');
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-08 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
====================
|
|
|
|
|
timestamp
|
|
|
|
|
====================
|
|
|
|
|
*/
|
2007-11-10 22:00:00 +01:00
|
|
|
|
const char* com_timestamp( int format )
|
2007-11-08 22:00:00 +01:00
|
|
|
|
{
|
2008-10-27 22:00:00 +01:00
|
|
|
|
static string timestamp;
|
|
|
|
|
time_t crt_time;
|
|
|
|
|
const struct tm *crt_tm;
|
|
|
|
|
string timestring;
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2008-10-27 22:00:00 +01:00
|
|
|
|
time( &crt_time );
|
|
|
|
|
crt_tm = localtime( &crt_time );
|
2007-11-08 22:00:00 +01:00
|
|
|
|
switch( format )
|
|
|
|
|
{
|
|
|
|
|
case TIME_FULL:
|
|
|
|
|
// Build the full timestamp (ex: "Apr03 2007 [23:31.55]");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%b%d %Y [%H:%M.%S]", crt_tm);
|
|
|
|
|
break;
|
|
|
|
|
case TIME_DATE_ONLY:
|
|
|
|
|
// Build the date stamp only (ex: "Apr03 2007");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%b%d %Y", crt_tm);
|
|
|
|
|
break;
|
|
|
|
|
case TIME_TIME_ONLY:
|
2007-11-13 22:00:00 +01:00
|
|
|
|
// Build the time stamp only (ex: "23:31.55");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%H:%M.%S", crt_tm);
|
2007-11-08 22:00:00 +01:00
|
|
|
|
break;
|
|
|
|
|
case TIME_NO_SECONDS:
|
2009-09-23 22:00:00 +02:00
|
|
|
|
// Build the time stamp exclude seconds (ex: "13:46");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%H:%M", crt_tm);
|
2007-11-08 22:00:00 +01:00
|
|
|
|
break;
|
2008-10-27 22:00:00 +01:00
|
|
|
|
case TIME_YEAR_ONLY:
|
|
|
|
|
// Build the date stamp year only (ex: "2006");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%Y", crt_tm);
|
|
|
|
|
break;
|
2009-09-23 22:00:00 +02:00
|
|
|
|
case TIME_FILENAME:
|
|
|
|
|
// Build a timestamp that can use for filename (ex: "Nov2006-26 (19.14)");
|
|
|
|
|
strftime(timestring, sizeof (timestring), "%b%Y-%d (%H.%M)", crt_tm);
|
|
|
|
|
break;
|
2008-06-08 22:00:00 +02:00
|
|
|
|
default: return NULL;
|
2007-11-08 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2008-10-27 22:00:00 +01:00
|
|
|
|
com.strncpy( timestamp, timestring, MAX_STRING );
|
2007-11-08 22:00:00 +01:00
|
|
|
|
return timestamp;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-31 22:00:00 +02:00
|
|
|
|
/*
|
|
|
|
|
============
|
|
|
|
|
strstr
|
|
|
|
|
|
|
|
|
|
search case - sensitive for string2 in string
|
|
|
|
|
============
|
|
|
|
|
*/
|
|
|
|
|
char *com_strstr( const char *string, const char *string2 )
|
|
|
|
|
{
|
|
|
|
|
return strstr( string, string2 ); //FIXME
|
|
|
|
|
}
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
2007-11-08 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
============
|
2007-11-10 22:00:00 +01:00
|
|
|
|
stristr
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2007-11-09 22:00:00 +01:00
|
|
|
|
search case - insensitive for string2 in string
|
2007-11-08 22:00:00 +01:00
|
|
|
|
============
|
|
|
|
|
*/
|
2007-11-10 22:00:00 +01:00
|
|
|
|
char *com_stristr( const char *string, const char *string2 )
|
2007-11-08 22:00:00 +01:00
|
|
|
|
{
|
2007-11-09 22:00:00 +01:00
|
|
|
|
int c, len;
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2007-11-09 22:00:00 +01:00
|
|
|
|
if (!string || !string2) return NULL;
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
c = com_tolower( *string2 );
|
|
|
|
|
len = com_strlen( string2 );
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
while (string)
|
|
|
|
|
{
|
2007-11-10 22:00:00 +01:00
|
|
|
|
for ( ; *string && com_tolower( *string ) != c; string++ );
|
2007-11-09 22:00:00 +01:00
|
|
|
|
if (*string)
|
|
|
|
|
{
|
2007-11-10 22:00:00 +01:00
|
|
|
|
if(!com_strnicmp( string, string2, len ))
|
2007-11-09 22:00:00 +01:00
|
|
|
|
break;
|
|
|
|
|
string++;
|
|
|
|
|
}
|
|
|
|
|
else return NULL;
|
|
|
|
|
}
|
|
|
|
|
return (char *)string;
|
|
|
|
|
}
|
2007-11-08 22:00:00 +01:00
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
size_t com_strpack( byte *buffer, size_t pos, char *string, int n )
|
|
|
|
|
{
|
|
|
|
|
if(!buffer || !string) return 0;
|
|
|
|
|
|
|
|
|
|
n++; // get space for terminator
|
|
|
|
|
|
|
|
|
|
com_strncpy(buffer + pos, string, n );
|
|
|
|
|
return pos + n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t com_strunpack( byte *buffer, size_t pos, char *string )
|
|
|
|
|
{
|
|
|
|
|
int n = 0;
|
|
|
|
|
char *in;
|
|
|
|
|
|
|
|
|
|
if(!buffer || !string) return 0;
|
|
|
|
|
in = buffer + pos;
|
|
|
|
|
|
|
|
|
|
do { in++, n++; } while(*in != '\0' && in != NULL );
|
|
|
|
|
|
|
|
|
|
com_strncpy( string, in - (n - 1), n );
|
|
|
|
|
return pos + n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int com_vsnprintf(char *buffer, size_t buffersize, const char *format, va_list args)
|
2007-11-08 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
result = _vsnprintf (buffer, buffersize, format, args);
|
|
|
|
|
if (result < 0 || (size_t)result >= buffersize)
|
|
|
|
|
{
|
|
|
|
|
buffer[buffersize - 1] = '\0';
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-10 22:00:00 +01:00
|
|
|
|
int com_vsprintf(char *buffer, const char *format, va_list args)
|
|
|
|
|
{
|
2007-11-15 22:00:00 +01:00
|
|
|
|
return com_vsnprintf(buffer, 99999, format, args);
|
2007-11-10 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int com_snprintf(char *buffer, size_t buffersize, const char *format, ...)
|
2007-11-08 22:00:00 +01:00
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
2007-11-10 22:00:00 +01:00
|
|
|
|
result = com_vsnprintf (buffer, buffersize, format, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int com_sprintf(char *buffer, const char *format, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
int result;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
2007-11-15 22:00:00 +01:00
|
|
|
|
result = com_vsnprintf (buffer, 99999, format, args);
|
2007-11-08 22:00:00 +01:00
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
return result;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2007-11-14 22:00:00 +01:00
|
|
|
|
char *com_pretifymem( float value, int digitsafterdecimal )
|
|
|
|
|
{
|
|
|
|
|
static char output[8][32];
|
|
|
|
|
static int current;
|
|
|
|
|
|
|
|
|
|
float onekb = 1024.0f;
|
|
|
|
|
float onemb = onekb * onekb;
|
|
|
|
|
char suffix[8];
|
|
|
|
|
char *out = output[current];
|
|
|
|
|
char val[32], *i, *o, *dot;
|
|
|
|
|
int pos;
|
|
|
|
|
|
|
|
|
|
current = ( current + 1 ) & ( 8 - 1 );
|
|
|
|
|
|
|
|
|
|
// first figure out which bin to use
|
|
|
|
|
if ( value > onemb )
|
|
|
|
|
{
|
|
|
|
|
value /= onemb;
|
|
|
|
|
com_sprintf( suffix, " Mb" );
|
|
|
|
|
}
|
|
|
|
|
else if ( value > onekb )
|
|
|
|
|
{
|
|
|
|
|
value /= onekb;
|
|
|
|
|
com_sprintf( suffix, " Kb" );
|
|
|
|
|
}
|
|
|
|
|
else com_sprintf( suffix, " bytes" );
|
|
|
|
|
|
|
|
|
|
// clamp to >= 0
|
|
|
|
|
digitsafterdecimal = max( digitsafterdecimal, 0 );
|
|
|
|
|
// if it's basically integral, don't do any decimals
|
|
|
|
|
if(fabs( value - (int)value ) < 0.00001)
|
|
|
|
|
{
|
|
|
|
|
com_sprintf( val, "%i%s", (int)value, suffix );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
char fmt[32];
|
|
|
|
|
|
|
|
|
|
// otherwise, create a format string for the decimals
|
|
|
|
|
com_sprintf( fmt, "%%.%if%s", digitsafterdecimal, suffix );
|
|
|
|
|
com_sprintf( val, fmt, value );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// copy from in to out
|
|
|
|
|
i = val;
|
|
|
|
|
o = out;
|
|
|
|
|
|
|
|
|
|
// search for decimal or if it was integral, find the space after the raw number
|
|
|
|
|
dot = strstr( i, "." );
|
|
|
|
|
if ( !dot ) dot = strstr( i, " " );
|
|
|
|
|
pos = dot - i; // compute position of dot
|
|
|
|
|
pos -= 3; // don't put a comma if it's <= 3 long
|
|
|
|
|
|
|
|
|
|
while ( *i )
|
|
|
|
|
{
|
|
|
|
|
// if pos is still valid then insert a comma every third digit, except if we would be
|
|
|
|
|
// putting one in the first spot
|
|
|
|
|
if ( pos >= 0 && !( pos % 3 ))
|
|
|
|
|
{
|
|
|
|
|
// never in first spot
|
|
|
|
|
if ( o != out ) *o++ = ',';
|
|
|
|
|
}
|
|
|
|
|
pos--; // count down comma position
|
|
|
|
|
*o++ = *i++; // copy rest of data as normal
|
|
|
|
|
}
|
|
|
|
|
*o = 0; // terminate
|
|
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2007-11-09 22:00:00 +01:00
|
|
|
|
/*
|
|
|
|
|
============
|
|
|
|
|
va
|
|
|
|
|
|
|
|
|
|
does a varargs printf into a temp buffer, so I don't need to have
|
|
|
|
|
varargs versions of all text functions.
|
|
|
|
|
FIXME: make this buffer size safe someday
|
|
|
|
|
============
|
|
|
|
|
*/
|
2008-11-13 22:00:00 +01:00
|
|
|
|
char *va( const char *format, ... )
|
2007-11-09 22:00:00 +01:00
|
|
|
|
{
|
2009-07-27 22:00:00 +02:00
|
|
|
|
va_list argptr;
|
|
|
|
|
static char string[256][1024], *s; // g-cont. 256 temporary strings should be enough...
|
|
|
|
|
static int stringindex = 0;
|
2007-11-09 22:00:00 +01:00
|
|
|
|
|
|
|
|
|
s = string[stringindex];
|
2009-07-27 22:00:00 +02:00
|
|
|
|
stringindex = (stringindex + 1) & 255;
|
2008-11-13 22:00:00 +01:00
|
|
|
|
va_start( argptr, format );
|
|
|
|
|
com_vsnprintf( s, sizeof(string[0]), format, argptr );
|
|
|
|
|
va_end( argptr );
|
2007-11-09 22:00:00 +01:00
|
|
|
|
return s;
|
2007-11-08 22:00:00 +01:00
|
|
|
|
}
|