2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
crtlib.c - internal stdlib
|
|
|
|
Copyright (C) 2011 Uncle Mike
|
2023-04-26 03:03:23 +02:00
|
|
|
Copyright (c) QuakeSpasm contributors
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
*/
|
2019-03-16 05:15:06 +01:00
|
|
|
#include "port.h"
|
|
|
|
#include "xash3d_types.h"
|
|
|
|
#include "const.h"
|
2018-04-13 18:23:45 +02:00
|
|
|
#include <math.h>
|
2018-04-13 19:14:20 +02:00
|
|
|
#include <stdarg.h>
|
2018-05-01 16:37:25 +02:00
|
|
|
#include <ctype.h>
|
2019-03-16 05:15:06 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "crtlib.h"
|
2021-12-23 17:17:11 +01:00
|
|
|
#include "xash3d_mathlib.h"
|
2019-10-10 03:21:50 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
void Q_strnlwr( const char *in, char *out, size_t size_out )
|
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Q_isdigit( const char *str )
|
|
|
|
{
|
|
|
|
if( str && *str )
|
|
|
|
{
|
|
|
|
while( isdigit( *str )) str++;
|
|
|
|
if( !*str ) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-04 00:22:10 +01:00
|
|
|
qboolean Q_isspace( const char *str )
|
|
|
|
{
|
|
|
|
if( str && *str )
|
|
|
|
{
|
|
|
|
while( isspace( *str ) ) str++;
|
|
|
|
if( !*str ) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-10 11:18:23 +01:00
|
|
|
size_t Q_colorstr( const char *string )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-03-10 11:18:23 +01:00
|
|
|
size_t len;
|
2018-04-13 18:23:45 +02:00
|
|
|
const char *p;
|
|
|
|
|
|
|
|
if( !string ) return 0;
|
|
|
|
|
|
|
|
len = 0;
|
|
|
|
p = string;
|
|
|
|
while( *p )
|
|
|
|
{
|
|
|
|
if( IsColorString( p ))
|
|
|
|
{
|
|
|
|
len += 2;
|
|
|
|
p += 2;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
char Q_toupper( const char in )
|
|
|
|
{
|
|
|
|
char out;
|
|
|
|
|
|
|
|
if( in >= 'a' && in <= 'z' )
|
|
|
|
out = in + 'A' - 'a';
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
char Q_tolower( const char in )
|
|
|
|
{
|
|
|
|
char out;
|
|
|
|
|
|
|
|
if( in >= 'A' && in <= 'Z' )
|
|
|
|
out = in + 'a' - 'A';
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Q_strncat( char *dst, const char *src, size_t size )
|
|
|
|
{
|
|
|
|
register char *d = dst;
|
|
|
|
register const char *s = src;
|
|
|
|
register size_t n = size;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
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++;
|
|
|
|
dlen = d - dst;
|
|
|
|
n = size - dlen;
|
|
|
|
|
|
|
|
if( n == 0 ) return( dlen + Q_strlen( s ));
|
|
|
|
|
|
|
|
while( *s != '\0' )
|
|
|
|
{
|
|
|
|
if( n != 1 )
|
|
|
|
{
|
|
|
|
*d++ = *s;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = '\0';
|
|
|
|
return( dlen + ( s - src )); // count does not include NULL
|
|
|
|
}
|
|
|
|
|
|
|
|
int Q_atoi( const char *str )
|
|
|
|
{
|
2019-07-19 19:23:08 +02:00
|
|
|
int val = 0;
|
|
|
|
int c, sign;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( !str ) return 0;
|
|
|
|
|
|
|
|
// check for empty charachters in string
|
|
|
|
while( str && *str == ' ' )
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if( !str ) return 0;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( *str == '-' )
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
else sign = 1;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// check for character
|
|
|
|
if( str[0] == '\'' )
|
|
|
|
return sign * str[1];
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// assume decimal
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = *str++;
|
|
|
|
if( c < '0' || c > '9' )
|
|
|
|
return val * sign;
|
|
|
|
val = val * 10 + c - '0';
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Q_atof( const char *str )
|
|
|
|
{
|
|
|
|
double val = 0;
|
|
|
|
int c, sign, decimal, total;
|
|
|
|
|
|
|
|
if( !str ) return 0.0f;
|
|
|
|
|
|
|
|
// check for empty charachters in string
|
|
|
|
while( str && *str == ' ' )
|
|
|
|
str++;
|
|
|
|
|
|
|
|
if( !str ) return 0.0f;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
if( *str == '-' )
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
else sign = 1;
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// check for character
|
|
|
|
if( str[0] == '\'' ) return sign * str[1];
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// 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--;
|
|
|
|
}
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
return val * sign;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Q_atov( float *vec, const char *str, size_t siz )
|
|
|
|
{
|
|
|
|
string buffer;
|
|
|
|
char *pstr, *pfront;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
Q_strncpy( buffer, str, sizeof( buffer ));
|
|
|
|
memset( vec, 0, sizeof( vec_t ) * siz );
|
|
|
|
pstr = pfront = buffer;
|
|
|
|
|
|
|
|
for( j = 0; j < siz; j++ )
|
|
|
|
{
|
|
|
|
vec[j] = Q_atof( pfront );
|
|
|
|
|
|
|
|
// valid separator is space
|
|
|
|
while( *pstr && *pstr != ' ' )
|
|
|
|
pstr++;
|
|
|
|
|
|
|
|
if( !*pstr ) break;
|
|
|
|
pstr++;
|
|
|
|
pfront = pstr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static qboolean Q_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( Q_tolower( *t ) == c1 && Q_stricmpext( p - 1, t ))
|
|
|
|
return true;
|
|
|
|
if( *t++ == '\0' ) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 18:14:52 +02:00
|
|
|
qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlength )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2022-08-25 18:14:52 +02:00
|
|
|
size_t i = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
char c;
|
|
|
|
|
|
|
|
while(( c = *pattern++ ) != '\0' )
|
|
|
|
{
|
2022-08-25 18:14:52 +02:00
|
|
|
i++;
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
switch( c )
|
|
|
|
{
|
|
|
|
case '?':
|
|
|
|
if( *text++ == '\0' )
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
if( Q_tolower( *pattern++ ) != Q_tolower( *text++ ))
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
return Q_starcmp( pattern, text );
|
|
|
|
default:
|
|
|
|
if( Q_tolower( c ) != Q_tolower( *text++ ))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 18:14:52 +02:00
|
|
|
return ( *text == '\0' ) || i == minimumlength;
|
|
|
|
}
|
|
|
|
|
|
|
|
qboolean Q_stricmpext( const char *pattern, const char *text )
|
|
|
|
{
|
|
|
|
return Q_strnicmpext( pattern, text, ~((size_t)0) );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-03 04:58:58 +01:00
|
|
|
const byte *Q_memmem( const byte *haystack, size_t haystacklen, const byte *needle, size_t needlelen )
|
|
|
|
{
|
|
|
|
const byte *i;
|
|
|
|
|
|
|
|
// quickly find first matching symbol
|
|
|
|
while( haystacklen && ( i = memchr( haystack, needle[0], haystacklen )))
|
|
|
|
{
|
|
|
|
if( !memcmp( i, needle, needlelen ))
|
|
|
|
return i;
|
|
|
|
|
2023-01-04 15:16:27 +01:00
|
|
|
// skip one byte
|
|
|
|
i++;
|
|
|
|
|
2023-01-03 04:58:58 +01:00
|
|
|
haystacklen -= i - haystack;
|
2023-01-04 15:16:27 +01:00
|
|
|
haystack = i;
|
2023-01-03 04:58:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
const char* Q_timestamp( int format )
|
|
|
|
{
|
|
|
|
static string timestamp;
|
|
|
|
time_t crt_time;
|
|
|
|
const struct tm *crt_tm;
|
|
|
|
string timestring;
|
|
|
|
|
|
|
|
time( &crt_time );
|
|
|
|
crt_tm = localtime( &crt_time );
|
|
|
|
|
|
|
|
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:
|
|
|
|
// Build the time stamp only (ex: "23:31.55");
|
|
|
|
strftime( timestring, sizeof( timestring ), "%H:%M.%S", crt_tm );
|
|
|
|
break;
|
|
|
|
case TIME_NO_SECONDS:
|
|
|
|
// Build the time stamp exclude seconds (ex: "13:46");
|
|
|
|
strftime( timestring, sizeof( timestring ), "%H:%M", crt_tm );
|
|
|
|
break;
|
|
|
|
case TIME_YEAR_ONLY:
|
|
|
|
// Build the date stamp year only (ex: "2006");
|
|
|
|
strftime( timestring, sizeof( timestring ), "%Y", crt_tm );
|
|
|
|
break;
|
|
|
|
case TIME_FILENAME:
|
|
|
|
// Build a timestamp that can use for filename (ex: "Nov2006-26 (19.14.28)");
|
|
|
|
strftime( timestring, sizeof( timestring ), "%b%Y-%d_%H.%M.%S", crt_tm );
|
|
|
|
break;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Q_strncpy( timestamp, timestring, sizeof( timestamp ));
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:39:18 +02:00
|
|
|
#if !defined( HAVE_STRCASESTR )
|
2022-06-29 02:56:42 +02:00
|
|
|
char *Q_stristr( const char *string, const char *string2 )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2021-03-10 11:18:23 +01:00
|
|
|
int c;
|
|
|
|
size_t len;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
if( !string || !string2 ) return NULL;
|
|
|
|
|
|
|
|
c = Q_tolower( *string2 );
|
|
|
|
len = Q_strlen( string2 );
|
|
|
|
|
|
|
|
while( string )
|
|
|
|
{
|
|
|
|
for( ; *string && Q_tolower( *string ) != c; string++ );
|
|
|
|
|
|
|
|
if( *string )
|
|
|
|
{
|
|
|
|
if( !Q_strnicmp( string, string2, len ))
|
|
|
|
break;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
else return NULL;
|
|
|
|
}
|
2022-06-29 02:56:42 +02:00
|
|
|
return (char *)string;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2022-06-29 01:39:18 +02:00
|
|
|
#endif // !defined( HAVE_STRCASESTR )
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args )
|
|
|
|
{
|
2021-03-10 11:38:36 +01:00
|
|
|
int result;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2018-04-26 02:23:00 +02:00
|
|
|
#ifndef _MSC_VER
|
2018-04-13 19:14:20 +02:00
|
|
|
result = vsnprintf( buffer, buffersize, format, args );
|
2018-04-26 02:23:00 +02:00
|
|
|
#else
|
2018-04-13 18:23:45 +02:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
result = _vsnprintf( buffer, buffersize, format, args );
|
|
|
|
}
|
|
|
|
|
|
|
|
// to prevent crash while output
|
|
|
|
__except( EXCEPTION_EXECUTE_HANDLER )
|
|
|
|
{
|
2018-04-26 02:09:36 +02:00
|
|
|
Q_strncpy( buffer, "^1sprintf throw exception^7\n", buffersize );
|
|
|
|
result = buffersize;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2018-04-26 02:23:00 +02:00
|
|
|
#endif
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2019-10-18 06:23:34 +02:00
|
|
|
if( result >= buffersize )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
buffer[buffersize - 1] = '\0';
|
|
|
|
return -1;
|
|
|
|
}
|
2018-04-13 19:14:20 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Q_snprintf( char *buffer, size_t buffersize, const char *format, ... )
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start( args, format );
|
|
|
|
result = Q_vsnprintf( buffer, buffersize, format, args );
|
|
|
|
va_end( args );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-06-10 17:43:04 +02:00
|
|
|
void COM_StripColors( const char *in, char *out )
|
2022-06-10 10:20:58 +02:00
|
|
|
{
|
|
|
|
while ( *in )
|
|
|
|
{
|
|
|
|
if ( IsColorString( in ) )
|
|
|
|
in += 2;
|
|
|
|
else *out++ = *in++;
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
char *Q_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;
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_strncpy( suffix, " Mb", sizeof( suffix ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else if( value > onekb )
|
|
|
|
{
|
|
|
|
value /= onekb;
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_strncpy( suffix, " Kb", sizeof( suffix ));
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
2023-04-23 21:56:05 +02:00
|
|
|
else Q_strncpy( suffix, " bytes", sizeof( suffix ));
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// clamp to >= 0
|
2021-12-23 17:17:11 +01:00
|
|
|
digitsafterdecimal = Q_max( digitsafterdecimal, 0 );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
// if it's basically integral, don't do any decimals
|
2019-10-10 03:21:50 +02:00
|
|
|
if( fabs( value - (int)value ) < 0.00001f )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_snprintf( val, sizeof( val ), "%i%s", (int)value, suffix );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char fmt[32];
|
|
|
|
|
|
|
|
// otherwise, create a format string for the decimals
|
2023-04-23 21:56:05 +02:00
|
|
|
Q_snprintf( fmt, sizeof( fmt ), "%%.%if%s", digitsafterdecimal, suffix );
|
|
|
|
Q_snprintf( val, sizeof( val ), fmt, (double)value );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy from in to out
|
|
|
|
i = val;
|
|
|
|
o = out;
|
|
|
|
|
|
|
|
// search for decimal or if it was integral, find the space after the raw number
|
2022-11-18 17:21:17 +01:00
|
|
|
dot = Q_strchr( i, '.' );
|
|
|
|
if( !dot ) dot = Q_strchr( i, ' ' );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:23:33 +01:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_FileBase
|
|
|
|
|
|
|
|
Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
|
2023-04-26 03:03:23 +02:00
|
|
|
a1ba: adapted and simplified version from QuakeSpasm
|
2019-03-06 14:23:33 +01:00
|
|
|
============
|
|
|
|
*/
|
2023-04-26 03:03:23 +02:00
|
|
|
void COM_FileBase( const char *in, char *out, size_t size )
|
2019-03-06 14:23:33 +01:00
|
|
|
{
|
2023-04-26 03:03:23 +02:00
|
|
|
const char *dot, *slash, *s;
|
|
|
|
size_t len;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
if( unlikely( !COM_CheckString( in ) || size <= 1 ))
|
|
|
|
{
|
|
|
|
out[0] = 0;
|
|
|
|
return;
|
|
|
|
}
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
slash = in;
|
|
|
|
dot = NULL;
|
|
|
|
for( s = in; *s; s++ )
|
|
|
|
{
|
|
|
|
if( *s == '/' || *s == '\\' )
|
|
|
|
slash = s + 1;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
if( *s == '.' )
|
|
|
|
dot = s;
|
|
|
|
}
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
if( dot == NULL || dot < slash )
|
|
|
|
dot = s;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
len = Q_min( size - 1, dot - slash );
|
2019-03-06 14:23:33 +01:00
|
|
|
|
2023-04-26 03:03:23 +02:00
|
|
|
memcpy( out, slash, len );
|
2019-03-06 14:23:33 +01:00
|
|
|
out[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_FileExtension
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
const char *COM_FileExtension( const char *in )
|
|
|
|
{
|
|
|
|
const char *separator, *backslash, *colon, *dot;
|
|
|
|
|
|
|
|
separator = Q_strrchr( in, '/' );
|
|
|
|
backslash = Q_strrchr( in, '\\' );
|
|
|
|
|
|
|
|
if( !separator || separator < backslash )
|
|
|
|
separator = backslash;
|
|
|
|
|
|
|
|
colon = Q_strrchr( in, ':' );
|
|
|
|
|
|
|
|
if( !separator || separator < colon )
|
|
|
|
separator = colon;
|
|
|
|
|
|
|
|
dot = Q_strrchr( in, '.' );
|
|
|
|
|
|
|
|
if( dot == NULL || ( separator && ( dot < separator )))
|
|
|
|
return "";
|
|
|
|
|
|
|
|
return dot + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_FileWithoutPath
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
const char *COM_FileWithoutPath( const char *in )
|
|
|
|
{
|
|
|
|
const char *separator, *backslash, *colon;
|
|
|
|
|
|
|
|
separator = Q_strrchr( in, '/' );
|
|
|
|
backslash = Q_strrchr( in, '\\' );
|
|
|
|
|
|
|
|
if( !separator || separator < backslash )
|
|
|
|
separator = backslash;
|
|
|
|
|
|
|
|
colon = Q_strrchr( in, ':' );
|
|
|
|
|
|
|
|
if( !separator || separator < colon )
|
|
|
|
separator = colon;
|
|
|
|
|
|
|
|
return separator ? separator + 1 : in;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_ExtractFilePath
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_ExtractFilePath( const char *path, char *dest )
|
|
|
|
{
|
2023-10-28 18:31:17 +02:00
|
|
|
const char *src = path + Q_strlen( path ) - 1;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
|
|
|
// back up until a \ or the start
|
2023-10-28 18:31:17 +02:00
|
|
|
while( src > path && !(*(src - 1) == '\\' || *(src - 1) == '/' ))
|
2019-03-06 14:23:33 +01:00
|
|
|
src--;
|
|
|
|
|
2023-10-28 18:31:17 +02:00
|
|
|
if( src > path )
|
2019-03-06 14:23:33 +01:00
|
|
|
{
|
|
|
|
memcpy( dest, path, src - path );
|
|
|
|
dest[src - path - 1] = 0; // cutoff backslash
|
|
|
|
}
|
2022-11-18 17:21:17 +01:00
|
|
|
else dest[0] = 0; // file without path
|
2019-03-06 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_StripExtension
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_StripExtension( char *path )
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
|
2021-02-14 22:33:41 +01:00
|
|
|
length = Q_strlen( path );
|
|
|
|
|
|
|
|
if( length > 0 )
|
|
|
|
length--;
|
|
|
|
|
2019-03-06 14:23:33 +01:00
|
|
|
while( length > 0 && path[length] != '.' )
|
|
|
|
{
|
|
|
|
length--;
|
|
|
|
if( path[length] == '/' || path[length] == '\\' || path[length] == ':' )
|
|
|
|
return; // no extension
|
|
|
|
}
|
|
|
|
|
|
|
|
if( length ) path[length] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
COM_DefaultExtension
|
|
|
|
==================
|
|
|
|
*/
|
2023-04-26 03:57:04 +02:00
|
|
|
void COM_DefaultExtension( char *path, const char *extension, size_t size )
|
2019-03-06 14:23:33 +01:00
|
|
|
{
|
|
|
|
const char *src;
|
2022-11-18 17:21:17 +01:00
|
|
|
size_t len;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
|
|
|
// if path doesn't have a .EXT, append extension
|
|
|
|
// (extension should include the .)
|
2022-11-18 17:21:17 +01:00
|
|
|
len = Q_strlen( path );
|
|
|
|
src = path + len - 1;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
|
|
|
while( *src != '/' && src != path )
|
|
|
|
{
|
|
|
|
// it has an extension
|
|
|
|
if( *src == '.' ) return;
|
|
|
|
src--;
|
|
|
|
}
|
|
|
|
|
2023-04-26 03:57:04 +02:00
|
|
|
Q_strncpy( &path[len], extension, size - len );
|
2019-03-06 14:23:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==================
|
|
|
|
COM_ReplaceExtension
|
|
|
|
==================
|
|
|
|
*/
|
2023-04-26 03:57:04 +02:00
|
|
|
void COM_ReplaceExtension( char *path, const char *extension, size_t size )
|
2019-03-06 14:23:33 +01:00
|
|
|
{
|
|
|
|
COM_StripExtension( path );
|
2023-04-26 03:57:04 +02:00
|
|
|
COM_DefaultExtension( path, extension, size );
|
2019-03-06 14:23:33 +01:00
|
|
|
}
|
2019-07-09 15:36:15 +02:00
|
|
|
|
2020-03-04 05:08:14 +01:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_RemoveLineFeed
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_RemoveLineFeed( char *str )
|
|
|
|
{
|
|
|
|
while( *str != '\0' )
|
|
|
|
{
|
|
|
|
if( *str == '\r' || *str == '\n' )
|
|
|
|
*str = '\0';
|
|
|
|
|
|
|
|
++str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-01 18:37:21 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_FixSlashes
|
|
|
|
|
2023-01-05 04:06:07 +01:00
|
|
|
Changes all '\' characters into '/' characters, in place.
|
2022-07-01 18:37:21 +02:00
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_FixSlashes( char *pname )
|
|
|
|
{
|
2023-01-05 04:06:07 +01:00
|
|
|
for( ; *pname; pname++ )
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2023-01-05 04:06:07 +01:00
|
|
|
if( *pname == '\\' )
|
|
|
|
*pname = '/';
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:08:14 +01:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_PathSlashFix
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_PathSlashFix( char *path )
|
|
|
|
{
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = Q_strlen( path );
|
|
|
|
|
2021-07-02 18:27:04 +02:00
|
|
|
if( path[len - 1] != '\\' && path[len - 1] != '/' )
|
2023-04-24 01:18:40 +02:00
|
|
|
{
|
|
|
|
path[len] = '/';
|
|
|
|
path[len + 1] = '\0';
|
|
|
|
}
|
2020-03-04 05:08:14 +01:00
|
|
|
}
|
|
|
|
|
2020-09-03 17:58:57 +02:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_Hex2Char
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
char COM_Hex2Char( uint8_t hex )
|
|
|
|
{
|
|
|
|
if( hex >= 0x0 && hex <= 0x9 )
|
|
|
|
hex += '0';
|
|
|
|
else if( hex >= 0xA && hex <= 0xF )
|
|
|
|
hex += '7';
|
|
|
|
|
|
|
|
return (char)hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_Hex2String
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_Hex2String( uint8_t hex, char *str )
|
|
|
|
{
|
|
|
|
*str++ = COM_Hex2Char( hex >> 4 );
|
|
|
|
*str++ = COM_Hex2Char( hex & 0x0F );
|
|
|
|
*str = '\0';
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:58:03 +02:00
|
|
|
/*
|
|
|
|
==============
|
|
|
|
COM_IsSingleChar
|
|
|
|
|
|
|
|
interpert this character as single
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
static int COM_IsSingleChar( unsigned int flags, char c )
|
|
|
|
{
|
|
|
|
if( c == '{' || c == '}' || c == '\'' || c == ',' )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !FBitSet( flags, PFILE_IGNOREBRACKET ) && ( c == ')' || c == '(' ))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( FBitSet( flags, PFILE_HANDLECOLON ) && c == ':' )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
==============
|
|
|
|
COM_ParseFile
|
|
|
|
|
|
|
|
text parser
|
|
|
|
==============
|
|
|
|
*/
|
2022-07-10 23:34:48 +02:00
|
|
|
char *COM_ParseFileSafe( char *data, char *token, const int size, unsigned int flags, int *plen, qboolean *quoted )
|
2021-10-01 18:58:03 +02:00
|
|
|
{
|
|
|
|
int c, len = 0;
|
|
|
|
qboolean overflow = false;
|
|
|
|
|
2022-07-10 23:34:48 +02:00
|
|
|
if( quoted )
|
|
|
|
*quoted = false;
|
|
|
|
|
2021-10-01 18:58:03 +02:00
|
|
|
if( !token || !size )
|
|
|
|
{
|
|
|
|
if( plen ) *plen = 0;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
token[0] = 0;
|
|
|
|
|
|
|
|
if( !data )
|
|
|
|
return NULL;
|
|
|
|
// skip whitespace
|
|
|
|
skipwhite:
|
|
|
|
while(( c = ((byte)*data)) <= ' ' )
|
|
|
|
{
|
|
|
|
if( c == 0 )
|
|
|
|
{
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
return NULL; // end of file;
|
|
|
|
}
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip // comments
|
|
|
|
if( c == '/' && data[1] == '/' )
|
|
|
|
{
|
|
|
|
while( *data && *data != '\n' )
|
|
|
|
data++;
|
|
|
|
goto skipwhite;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle quoted strings specially
|
|
|
|
if( c == '\"' )
|
|
|
|
{
|
2022-07-10 23:34:48 +02:00
|
|
|
if( quoted )
|
|
|
|
*quoted = true;
|
|
|
|
|
2021-10-01 18:58:03 +02:00
|
|
|
data++;
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
c = (byte)*data;
|
|
|
|
|
|
|
|
// unexpected line end
|
|
|
|
if( !c )
|
|
|
|
{
|
|
|
|
token[len] = 0;
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
data++;
|
|
|
|
|
|
|
|
if( c == '\\' && *data == '"' )
|
|
|
|
{
|
|
|
|
if( len + 1 < size )
|
|
|
|
{
|
|
|
|
token[len] = (byte)*data;
|
|
|
|
len++;
|
|
|
|
}
|
2021-10-01 19:38:52 +02:00
|
|
|
else overflow = true;
|
2021-10-01 18:58:03 +02:00
|
|
|
|
|
|
|
data++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( c == '\"' )
|
|
|
|
{
|
|
|
|
token[len] = 0;
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( len + 1 < size )
|
|
|
|
{
|
|
|
|
token[len] = c;
|
|
|
|
len++;
|
|
|
|
}
|
2021-10-01 19:38:52 +02:00
|
|
|
else overflow = true;
|
2021-10-01 18:58:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse single characters
|
|
|
|
if( COM_IsSingleChar( flags, c ))
|
|
|
|
{
|
|
|
|
if( size >= 2 ) // char and \0
|
|
|
|
{
|
|
|
|
token[len] = c;
|
|
|
|
len++;
|
|
|
|
token[len] = 0;
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
return data + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// couldn't pass anything
|
|
|
|
token[0] = 0;
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a regular word
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( len + 1 < size )
|
|
|
|
{
|
|
|
|
token[len] = c;
|
|
|
|
len++;
|
|
|
|
}
|
2021-10-01 19:38:52 +02:00
|
|
|
else overflow = true;
|
2021-10-01 18:58:03 +02:00
|
|
|
|
|
|
|
data++;
|
|
|
|
c = ((byte)*data);
|
|
|
|
|
|
|
|
if( COM_IsSingleChar( flags, c ))
|
|
|
|
break;
|
|
|
|
} while( c > 32 );
|
|
|
|
|
|
|
|
token[len] = 0;
|
|
|
|
|
|
|
|
if( plen ) *plen = overflow ? -1 : len;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:36:15 +02:00
|
|
|
int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive )
|
|
|
|
{
|
2023-07-05 04:09:15 +02:00
|
|
|
const char *separators = "/\\:";
|
|
|
|
|
|
|
|
if( !Q_strcmp( pattern, "*" ))
|
|
|
|
separators = "";
|
|
|
|
|
|
|
|
return matchpattern_with_separator( in, pattern, caseinsensitive, separators, false );
|
2019-07-09 15:36:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// wildcard_least_one: if true * matches 1 or more characters
|
|
|
|
// if false * matches 0 or more characters
|
|
|
|
int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one )
|
|
|
|
{
|
|
|
|
int c1, c2;
|
|
|
|
|
|
|
|
while( *pattern )
|
|
|
|
{
|
|
|
|
switch( *pattern )
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return 1; // end of pattern
|
|
|
|
case '?': // match any single character
|
|
|
|
if( *in == 0 || Q_strchr( separators, *in ))
|
|
|
|
return 0; // no match
|
|
|
|
in++;
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
case '*': // match anything until following string
|
|
|
|
if( wildcard_least_one )
|
|
|
|
{
|
|
|
|
if( *in == 0 || Q_strchr( separators, *in ))
|
|
|
|
return 0; // no match
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
while( *in )
|
|
|
|
{
|
|
|
|
if( Q_strchr(separators, *in ))
|
|
|
|
break;
|
|
|
|
// see if pattern matches at this offset
|
|
|
|
if( matchpattern_with_separator(in, pattern, caseinsensitive, separators, wildcard_least_one ))
|
|
|
|
return 1;
|
|
|
|
// nope, advance to next offset
|
|
|
|
in++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if( *in != *pattern )
|
|
|
|
{
|
|
|
|
if( !caseinsensitive )
|
|
|
|
return 0; // no match
|
|
|
|
c1 = *in;
|
|
|
|
if( c1 >= 'A' && c1 <= 'Z' )
|
|
|
|
c1 += 'a' - 'A';
|
|
|
|
c2 = *pattern;
|
|
|
|
if( c2 >= 'A' && c2 <= 'Z' )
|
|
|
|
c2 += 'a' - 'A';
|
|
|
|
if( c1 != c2 )
|
|
|
|
return 0; // no match
|
|
|
|
}
|
|
|
|
in++;
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if( *in )
|
|
|
|
return 0; // reached end of pattern but not end of input
|
|
|
|
return 1; // success
|
|
|
|
}
|
2020-09-03 17:58:57 +02:00
|
|
|
|