This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stb_lib.h
3305 lines (2911 loc) · 104 KB
/
stb_lib.h
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
/* stb_lib.h - v1.00 - http://nothings.org/stb
no warranty is offered or implied; use this code at your own risk
============================================================================
You MUST
#define STB_LIB_IMPLEMENTATION
in EXACTLY _one_ C or C++ file that includes this header, BEFORE the
include, like this:
#define STB_LIB_IMPLEMENTATION
#include "stblib_files.h"
All other files should just #include "stblib_files.h" without the #define.
============================================================================
LICENSE
See end of file for license information.
CREDITS
Written by Sean Barrett.
Fixes:
Philipp Wiesemann Robert Nix
r-lyeh blackpawn
github:Mojofreem Ryan Whitworth
Vincent Isambart Mike Sartain
Eugene Opalev Tim Sjostrand
github:infatum Dave Butler
*/
#ifndef STB_INCLUDE_STB_LIB_H
#include <stdarg.h>
#if defined(_WIN32) && !defined(__MINGW32__)
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#ifndef _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE
#endif
#ifndef _CRT_NON_CONFORMING_SWPRINTFS
#define _CRT_NON_CONFORMING_SWPRINTFS
#endif
#if !defined(_MSC_VER) || _MSC_VER > 1700
#include <intrin.h> // _BitScanReverse
#endif
#endif
#include <stdlib.h> // stdlib could have min/max
#include <stdio.h> // need FILE
#include <string.h> // stb_define_hash needs memcpy/memset
#include <time.h> // stb_dirtree
typedef unsigned char stb_uchar;
typedef unsigned char stb_uint8;
typedef unsigned int stb_uint;
typedef unsigned short stb_uint16;
typedef short stb_int16;
typedef signed char stb_int8;
#if defined(STB_USE_LONG_FOR_32_BIT_INT) || defined(STB_LONG32)
typedef unsigned long stb_uint32;
typedef long stb_int32;
#else
typedef unsigned int stb_uint32;
typedef int stb_int32;
#endif
typedef char stb__testsize2_16[sizeof(stb_uint16)==2 ? 1 : -1];
typedef char stb__testsize2_32[sizeof(stb_uint32)==4 ? 1 : -1];
#ifdef _MSC_VER
typedef unsigned __int64 stb_uint64;
typedef __int64 stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ui64)
#else
// ??
typedef unsigned long long stb_uint64;
typedef long long stb_int64;
#define STB_IMM_UINT64(literalui64) (literalui64##ULL)
#endif
typedef char stb__testsize2_64[sizeof(stb_uint64)==8 ? 1 : -1];
#ifdef __cplusplus
#define STB_EXTERN extern "C"
#else
#define STB_EXTERN extern
#endif
// check for well-known debug defines
#if defined(DEBUG) || defined(_DEBUG) || defined(DBG)
#ifndef NDEBUG
#define STB_DEBUG
#endif
#endif
#ifdef STB_DEBUG
#include <assert.h>
#endif
#endif // STB_INCLUDE_STB_LIB_H
#ifdef STB_LIB_IMPLEMENTATION
#include <assert.h>
#include <stdarg.h>
#include <stddef.h>
#include <ctype.h>
#include <math.h>
#ifndef _WIN32
#include <unistd.h>
#else
#include <io.h> // _mktemp
#include <direct.h> // _rmdir
#endif
#include <sys/types.h> // stat()/_stat()
#include <sys/stat.h> // stat()/_stat()
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Miscellany
//
#ifdef _WIN32
#define stb_stricmp(a,b) stricmp(a,b)
#define stb_strnicmp(a,b,n) strnicmp(a,b,n)
#else
#define stb_stricmp(a,b) strcasecmp(a,b)
#define stb_strnicmp(a,b,n) strncasecmp(a,b,n)
#endif
#ifndef STB_INCLUDE_STB_LIB_H
STB_EXTERN void stb_fatal(char *fmt, ...);
STB_EXTERN void stb_swap(void *p, void *q, size_t sz);
STB_EXTERN double stb_linear_remap(double x, double x_min, double x_max,
double out_min, double out_max);
#define stb_arrcount(x) (sizeof(x)/sizeof((x)[0]))
#define stb_lerp(t,a,b) ( (a) + (t) * (float) ((b)-(a)) )
#define stb_unlerp(t,a,b) ( ((t) - (a)) / (float) ((b) - (a)) )
#endif
#ifdef STB_LIB_IMPLEMENTATION
void stb_fatal(char *s, ...)
{
va_list a;
va_start(a,s);
fputs("Fatal error: ", stderr);
vfprintf(stderr, s, a);
va_end(a);
fputs("\n", stderr);
#ifdef STB_DEBUG
#ifdef _MSC_VER
#ifndef _WIN64
__asm int 3; // trap to debugger!
#else
__debugbreak();
#endif
#else
__builtin_trap();
#endif
#endif
exit(1);
}
typedef struct { char d[4]; } stb__4;
typedef struct { char d[8]; } stb__8;
// optimize the small cases, though you shouldn't be calling this for those!
void stb_swap(void *p, void *q, size_t sz)
{
char buffer[256];
if (p == q) return;
if (sz == 4) {
stb__4 temp = * ( stb__4 *) p;
* (stb__4 *) p = * ( stb__4 *) q;
* (stb__4 *) q = temp;
return;
} else if (sz == 8) {
stb__8 temp = * ( stb__8 *) p;
* (stb__8 *) p = * ( stb__8 *) q;
* (stb__8 *) q = temp;
return;
}
while (sz > sizeof(buffer)) {
stb_swap(p, q, sizeof(buffer));
p = (char *) p + sizeof(buffer);
q = (char *) q + sizeof(buffer);
sz -= sizeof(buffer);
}
memcpy(buffer, p , sz);
memcpy(p , q , sz);
memcpy(q , buffer, sz);
}
#ifdef stb_linear_remap
#undef stb_linear_remap
#endif
double stb_linear_remap(double x, double x_min, double x_max,
double out_min, double out_max)
{
return stb_lerp(stb_unlerp(x,x_min,x_max),out_min,out_max);
}
#define stb_linear_remap(t,a,b,c,d) stb_lerp(stb_unlerp(t,a,b),c,d)
#endif // STB_LIB_IMPLEMENTATION
#ifndef STB_INCLUDE_STB_LIB_H
// avoid unnecessary function call, but define function so its address can be taken
#ifndef stb_linear_remap
#define stb_linear_remap(t,a,b,c,d) stb_lerp(stb_unlerp(t,a,b),c,d)
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// cross-platform snprintf because they keep changing that,
// and with old compilers without vararg macros we can't write
// a macro wrapper to fix it up
#ifndef STB_INCLUDE_STB_LIB_H
STB_EXTERN int stb_snprintf(char *s, size_t n, const char *fmt, ...);
STB_EXTERN int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v);
STB_EXTERN char *stb_sprintf(const char *fmt, ...);
#endif
#ifdef STB_LIB_IMPLEMENTATION
int stb_vsnprintf(char *s, size_t n, const char *fmt, va_list v)
{
int res;
#ifdef _WIN32
// Could use "_vsnprintf_s(s, n, _TRUNCATE, fmt, v)" ?
res = _vsnprintf(s,n,fmt,v);
#else
res = vsnprintf(s,n,fmt,v);
#endif
if (n) s[n-1] = 0;
// Unix returns length output would require, Windows returns negative when truncated.
return (res >= (int) n || res < 0) ? -1 : res;
}
int stb_snprintf(char *s, size_t n, const char *fmt, ...)
{
int res;
va_list v;
va_start(v,fmt);
res = stb_vsnprintf(s, n, fmt, v);
va_end(v);
return res;
}
char *stb_sprintf(const char *fmt, ...)
{
static char buffer[1024];
va_list v;
va_start(v,fmt);
stb_vsnprintf(buffer,1024,fmt,v);
va_end(v);
return buffer;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Windows UTF8 filename handling
//
// Windows stupidly treats 8-bit filenames as some dopey code page,
// rather than utf-8. If we want to use utf8 filenames, we have to
// convert them to WCHAR explicitly and call WCHAR versions of the
// file functions. So, ok, we do.
#ifndef STB_INCLUDE_STB_LIB_H
#ifdef _WIN32
#define stb__fopen(x,y) _wfopen((const wchar_t *)stb__from_utf8(x), (const wchar_t *)stb__from_utf8_alt(y))
#define stb__windows(x,y) x
#else
#define stb__fopen(x,y) fopen(x,y)
#define stb__windows(x,y) y
#endif
typedef unsigned short stb__wchar;
STB_EXTERN stb__wchar * stb_from_utf8(stb__wchar *buffer, char *str, int n);
STB_EXTERN char * stb_to_utf8 (char *buffer, stb__wchar *str, int n);
STB_EXTERN stb__wchar *stb__from_utf8(char *str);
STB_EXTERN stb__wchar *stb__from_utf8_alt(char *str);
STB_EXTERN char *stb__to_utf8(stb__wchar *str);
#endif
#ifdef STB_LIB_IMPLEMENTATION
stb__wchar * stb_from_utf8(stb__wchar *buffer, char *ostr, int n)
{
unsigned char *str = (unsigned char *) ostr;
stb_uint32 c;
int i=0;
--n;
while (*str) {
if (i >= n)
return NULL;
if (!(*str & 0x80))
buffer[i++] = *str++;
else if ((*str & 0xe0) == 0xc0) {
if (*str < 0xc2) return NULL;
c = (*str++ & 0x1f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
buffer[i++] = c + (*str++ & 0x3f);
} else if ((*str & 0xf0) == 0xe0) {
if (*str == 0xe0 && (str[1] < 0xa0 || str[1] > 0xbf)) return NULL;
if (*str == 0xed && str[1] > 0x9f) return NULL; // str[1] < 0x80 is checked below
c = (*str++ & 0x0f) << 12;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
buffer[i++] = c + (*str++ & 0x3f);
} else if ((*str & 0xf8) == 0xf0) {
if (*str > 0xf4) return NULL;
if (*str == 0xf0 && (str[1] < 0x90 || str[1] > 0xbf)) return NULL;
if (*str == 0xf4 && str[1] > 0x8f) return NULL; // str[1] < 0x80 is checked below
c = (*str++ & 0x07) << 18;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 12;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f) << 6;
if ((*str & 0xc0) != 0x80) return NULL;
c += (*str++ & 0x3f);
// utf-8 encodings of values used in surrogate pairs are invalid
if ((c & 0xFFFFF800) == 0xD800) return NULL;
if (c >= 0x10000) {
c -= 0x10000;
if (i + 2 > n) return NULL;
buffer[i++] = 0xD800 | (0x3ff & (c >> 10));
buffer[i++] = 0xDC00 | (0x3ff & (c ));
}
} else
return NULL;
}
buffer[i] = 0;
return buffer;
}
char * stb_to_utf8(char *buffer, stb__wchar *str, int n)
{
int i=0;
--n;
while (*str) {
if (*str < 0x80) {
if (i+1 > n) return NULL;
buffer[i++] = (char) *str++;
} else if (*str < 0x800) {
if (i+2 > n) return NULL;
buffer[i++] = 0xc0 + (*str >> 6);
buffer[i++] = 0x80 + (*str & 0x3f);
str += 1;
} else if (*str >= 0xd800 && *str < 0xdc00) {
stb_uint32 c;
if (i+4 > n) return NULL;
c = ((str[0] - 0xd800) << 10) + ((str[1]) - 0xdc00) + 0x10000;
buffer[i++] = 0xf0 + (c >> 18);
buffer[i++] = 0x80 + ((c >> 12) & 0x3f);
buffer[i++] = 0x80 + ((c >> 6) & 0x3f);
buffer[i++] = 0x80 + ((c ) & 0x3f);
str += 2;
} else if (*str >= 0xdc00 && *str < 0xe000) {
return NULL;
} else {
if (i+3 > n) return NULL;
buffer[i++] = 0xe0 + (*str >> 12);
buffer[i++] = 0x80 + ((*str >> 6) & 0x3f);
buffer[i++] = 0x80 + ((*str ) & 0x3f);
str += 1;
}
}
buffer[i] = 0;
return buffer;
}
stb__wchar *stb__from_utf8(char *str)
{
static stb__wchar buffer[4096];
return stb_from_utf8(buffer, str, 4096);
}
stb__wchar *stb__from_utf8_alt(char *str)
{
static stb__wchar buffer[4096];
return stb_from_utf8(buffer, str, 4096);
}
char *stb__to_utf8(stb__wchar *str)
{
static char buffer[4096];
return stb_to_utf8(buffer, str, 4096);
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// qsort Compare Routines
// NOT THREAD SAFE
#ifndef STB_INCLUDE_STB_LIB_H
STB_EXTERN int (*stb_intcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_qsort_strcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_qsort_stricmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_floatcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_doublecmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_ucharcmp(int offset))(const void *a, const void *b);
STB_EXTERN int (*stb_charcmp(int offset))(const void *a, const void *b);
#endif
#ifdef STB_LIB_IMPLEMENTATION
static int stb__intcmpoffset, stb__ucharcmpoffset, stb__strcmpoffset;
static int stb__floatcmpoffset, stb__doublecmpoffset, stb__charcmpoffset;
int stb__intcmp(const void *a, const void *b)
{
const int p = *(const int *) ((const char *) a + stb__intcmpoffset);
const int q = *(const int *) ((const char *) b + stb__intcmpoffset);
return p < q ? -1 : p > q;
}
int stb__ucharcmp(const void *a, const void *b)
{
const int p = *(const unsigned char *) ((const char *) a + stb__ucharcmpoffset);
const int q = *(const unsigned char *) ((const char *) b + stb__ucharcmpoffset);
return p < q ? -1 : p > q;
}
int stb__charcmp(const void *a, const void *b)
{
const int p = *(const char *) ((const char *) a + stb__ucharcmpoffset);
const int q = *(const char *) ((const char *) b + stb__ucharcmpoffset);
return p < q ? -1 : p > q;
}
int stb__floatcmp(const void *a, const void *b)
{
const float p = *(const float *) ((const char *) a + stb__floatcmpoffset);
const float q = *(const float *) ((const char *) b + stb__floatcmpoffset);
return p < q ? -1 : p > q;
}
int stb__doublecmp(const void *a, const void *b)
{
const double p = *(const double *) ((const char *) a + stb__doublecmpoffset);
const double q = *(const double *) ((const char *) b + stb__doublecmpoffset);
return p < q ? -1 : p > q;
}
int stb__qsort_strcmp(const void *a, const void *b)
{
const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
return strcmp(p,q);
}
int stb__qsort_stricmp(const void *a, const void *b)
{
const char *p = *(const char **) ((const char *) a + stb__strcmpoffset);
const char *q = *(const char **) ((const char *) b + stb__strcmpoffset);
return stb_stricmp(p,q);
}
int (*stb_intcmp(int offset))(const void *, const void *)
{
stb__intcmpoffset = offset;
return &stb__intcmp;
}
int (*stb_ucharcmp(int offset))(const void *, const void *)
{
stb__ucharcmpoffset = offset;
return &stb__ucharcmp;
}
int (*stb_charcmp(int offset))(const void *, const void *)
{
stb__charcmpoffset = offset;
return &stb__ucharcmp;
}
int (*stb_qsort_strcmp(int offset))(const void *, const void *)
{
stb__strcmpoffset = offset;
return &stb__qsort_strcmp;
}
int (*stb_qsort_stricmp(int offset))(const void *, const void *)
{
stb__strcmpoffset = offset;
return &stb__qsort_stricmp;
}
int (*stb_floatcmp(int offset))(const void *, const void *)
{
stb__floatcmpoffset = offset;
return &stb__floatcmp;
}
int (*stb_doublecmp(int offset))(const void *, const void *)
{
stb__doublecmpoffset = offset;
return &stb__doublecmp;
}
#endif
//////////////////////////////////////////////////////////////////////////////
//
// String Processing
//
#ifndef STB_INCLUDE_STB_LIB_H
#define stb_prefixi(s,t) (0==stb_strnicmp((s),(t),strlen(t)))
enum stb_splitpath_flag
{
STB_PATH = 1,
STB_FILE = 2,
STB_EXT = 4,
STB_PATH_FILE = STB_PATH + STB_FILE,
STB_FILE_EXT = STB_FILE + STB_EXT,
STB_EXT_NO_PERIOD = 8,
};
STB_EXTERN char * stb_skipwhite(char *s);
STB_EXTERN char * stb_trimwhite(char *s);
STB_EXTERN char * stb_skipnewline(char *s);
STB_EXTERN char * stb_strncpy(char *s, char *t, int n);
STB_EXTERN char * stb_substr(char *t, int n);
STB_EXTERN char * stb_duplower(char *s);
STB_EXTERN void stb_tolower (char *s);
STB_EXTERN char * stb_strchr2 (char *s, char p1, char p2);
STB_EXTERN char * stb_strrchr2(char *s, char p1, char p2);
STB_EXTERN char * stb_strtok(char *output, char *src, char *delimit);
STB_EXTERN char * stb_strtok_keep(char *output, char *src, char *delimit);
STB_EXTERN char * stb_strtok_invert(char *output, char *src, char *allowed);
STB_EXTERN char * stb_dupreplace(char *s, char *find, char *replace);
STB_EXTERN void stb_replaceinplace(char *s, char *find, char *replace);
STB_EXTERN char * stb_splitpath(char *output, char *src, int flag);
STB_EXTERN char * stb_splitpathdup(char *src, int flag);
STB_EXTERN char * stb_replacedir(char *output, char *src, char *dir);
STB_EXTERN char * stb_replaceext(char *output, char *src, char *ext);
STB_EXTERN void stb_fixpath(char *path);
STB_EXTERN char * stb_shorten_path_readable(char *path, int max_len);
STB_EXTERN int stb_suffix (char *s, char *t);
STB_EXTERN int stb_suffixi(char *s, char *t);
STB_EXTERN int stb_prefix (char *s, char *t);
STB_EXTERN char * stb_strichr(char *s, char t);
STB_EXTERN char * stb_stristr(char *s, char *t);
STB_EXTERN int stb_prefix_count(char *s, char *t);
STB_EXTERN const char * stb_plural(int n); // "s" or ""
STB_EXTERN size_t stb_strscpy(char *d, const char *s, size_t n);
STB_EXTERN char **stb_tokens(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
STB_EXTERN char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out);
STB_EXTERN char **stb_tokens_allowempty(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_stripwhite(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_withdelim(char *src, char *delimit, int *count);
STB_EXTERN char **stb_tokens_quoted(char *src, char *delimit, int *count);
// with 'quoted', allow delimiters to appear inside quotation marks, and don't
// strip whitespace inside them (and we delete the quotation marks unless they
// appear back to back, in which case they're considered escaped)
#endif // STB_INCLUDE_STB_LIB_H
#ifdef STB_LIB_IMPLEMENTATION
#include <ctype.h>
size_t stb_strscpy(char *d, const char *s, size_t n)
{
size_t len = strlen(s);
if (len >= n) {
if (n) d[0] = 0;
return 0;
}
strcpy(d,s);
return len + 1;
}
const char *stb_plural(int n)
{
return n == 1 ? "" : "s";
}
int stb_prefix(char *s, char *t)
{
while (*t)
if (*s++ != *t++)
return 0;
return 1;
}
int stb_prefix_count(char *s, char *t)
{
int c=0;
while (*t) {
if (*s++ != *t++)
break;
++c;
}
return c;
}
int stb_suffix(char *s, char *t)
{
size_t n = strlen(s);
size_t m = strlen(t);
if (m <= n)
return 0 == strcmp(s+n-m, t);
else
return 0;
}
int stb_suffixi(char *s, char *t)
{
size_t n = strlen(s);
size_t m = strlen(t);
if (m <= n)
return 0 == stb_stricmp(s+n-m, t);
else
return 0;
}
// originally I was using this table so that I could create known sentinel
// values--e.g. change whitetable[0] to be true if I was scanning for whitespace,
// and false if I was scanning for nonwhite. I don't appear to be using that
// functionality anymore (I do for tokentable, though), so just replace it
// with isspace()
char *stb_skipwhite(char *s)
{
while (isspace((unsigned char) *s)) ++s;
return s;
}
char *stb_skipnewline(char *s)
{
if (s[0] == '\r' || s[0] == '\n') {
if (s[0]+s[1] == '\r' + '\n') ++s;
++s;
}
return s;
}
char *stb_trimwhite(char *s)
{
int i,n;
s = stb_skipwhite(s);
n = (int) strlen(s);
for (i=n-1; i >= 0; --i)
if (!isspace(s[i]))
break;
s[i+1] = 0;
return s;
}
char *stb_strncpy(char *s, char *t, int n)
{
strncpy(s,t,n);
s[n-1] = 0;
return s;
}
char *stb_substr(char *t, int n)
{
char *a;
int z = (int) strlen(t);
if (z < n) n = z;
a = (char *) malloc(n+1);
strncpy(a,t,n);
a[n] = 0;
return a;
}
char *stb_duplower(char *s)
{
char *p = strdup(s), *q = p;
while (*q) {
*q = tolower(*q);
++q;
}
return p;
}
void stb_tolower(char *s)
{
while (*s) {
*s = tolower(*s);
++s;
}
}
char *stb_strchr2(char *s, char x, char y)
{
for(; *s; ++s)
if (*s == x || *s == y)
return s;
return NULL;
}
char *stb_strrchr2(char *s, char x, char y)
{
char *r = NULL;
for(; *s; ++s)
if (*s == x || *s == y)
r = s;
return r;
}
char *stb_strichr(char *s, char t)
{
if (tolower(t) == toupper(t))
return strchr(s,t);
return stb_strchr2(s, (char) tolower(t), (char) toupper(t));
}
char *stb_stristr(char *s, char *t)
{
size_t n = strlen(t);
char *z;
if (n==0) return s;
while ((z = stb_strichr(s, *t)) != NULL) {
if (0==stb_strnicmp(z, t, n))
return z;
s = z+1;
}
return NULL;
}
static char *stb_strtok_raw(char *output, char *src, char *delimit, int keep, int invert)
{
if (invert) {
while (*src && strchr(delimit, *src) != NULL) {
*output++ = *src++;
}
} else {
while (*src && strchr(delimit, *src) == NULL) {
*output++ = *src++;
}
}
*output = 0;
if (keep)
return src;
else
return *src ? src+1 : src;
}
char *stb_strtok(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 0, 0);
}
char *stb_strtok_keep(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 1, 0);
}
char *stb_strtok_invert(char *output, char *src, char *delimit)
{
return stb_strtok_raw(output, src, delimit, 1,1);
}
static char **stb_tokens_raw(char *src_, char *delimit, int *count,
int stripwhite, int allow_empty, char *start, char *end)
{
int nested = 0;
unsigned char *src = (unsigned char *) src_;
static char stb_tokentable[256]; // rely on static initializion to 0
static char stable[256],etable[256];
char *out;
char **result;
int num=0;
unsigned char *s;
s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 1;
if (start) {
s = (unsigned char *) start; while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
}
stable[0] = 1;
// two passes through: the first time, counting how many
s = (unsigned char *) src;
while (*s) {
// state: just found delimiter
// skip further delimiters
if (!allow_empty) {
stb_tokentable[0] = 0;
while (stb_tokentable[*s])
++s;
if (!*s) break;
}
++num;
// skip further non-delimiters
stb_tokentable[0] = 1;
if (stripwhite == 2) { // quoted strings
while (!stb_tokentable[*s]) {
if (*s != '"')
++s;
else {
++s;
if (*s == '"')
++s; // "" -> ", not start a string
else {
// begin a string
while (*s) {
if (s[0] == '"') {
if (s[1] == '"') s += 2; // "" -> "
else { ++s; break; } // terminating "
} else
++s;
}
}
}
}
} else
while (nested || !stb_tokentable[*s]) {
if (stable[*s]) {
if (!*s) break;
if (end ? etable[*s] : nested)
--nested;
else
++nested;
}
++s;
}
if (allow_empty) {
if (*s) ++s;
}
}
// now num has the actual count... malloc our output structure
// need space for all the strings: strings won't be any longer than
// original input, since for every '\0' there's at least one delimiter
result = (char **) malloc(sizeof(*result) * (num+1) + (s-src+1));
if (result == NULL) return result;
out = (char *) (result + (num+1));
// second pass: copy out the data
s = (unsigned char *) src;
num = 0;
nested = 0;
while (*s) {
char *last_nonwhite;
// state: just found delimiter
// skip further delimiters
if (!allow_empty) {
stb_tokentable[0] = 0;
if (stripwhite)
while (stb_tokentable[*s] || isspace(*s))
++s;
else
while (stb_tokentable[*s])
++s;
} else if (stripwhite) {
while (isspace(*s)) ++s;
}
if (!*s) break;
// we're past any leading delimiters and whitespace
result[num] = out;
++num;
// copy non-delimiters
stb_tokentable[0] = 1;
last_nonwhite = out-1;
if (stripwhite == 2) {
while (!stb_tokentable[*s]) {
if (*s != '"') {
if (!isspace(*s)) last_nonwhite = out;
*out++ = *s++;
} else {
++s;
if (*s == '"') {
if (!isspace(*s)) last_nonwhite = out;
*out++ = *s++; // "" -> ", not start string
} else {
// begin a quoted string
while (*s) {
if (s[0] == '"') {
if (s[1] == '"') { *out++ = *s; s += 2; }
else { ++s; break; } // terminating "
} else
*out++ = *s++;
}
last_nonwhite = out-1; // all in quotes counts as non-white
}
}
}
} else {
while (nested || !stb_tokentable[*s]) {
if (!isspace(*s)) last_nonwhite = out;
if (stable[*s]) {
if (!*s) break;
if (end ? etable[*s] : nested)
--nested;
else
++nested;
}
*out++ = *s++;
}
}
if (stripwhite) // rewind to last non-whitespace char
out = last_nonwhite+1;
*out++ = '\0';
if (*s) ++s; // skip delimiter
}
s = (unsigned char *) delimit; while (*s) stb_tokentable[*s++] = 0;
if (start) {
s = (unsigned char *) start; while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) stable[*s++] = 1;
s = (unsigned char *) end; if (s) while (*s) etable[*s++] = 1;
}
if (count != NULL) *count = num;
result[num] = 0;
return result;
}
char **stb_tokens(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,0,0,0,0);
}
char **stb_tokens_nested(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
{
return stb_tokens_raw(src,delimit,count,0,0,nest_in,nest_out);
}
char **stb_tokens_nested_empty(char *src, char *delimit, int *count, char *nest_in, char *nest_out)
{
return stb_tokens_raw(src,delimit,count,0,1,nest_in,nest_out);
}
char **stb_tokens_allowempty(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,0,1,0,0);
}
char **stb_tokens_stripwhite(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,1,1,0,0);
}
char **stb_tokens_quoted(char *src, char *delimit, int *count)
{
return stb_tokens_raw(src,delimit,count,2,1,0,0);
}
char *stb_dupreplace(char *src, char *find, char *replace)
{
size_t len_find = strlen(find);
size_t len_replace = strlen(replace);
int count = 0;
char *s,*p,*q;
s = strstr(src, find);
if (s == NULL) return strdup(src);
do {
++count;
s = strstr(s + len_find, find);
} while (s != NULL);
p = (char *) malloc(strlen(src) + count * (len_replace - len_find) + 1);
if (p == NULL) return p;
q = p;
s = src;
for (;;) {
char *t = strstr(s, find);
if (t == NULL) {
strcpy(q,s);
assert(strlen(p) == strlen(src) + count*(len_replace-len_find));
return p;
}
memcpy(q, s, t-s);
q += t-s;
memcpy(q, replace, len_replace);
q += len_replace;
s = t + len_find;
}
}
void stb_replaceinplace(char *src, char *find, char *replace)
{
size_t len_find = strlen(find);
size_t len_replace = strlen(replace);
int delta;
char *s,*p,*q;
delta = len_replace - len_find;