2018-04-13 18:23:45 +02:00
|
|
|
/*
|
|
|
|
crtlib.h - internal stdlib
|
|
|
|
Copyright (C) 2011 Uncle Mike
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef STDLIB_H
|
|
|
|
#define STDLIB_H
|
|
|
|
|
2021-12-09 23:12:25 +01:00
|
|
|
#include <string.h>
|
2022-06-29 01:39:18 +02:00
|
|
|
#include <stdarg.h>
|
2024-04-28 05:18:16 +02:00
|
|
|
#include <ctype.h>
|
2019-06-21 16:25:43 +02:00
|
|
|
#include "build.h"
|
2022-06-29 01:39:18 +02:00
|
|
|
#include "xash3d_types.h"
|
2019-06-06 03:33:57 +02:00
|
|
|
|
2022-07-11 02:59:35 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
// timestamp modes
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TIME_FULL = 0,
|
|
|
|
TIME_DATE_ONLY,
|
|
|
|
TIME_TIME_ONLY,
|
|
|
|
TIME_NO_SECONDS,
|
|
|
|
TIME_YEAR_ONLY,
|
|
|
|
TIME_FILENAME,
|
|
|
|
};
|
|
|
|
|
2021-10-03 02:46:26 +02:00
|
|
|
// a1ba: not using BIT macro, so flags can be copypasted into
|
|
|
|
// exported APIs headers and will get nice warning in case of changing values
|
|
|
|
#define PFILE_IGNOREBRACKET (1<<0)
|
|
|
|
#define PFILE_HANDLECOLON (1<<1)
|
2022-01-27 01:25:32 +01:00
|
|
|
#define PFILE_TOKEN_MAX_LENGTH 1024
|
2022-07-10 23:34:48 +02:00
|
|
|
#define PFILE_FS_TOKEN_MAX_LENGTH 512
|
2021-10-01 18:58:03 +02:00
|
|
|
|
2024-05-26 22:11:07 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
#define restrict
|
|
|
|
#endif // __cplusplus
|
|
|
|
|
2022-07-26 03:10:36 +02:00
|
|
|
//
|
|
|
|
// build.c
|
|
|
|
//
|
|
|
|
int Q_buildnum( void );
|
2023-04-17 18:10:40 +02:00
|
|
|
int Q_buildnum_date( const char *date );
|
2022-07-26 03:10:36 +02:00
|
|
|
int Q_buildnum_compat( void );
|
2023-01-09 06:06:58 +01:00
|
|
|
const char *Q_PlatformStringByID( const int platform );
|
2022-07-26 03:10:36 +02:00
|
|
|
const char *Q_buildos( void );
|
2023-01-09 20:53:05 +01:00
|
|
|
const char *Q_ArchitectureStringByID( const int arch, const uint abi, const int endianness, const qboolean is64 );
|
2022-07-26 03:10:36 +02:00
|
|
|
const char *Q_buildarch( void );
|
|
|
|
const char *Q_buildcommit( void );
|
2024-03-16 18:32:31 +01:00
|
|
|
const char *Q_buildbranch( void );
|
2022-07-26 03:10:36 +02:00
|
|
|
|
2018-04-13 18:23:45 +02:00
|
|
|
//
|
|
|
|
// crtlib.c
|
|
|
|
//
|
|
|
|
void Q_strnlwr( const char *in, char *out, size_t size_out );
|
2021-12-03 07:44:51 +01:00
|
|
|
#define Q_strlen( str ) (( str ) ? strlen(( str )) : 0 )
|
2021-03-10 11:18:23 +01:00
|
|
|
size_t Q_colorstr( const char *string );
|
2024-10-28 00:07:52 +01:00
|
|
|
int Q_atoi_hex( int sign, const char *str );
|
2018-04-13 18:23:45 +02:00
|
|
|
int Q_atoi( const char *str );
|
|
|
|
float Q_atof( const char *str );
|
|
|
|
void Q_atov( float *vec, const char *str, size_t siz );
|
2022-06-14 02:23:46 +02:00
|
|
|
#define Q_strchr strchr
|
|
|
|
#define Q_strrchr strrchr
|
2022-08-25 18:14:52 +02:00
|
|
|
qboolean Q_stricmpext( const char *pattern, const char *text );
|
|
|
|
qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlen );
|
2023-01-03 04:58:58 +01:00
|
|
|
const byte *Q_memmem( const byte *haystack, size_t haystacklen, const byte *needle, size_t needlelen );
|
2024-05-26 22:11:07 +02:00
|
|
|
void Q_memor( byte *XASH_RESTRICT dst, const byte *XASH_RESTRICT src, size_t len );
|
2018-04-13 18:23:45 +02:00
|
|
|
const char *Q_timestamp( int format );
|
|
|
|
int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args );
|
2024-11-07 12:24:07 +01:00
|
|
|
int Q_snprintf( char *buffer, size_t buffersize, const char *format, ... ) FORMAT_CHECK( 3 );
|
2022-12-17 21:17:32 +01:00
|
|
|
#define Q_strpbrk strpbrk
|
2022-06-10 17:43:04 +02:00
|
|
|
void COM_StripColors( const char *in, char *out );
|
2018-04-13 18:23:45 +02:00
|
|
|
#define Q_memprint( val ) Q_pretifymem( val, 2 )
|
|
|
|
char *Q_pretifymem( float value, int digitsafterdecimal );
|
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
|
|
|
const char *COM_FileExtension( const char *in );
|
2023-04-26 03:57:04 +02:00
|
|
|
void COM_DefaultExtension( char *path, const char *extension, size_t size );
|
|
|
|
void COM_ReplaceExtension( char *path, const char *extension, size_t size );
|
2019-03-06 14:23:33 +01:00
|
|
|
void COM_ExtractFilePath( const char *path, char *dest );
|
|
|
|
const char *COM_FileWithoutPath( const char *in );
|
|
|
|
void COM_StripExtension( char *path );
|
2024-05-15 02:06:57 +02:00
|
|
|
void COM_RemoveLineFeed( char *str, size_t bufsize );
|
2022-07-01 18:37:21 +02:00
|
|
|
void COM_FixSlashes( char *pname );
|
2020-03-04 05:08:14 +01:00
|
|
|
void COM_PathSlashFix( char *path );
|
2022-07-10 23:34:48 +02:00
|
|
|
// return 0 on empty or null string, 1 otherwise
|
2019-03-16 05:15:06 +01:00
|
|
|
#define COM_CheckString( string ) ( ( !string || !*string ) ? 0 : 1 )
|
2020-11-22 07:49:39 +01:00
|
|
|
#define COM_CheckStringEmpty( string ) ( ( !*string ) ? 0 : 1 )
|
2022-07-10 23:34:48 +02:00
|
|
|
char *COM_ParseFileSafe( char *data, char *token, const int size, unsigned int flags, int *len, qboolean *quoted );
|
|
|
|
#define COM_ParseFile( data, token, size ) COM_ParseFileSafe( data, token, size, 0, NULL, NULL )
|
2019-07-09 15:36:15 +02:00
|
|
|
int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive );
|
|
|
|
int matchpattern_with_separator( const char *in, const char *pattern, qboolean caseinsensitive, const char *separators, qboolean wildcard_least_one );
|
2019-03-16 05:15:06 +01:00
|
|
|
|
2024-04-28 05:18:16 +02:00
|
|
|
static inline char Q_toupper( const char in )
|
|
|
|
{
|
|
|
|
char out;
|
|
|
|
|
|
|
|
if( in >= 'a' && in <= 'z' )
|
|
|
|
out = in + 'A' - 'a';
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char Q_tolower( const char in )
|
|
|
|
{
|
|
|
|
char out;
|
|
|
|
|
|
|
|
if( in >= 'A' && in <= 'Z' )
|
|
|
|
out = in + 'a' - 'A';
|
|
|
|
else out = in;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qboolean Q_isdigit( const char *str )
|
|
|
|
{
|
|
|
|
if( likely( str && *str ))
|
|
|
|
{
|
|
|
|
while( isdigit( *str )) str++;
|
|
|
|
if( !*str ) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline qboolean Q_isspace( const char *str )
|
|
|
|
{
|
|
|
|
if( likely( str && *str ))
|
|
|
|
{
|
|
|
|
while( isspace( *str ) ) str++;
|
|
|
|
if( !*str ) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-06-29 01:39:18 +02:00
|
|
|
static inline int Q_strcmp( const char *s1, const char *s2 )
|
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
return strcmp( s1, s2 );
|
|
|
|
return ( s1 ? 1 : 0 ) - ( s2 ? 1 : 0 );
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int Q_strncmp( const char *s1, const char *s2, size_t n )
|
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
return strncmp( s1, s2, n );
|
|
|
|
return ( s1 ? 1 : 0 ) - ( s2 ? 1 : 0 );
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-29 02:56:42 +02:00
|
|
|
static inline char *Q_strstr( const char *s1, const char *s2 )
|
2022-06-29 01:39:18 +02:00
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
return (char *)strstr( s1, s2 );
|
|
|
|
return NULL;
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
// libc extensions, be careful what to enable or what not
|
|
|
|
static inline size_t Q_strncpy( char *dst, const char *src, size_t size )
|
2023-06-27 03:11:13 +02:00
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
#if HAVE_STRLCPY
|
|
|
|
if( unlikely( !dst || !src || !size ))
|
|
|
|
return 0;
|
|
|
|
return strlcpy( dst, src, size );
|
|
|
|
#else
|
2023-06-27 03:11:13 +02:00
|
|
|
size_t len;
|
2024-04-29 05:16:44 +02:00
|
|
|
if( unlikely( !dst || !src || !size ))
|
2023-06-27 03:11:13 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
len = strlen( src );
|
2024-04-29 05:16:44 +02:00
|
|
|
if( len + 1 > size ) // check if truncate
|
2023-06-27 03:11:13 +02:00
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
memcpy( dst, src, size - 1 );
|
|
|
|
dst[size - 1] = 0;
|
2023-06-27 03:11:13 +02:00
|
|
|
}
|
|
|
|
else memcpy( dst, src, len + 1 );
|
|
|
|
|
|
|
|
return len; // count does not include NULL
|
2024-04-29 05:16:44 +02:00
|
|
|
#endif
|
2023-06-27 03:11:13 +02:00
|
|
|
}
|
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
static inline size_t Q_strncat( char *dst, const char *src, size_t size )
|
|
|
|
{
|
|
|
|
#if HAVE_STRLCAT
|
|
|
|
if( unlikely( !dst || !src || !size ))
|
|
|
|
return 0;
|
|
|
|
return strlcat( dst, src, size );
|
|
|
|
#else
|
|
|
|
char *d = dst;
|
|
|
|
const char *s = src;
|
|
|
|
size_t n = size;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
if( unlikely( !dst || !src || !size ))
|
|
|
|
return 0;
|
2022-06-29 01:39:18 +02:00
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
// 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;
|
2022-06-29 01:39:18 +02:00
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
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
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if HAVE_STRICMP || HAVE_STRCASECMP
|
2022-06-29 01:39:18 +02:00
|
|
|
static inline int Q_stricmp( const char *s1, const char *s2 )
|
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
{
|
|
|
|
#if HAVE_STRICMP
|
|
|
|
return stricmp( s1, s2 );
|
|
|
|
#elif HAVE_STRCASECMP
|
|
|
|
return strcasecmp( s1, s2 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return ( s1 ? 1 : 0 ) - ( s2 ? 1 : 0 );
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
2024-04-29 05:16:44 +02:00
|
|
|
#else
|
|
|
|
int Q_stricmp( const char *s1, const char *s2 );
|
|
|
|
#endif
|
2022-06-29 01:39:18 +02:00
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
#if HAVE_STRICMP || HAVE_STRCASECMP
|
2022-06-29 01:39:18 +02:00
|
|
|
static inline int Q_strnicmp( const char *s1, const char *s2, size_t n )
|
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
{
|
|
|
|
#if HAVE_STRICMP
|
|
|
|
return strnicmp( s1, s2, n );
|
|
|
|
#elif HAVE_STRCASECMP
|
|
|
|
return strncasecmp( s1, s2, n );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return ( s1 ? 1 : 0 ) - ( s2 ? 1 : 0 );
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
2024-04-29 05:16:44 +02:00
|
|
|
#else
|
|
|
|
int Q_strnicmp( const char *s1, const char *s2, size_t n );
|
2022-06-29 01:39:18 +02:00
|
|
|
#endif
|
2024-04-29 05:16:44 +02:00
|
|
|
|
|
|
|
|
2024-05-03 14:30:39 +02:00
|
|
|
#if HAVE_STRCASESTR
|
2022-06-29 02:56:42 +02:00
|
|
|
static inline char *Q_stristr( const char *s1, const char *s2 )
|
2022-06-29 01:39:18 +02:00
|
|
|
{
|
2024-04-29 05:16:44 +02:00
|
|
|
if( likely( s1 && s2 ))
|
|
|
|
return (char *)strcasestr( s1, s2 );
|
|
|
|
return NULL;
|
2022-06-29 01:39:18 +02:00
|
|
|
}
|
2024-05-03 14:30:39 +02:00
|
|
|
#else // !HAVE_STRCASESTR
|
2022-06-29 02:56:42 +02:00
|
|
|
char *Q_stristr( const char *s1, const char *s2 );
|
2024-05-03 14:30:39 +02:00
|
|
|
#endif // !HAVE_STRCASESTR
|
2022-06-29 01:39:18 +02:00
|
|
|
|
2024-04-29 05:16:44 +02:00
|
|
|
#if HAVE_STRCHRNUL
|
2022-12-17 21:17:32 +01:00
|
|
|
#define Q_strchrnul strchrnul
|
2024-04-29 05:16:44 +02:00
|
|
|
#else // !HAVE_STRCHRNUL
|
2022-12-17 21:17:32 +01:00
|
|
|
static inline const char *Q_strchrnul( const char *s, int c )
|
|
|
|
{
|
|
|
|
const char *p = Q_strchr( s, c );
|
2024-04-29 05:16:44 +02:00
|
|
|
if( p ) return p;
|
2022-12-17 21:17:32 +01:00
|
|
|
return s + Q_strlen( s );
|
|
|
|
}
|
2024-04-29 05:16:44 +02:00
|
|
|
#endif // !HAVE_STRCHRNUL
|
2022-12-17 21:17:32 +01:00
|
|
|
|
2022-07-11 02:59:35 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
2022-06-29 01:39:18 +02:00
|
|
|
|
2018-04-23 20:36:33 +02:00
|
|
|
#endif//STDLIB_H
|