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>
|
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 )
|
|
|
|
{
|
2024-07-04 05:38:50 +02:00
|
|
|
size_t len, i;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:38:50 +02:00
|
|
|
len = Q_strncpy( out, in, size_out );
|
|
|
|
|
|
|
|
for( i = 0; i < len; i++ )
|
|
|
|
out[i] = Q_tolower( out[i] );
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
2021-03-10 11:18:23 +01:00
|
|
|
size_t Q_colorstr( const char *string )
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
2024-07-04 05:38:50 +02:00
|
|
|
const char *p = string;
|
|
|
|
size_t len = 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:38:50 +02:00
|
|
|
if( !string )
|
|
|
|
return len;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:38:50 +02:00
|
|
|
while(( p = Q_strchr( p, '^' )))
|
2018-04-13 18:23:45 +02:00
|
|
|
{
|
|
|
|
if( IsColorString( p ))
|
|
|
|
{
|
|
|
|
len += 2;
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2024-10-28 00:07:52 +01:00
|
|
|
int Q_atoi_hex( int sign, const char *str )
|
2024-07-04 05:30:59 +02:00
|
|
|
{
|
|
|
|
int c, val = 0;
|
|
|
|
|
2024-10-28 00:07:52 +01:00
|
|
|
if( str[0] == '0' && ( str[1] == 'x' || str[1] == 'X' ))
|
|
|
|
str += 2;
|
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int Q_atoi_character( int sign, const char *str )
|
|
|
|
{
|
|
|
|
return sign * str[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *Q_atoi_strip_whitespace( const char *str )
|
|
|
|
{
|
|
|
|
while( str && *str == ' ' )
|
|
|
|
str++;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
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
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
if( !COM_CheckString( str ))
|
|
|
|
return 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
str = Q_atoi_strip_whitespace( str );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
if( !COM_CheckString( 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' ))
|
2024-07-04 05:30:59 +02:00
|
|
|
return Q_atoi_hex( sign, str );
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// check for character
|
|
|
|
if( str[0] == '\'' )
|
2024-07-04 05:30:59 +02:00
|
|
|
return Q_atoi_character( sign, str );
|
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;
|
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
if( !COM_CheckString( str ))
|
|
|
|
return 0;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
str = Q_atoi_strip_whitespace( str );
|
2018-04-13 18:23:45 +02:00
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
if( !COM_CheckString( 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' ))
|
2024-07-04 05:30:59 +02:00
|
|
|
return Q_atoi_hex( sign, str );
|
2021-01-03 02:28:45 +01:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// check for character
|
2024-07-04 05:30:59 +02:00
|
|
|
if( str[0] == '\'' )
|
|
|
|
return Q_atoi_character( sign, str );
|
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 )
|
|
|
|
{
|
2024-04-28 05:16:59 +02:00
|
|
|
const char *pstr, *pfront;
|
2018-04-13 18:23:45 +02:00
|
|
|
int j;
|
|
|
|
|
2024-07-04 05:30:59 +02:00
|
|
|
memset( vec, 0, sizeof( *vec ) * siz );
|
2024-04-28 05:16:59 +02:00
|
|
|
pstr = pfront = str;
|
2018-04-13 18:23:45 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-05-26 22:11:07 +02:00
|
|
|
void Q_memor( byte *XASH_RESTRICT dst, const byte *XASH_RESTRICT src, size_t len )
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
for( i = 0; i < len; i++ ) // msvc likes to optimize this loop form
|
|
|
|
dst[i] |= src[i];
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
time( &crt_time );
|
|
|
|
crt_tm = localtime( &crt_time );
|
|
|
|
|
|
|
|
switch( format )
|
|
|
|
{
|
|
|
|
case TIME_FULL:
|
|
|
|
// Build the full timestamp (ex: "Apr03 2007 [23:31.55]");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%b%d %Y [%H:%M.%S]", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
case TIME_DATE_ONLY:
|
|
|
|
// Build the date stamp only (ex: "Apr03 2007");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%b%d %Y", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
case TIME_TIME_ONLY:
|
|
|
|
// Build the time stamp only (ex: "23:31.55");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%H:%M.%S", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
case TIME_NO_SECONDS:
|
|
|
|
// Build the time stamp exclude seconds (ex: "13:46");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%H:%M", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
case TIME_YEAR_ONLY:
|
|
|
|
// Build the date stamp year only (ex: "2006");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%Y", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
|
|
|
case TIME_FILENAME:
|
|
|
|
// Build a timestamp that can use for filename (ex: "Nov2006-26 (19.14.28)");
|
2024-10-02 21:18:16 +02:00
|
|
|
strftime( timestamp, sizeof( timestamp ), "%b%Y-%d_%H.%M.%S", crt_tm );
|
2018-04-13 18:23:45 +02:00
|
|
|
break;
|
2024-11-07 14:11:40 +01:00
|
|
|
default:
|
|
|
|
Q_snprintf( timestamp, sizeof( timestamp ), "%s: unknown format %d", __func__, format );
|
|
|
|
break;
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return timestamp;
|
|
|
|
}
|
|
|
|
|
2024-05-03 14:30:39 +02:00
|
|
|
#if !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
|
|
|
{
|
2024-07-04 05:30:59 +02:00
|
|
|
while( *in )
|
2022-06-10 10:20:58 +02:00
|
|
|
{
|
2024-07-04 05:30:59 +02:00
|
|
|
if( IsColorString( in ))
|
2022-06-10 10:20:58 +02:00
|
|
|
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;
|
2024-07-04 05:39:18 +02:00
|
|
|
const float onekb = 1024.0f;
|
|
|
|
const float onemb = onekb * onekb;
|
|
|
|
const char *suffix;
|
2018-04-13 18:23:45 +02:00
|
|
|
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;
|
2024-07-04 05:39:18 +02:00
|
|
|
suffix = " Mb";
|
2018-04-13 18:23:45 +02:00
|
|
|
}
|
|
|
|
else if( value > onekb )
|
|
|
|
{
|
|
|
|
value /= onekb;
|
2024-07-04 05:39:18 +02:00
|
|
|
suffix = " Kb";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
suffix = " bytes";
|
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 )
|
|
|
|
{
|
2024-11-17 04:20:32 +01:00
|
|
|
const char *dot;
|
2019-03-06 14:23:33 +01:00
|
|
|
|
|
|
|
dot = Q_strrchr( in, '.' );
|
|
|
|
|
2024-11-17 04:20:32 +01:00
|
|
|
// quickly exit if there is no dot at all
|
|
|
|
if( dot == NULL )
|
|
|
|
return "";
|
|
|
|
|
|
|
|
// if there are any of these special symbols after the dot, the file has no extension
|
|
|
|
if( Q_strpbrk( dot + 1, "\\/:" ))
|
2019-03-06 14:23:33 +01:00
|
|
|
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
|
|
|
|
============
|
|
|
|
*/
|
2024-05-15 02:06:57 +02:00
|
|
|
void COM_RemoveLineFeed( char *str, size_t bufsize )
|
2020-03-04 05:08:14 +01:00
|
|
|
{
|
2024-05-15 02:06:57 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for( i = 0; i < bufsize && *str != '\0'; i++, str++ )
|
2020-03-04 05:08:14 +01:00
|
|
|
{
|
|
|
|
if( *str == '\r' || *str == '\n' )
|
|
|
|
*str = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
{
|
2024-07-04 05:42:49 +02:00
|
|
|
while(( pname = Q_strchr( pname, '\\' )))
|
2022-07-01 18:37:21 +02:00
|
|
|
{
|
2024-07-04 05:42:49 +02:00
|
|
|
*pname = '/';
|
2022-07-01 18:37:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:08:14 +01:00
|
|
|
/*
|
|
|
|
============
|
|
|
|
COM_PathSlashFix
|
2024-07-30 13:22:08 +02:00
|
|
|
|
|
|
|
ensure directory path always ends on forward slash
|
2020-03-04 05:08:14 +01:00
|
|
|
============
|
|
|
|
*/
|
|
|
|
void COM_PathSlashFix( char *path )
|
|
|
|
{
|
2024-07-30 13:22:08 +02:00
|
|
|
size_t len = Q_strlen( path );
|
2020-03-04 05:08:14 +01:00
|
|
|
|
2024-07-30 13:22:08 +02:00
|
|
|
if( path[len - 1] == '\\' )
|
|
|
|
{
|
|
|
|
path[len - 1] = '/';
|
|
|
|
}
|
|
|
|
else if( 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
|
|
|
}
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
2024-11-18 11:25:00 +01:00
|
|
|
// skip // or #, if requested, comments
|
|
|
|
if(( c == '/' && data[1] == '/' ) || ( c == '#' && FBitSet( flags, PFILE_IGNOREHASHCMT )))
|
2021-10-01 18:58:03 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|