forked from FWGS/xash3d-fwgs
-
Notifications
You must be signed in to change notification settings - Fork 16
/
crtlib.c
1025 lines (856 loc) · 18.1 KB
/
crtlib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
crtlib.c - internal stdlib
Copyright (C) 2011 Uncle Mike
Copyright (c) QuakeSpasm contributors
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.
*/
#include "port.h"
#include "xash3d_types.h"
#include "const.h"
#include <math.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include "stdio.h"
#include "crtlib.h"
#include "xash3d_mathlib.h"
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;
}
qboolean Q_isspace( const char *str )
{
if( str && *str )
{
while( isspace( *str ) ) str++;
if( !*str ) return true;
}
return false;
}
size_t Q_colorstr( const char *string )
{
size_t len;
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 )
{
int val = 0;
int c, sign;
if( !str ) return 0;
// check for empty charachters in string
while( str && *str == ' ' )
str++;
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;
}
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;
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;
}
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;
}
}
qboolean Q_strnicmpext( const char *pattern, const char *text, size_t minimumlength )
{
size_t i = 0;
char c;
while(( c = *pattern++ ) != '\0' )
{
i++;
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;
}
}
return ( *text == '\0' ) || i == minimumlength;
}
qboolean Q_stricmpext( const char *pattern, const char *text )
{
return Q_strnicmpext( pattern, text, ~((size_t)0) );
}
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;
// skip one byte
i++;
haystacklen -= i - haystack;
haystack = i;
}
return NULL;
}
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;
}
#if !defined( HAVE_STRCASESTR )
char *Q_stristr( const char *string, const char *string2 )
{
int c;
size_t len;
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;
}
return (char *)string;
}
#endif // !defined( HAVE_STRCASESTR )
int Q_vsnprintf( char *buffer, size_t buffersize, const char *format, va_list args )
{
int result;
#ifndef _MSC_VER
result = vsnprintf( buffer, buffersize, format, args );
#else
__try
{
result = _vsnprintf( buffer, buffersize, format, args );
}
// to prevent crash while output
__except( EXCEPTION_EXECUTE_HANDLER )
{
Q_strncpy( buffer, "^1sprintf throw exception^7\n", buffersize );
result = buffersize;
}
#endif
if( result >= buffersize )
{
buffer[buffersize - 1] = '\0';
return -1;
}
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;
}
void COM_StripColors( const char *in, char *out )
{
while ( *in )
{
if ( IsColorString( in ) )
in += 2;
else *out++ = *in++;
}
*out = '\0';
}
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;
Q_strncpy( suffix, " Mb", sizeof( suffix ));
}
else if( value > onekb )
{
value /= onekb;
Q_strncpy( suffix, " Kb", sizeof( suffix ));
}
else Q_strncpy( suffix, " bytes", sizeof( suffix ));
// clamp to >= 0
digitsafterdecimal = Q_max( digitsafterdecimal, 0 );
// if it's basically integral, don't do any decimals
if( fabs( value - (int)value ) < 0.00001f )
{
Q_snprintf( val, sizeof( val ), "%i%s", (int)value, suffix );
}
else
{
char fmt[32];
// otherwise, create a format string for the decimals
Q_snprintf( fmt, sizeof( fmt ), "%%.%if%s", digitsafterdecimal, suffix );
Q_snprintf( val, sizeof( val ), fmt, (double)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 = Q_strchr( i, '.' );
if( !dot ) dot = Q_strchr( 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;
}
/*
============
COM_FileBase
Extracts the base name of a file (no path, no extension, assumes '/' as path separator)
a1ba: adapted and simplified version from QuakeSpasm
============
*/
void COM_FileBase( const char *in, char *out, size_t size )
{
const char *dot, *slash, *s;
size_t len;
if( unlikely( !COM_CheckString( in ) || size <= 1 ))
{
out[0] = 0;
return;
}
slash = in;
dot = NULL;
for( s = in; *s; s++ )
{
if( *s == '/' || *s == '\\' )
slash = s + 1;
if( *s == '.' )
dot = s;
}
if( dot == NULL || dot < slash )
dot = s;
len = Q_min( size - 1, dot - slash );
memcpy( out, slash, len );
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 )
{
const char *src = path + Q_strlen( path ) - 1;
// back up until a \ or the start
while( src > path && !(*(src - 1) == '\\' || *(src - 1) == '/' ))
src--;
if( src > path )
{
memcpy( dest, path, src - path );
dest[src - path - 1] = 0; // cutoff backslash
}
else dest[0] = 0; // file without path
}
/*
============
COM_StripExtension
============
*/
void COM_StripExtension( char *path )
{
size_t length;
length = Q_strlen( path );
if( length > 0 )
length--;
while( length > 0 && path[length] != '.' )
{
length--;
if( path[length] == '/' || path[length] == '\\' || path[length] == ':' )
return; // no extension
}
if( length ) path[length] = 0;
}
/*
==================
COM_DefaultExtension
==================
*/
void COM_DefaultExtension( char *path, const char *extension, size_t size )
{
const char *src;
size_t len;
// if path doesn't have a .EXT, append extension
// (extension should include the .)
len = Q_strlen( path );
src = path + len - 1;
while( *src != '/' && src != path )
{
// it has an extension
if( *src == '.' ) return;
src--;
}
Q_strncpy( &path[len], extension, size - len );
}
/*
==================
COM_ReplaceExtension
==================
*/
void COM_ReplaceExtension( char *path, const char *extension, size_t size )
{
COM_StripExtension( path );
COM_DefaultExtension( path, extension, size );
}
/*
============
COM_RemoveLineFeed
============
*/
void COM_RemoveLineFeed( char *str )
{
while( *str != '\0' )
{
if( *str == '\r' || *str == '\n' )
*str = '\0';
++str;
}
}
/*
============
COM_FixSlashes
Changes all '\' characters into '/' characters, in place.
============
*/
void COM_FixSlashes( char *pname )
{
for( ; *pname; pname++ )
{
if( *pname == '\\' )
*pname = '/';
}
}
/*
============
COM_PathSlashFix
============
*/
void COM_PathSlashFix( char *path )
{
size_t len;
len = Q_strlen( path );
if( path[len - 1] != '\\' && path[len - 1] != '/' )
{
path[len] = '/';
path[len + 1] = '\0';
}
}
/*
============
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';
}
/*
==============
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
==============
*/
char *COM_ParseFileSafe( char *data, char *token, const int size, unsigned int flags, int *plen, qboolean *quoted )
{
int c, len = 0;
qboolean overflow = false;
if( quoted )
*quoted = false;
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 == '\"' )
{
if( quoted )
*quoted = true;
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++;
}
else overflow = true;
data++;
continue;
}
if( c == '\"' )
{
token[len] = 0;
if( plen ) *plen = overflow ? -1 : len;
return data;
}
if( len + 1 < size )
{
token[len] = c;
len++;
}
else overflow = true;
}
}
// 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++;
}
else overflow = true;
data++;
c = ((byte)*data);
if( COM_IsSingleChar( flags, c ))
break;
} while( c > 32 );
token[len] = 0;
if( plen ) *plen = overflow ? -1 : len;
return data;
}
int matchpattern( const char *in, const char *pattern, qboolean caseinsensitive )
{
const char *separators = "/\\:";
if( !Q_strcmp( pattern, "*" ))
separators = "";
return matchpattern_with_separator( in, pattern, caseinsensitive, separators, false );
}
// 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++;
}