-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmol_utils.h
More file actions
1281 lines (1006 loc) · 38.3 KB
/
Copy pathsmol_utils.h
File metadata and controls
1281 lines (1006 loc) · 38.3 KB
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
/*
Copyright © 2023 Marko Ranta (Discord: coderunner)
This software is provided *as-is*, without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef SMOL_UTILS_H
#define SMOL_UTILS_H
#if defined(_WIN32)
# ifndef SMOL_PLATFORM_WINDOWS
# define SMOL_PLATFORM_WINDOWS
# endif
# define WIN32_LEAN_AND_MEAN
# include <Windows.h>
#elif defined(__linux__)
# ifndef SMOL_PLATFORM_LINUX
# define SMOL_PLATFORM_LINUX
# endif
# define _XOPEN_SOURCE 500
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <sys/time.h>
# include <time.h>
# include <fcntl.h>
# include <dirent.h>
#elif defined(__APPLE__)
# define SMOL_PLATFORM_MAC_OS
//TODO:
# error Mac OS backend not implemented yet!
#elif defined(__EMSCRIPTEN__)
# ifndef SMOL_PLATFORM_EMSCRIPTEN
# define SMOL_PLATFORM_EMSCRIPTEN
# endif
# include <emscripten.h>
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define SMOL_RAND_MAX 0x7FFFFFFF
#if defined(_WIN64) || defined(__linux__)
typedef unsigned long long smol_size_t;
#else
typedef unsigned int smol_size_t;
#endif
typedef void* smol_file_scan_session_t;
typedef struct _smol_file_info smol_file_info_t;
#ifndef SMOL_TRUE
#define SMOL_TRUE 1
#endif
#ifndef SMOL_FALSE
#define SMOL_FALSE 0
#endif
#ifdef _MSC_VER
# ifndef SMOL_INLINE
# define SMOL_INLINE __forceinline
# endif
# define SMOL_THREAD_LOCAL __declspec(thread)
#else
# ifndef SMOL_INLINE
# define SMOL_INLINE inline __attribute__((always_inline))
# endif
# define SMOL_THREAD_LOCAL __thread
#endif
#ifndef SMOL_ALLOC
#define SMOL_ALLOC( size ) malloc(size)
#endif
#ifndef SMOL_FREE
#define SMOL_FREE( ptr ) free(ptr)
#endif
#ifndef SMOL_REALLOC
#define SMOL_REALLOC( old_ptr, new_size ) realloc(old_ptr, new_size)
#endif
#ifndef smol_offset_of
# define smol_offset_of(Type, Field) ((void*)&(((Type*)0)->Field))
#endif
//smol_timer - Returns high precision system up time in seconds on
// Windows, and high precision time since Unix epoch on Linux.
//Returns: double - containing seconds.microseconds since the last start
// of the computer (win32) or since unix epoch (on linux)
double smol_timer();
/* --------------------------------------- */
/* SOME UNICODE CONVERSION FUNCTIONALITY */
/* --------------------------------------- */
//smol_utf8_to_utf32 - Converts utf8 encoded character into utf32 codepoint
//Arguments:
// - const char* utf8 -- utf8 encoded bytes
// - unsigned int* utf32 -- the codepoint
//Returns: int -- number of utf8 bytes handled
int smol_utf8_to_utf32(const char* utf8, unsigned int* utf32);
//smol_utf32_to_utf8 - Converts utf32 codepoint into utf8 encoded character
//Arguments:
// - unsigned int utf32 -- utf32 codepoint
// - int buf_len -- the number of bytes available in the result buffer
// - char* utf8 -- the buffer where the result is stored
//Returns: int -- number of utf8 bytes written
int smol_utf32_to_utf8(unsigned int utf32, int buf_len, char* utf8);
//smol_utf16_to_utf32 - Converts utf8 encoded character into utf32 codepoint
//Arguments:
// - const unsigned short* utf8 -- utf16 encoded bytes
// - unsigned int* utf32 -- the codepoint
//Returns: int -- number of utf16 symbols handled
int smol_utf16_to_utf32(const unsigned short* utf16, unsigned int* utf32);
//smol_utf32_to_utf16 - Converts utf32 codepoint into utf16 encoded character
//Arguments:
// - unsigned int utf32 -- utf32 codepoint
// - int buf_len -- the number of ushorts available in the result buffer
// - unsigned short* utf16 -- the buffer where the result is stored
//Returns: int -- number of utf16 symbols written
int smol_utf32_to_utf16(unsigned int utf32, int buf_len, unsigned short* utf16);
//smol_utf16_to_utf8 - Converts utf16 encoded character into utf8 encoded character
//Arguments:
// - const unsigned short* utf16 -- utf16 encoded bytes
// - int buf_len -- the number of bytes available in the result buffer
// - char* utf8 -- the buffer where the result is stored
//Returns: int -- number of utf8 bytes written
int smol_utf16_to_utf8(const unsigned short* utf16, int buf_len, char* utf8);
//smol_utf8_to_utf16 - Converts utf8 encoded character into utf16 encoded character
//Arguments:
// - const char* utf8 -- utf8 character
// - int buf_len -- the number of ushorts available in the result buffer
// - unsigned short* utf16 -- the buffer where the result is stored
//Returns: int -- number of utf16 symbols written
int smol_utf8_to_utf16(const char* utf8, int buf_len, unsigned short* utf16);
/* ---------------------------------------------- */
/* A SORTING UTILITITY BECAUSE QSORT WON'T CUT IT */
/* ---------------------------------------------- */
typedef int(*smol_sort_proc)(const void* a, const void* b, void* user_data);
void smol_sort(void* data, int num_elements, int element_size, smol_sort_proc compare, void* user_data);
#define smol_sort_array_user(data, compare, user_data) smol_sort(data, sizeof(data) / sizeof(*data), sizeof(*data), compare, user_data)
#define smol_sort_array(data, compare) smol_sort_array_user(data, compare, NULL)
#define smol_sort_vector_user(vec, compare, user_data) smol_sort((void*)((vec)->data), (vec)->count, sizeof(*(vec)->data), compare, user_data)
#define smol_sort_vector(vec, compare) smol_sort_vector_user(vec, compare, NULL)
/* --------------------------------------- */
/* A RANDOM NUMBER GENERATOR FUNCTIONALITY */
/* --------------------------------------- */
//smol_randomize - Randomizes the random generator
//Arguments:
// - int seed -- The random seed
void smol_randomize(unsigned int seed);
//smol_rand - Returns a random integer between [0..SMOL_RAND_MAX)
unsigned int smol_rand();
//smol_randf - Returns a random float between [0..1)
float smol_randf();
//smol_rnd - Returns an exclusive random integer berween [minimum...maximum)
//Arguments:
// - int minimum - The lowest number in the range
// - int maximum - The highhest number-1 in the range
//Returns int - containing the random number
int smol_rnd(int minimum, int maximum);
//smol_rnd - Returns an exclusive random float berween [minimum...maximum)
//Arguments:
// - float minimum - The lowest number in the range
// - float maximum - The highest number-epsilon in the range (epsilon = FLT_EPSILON)
//Returns: float - containing the random number
float smol_rndf(float minimum, float maximum);
/* --------------------------------------- */
/* SOME MATHEMATICAL UTILITY FUNCTIONS */
/* --------------------------------------- */
//smol_remapf - Remaps a float value between old range into between a new range
//Arguments:
// - float value -- The value that's being remapped
// - float low -- The old range's lowest value
// - float high -- The old range's highest value
// - float new_low -- The new range's lowest value
// - float new_high -- The new range's highest value
//Returns: float - containing the mapped value
SMOL_INLINE float smol_remapf(float value, float low, float high, float new_low, float new_high) {
return ((value - low) / (high - low)) * (new_high - new_low) + new_low;
}
//smol_remapf - Remaps a double value between old range into between a new range
//Arguments:
// - double value -- The value that's being remapped
// - double low -- The old range's lowest value
// - double high -- The old range's highest value
// - double new_low -- The new range's lowest value
// - double new_high -- The new range's highest value
//Returns: double - containing the mapped value
SMOL_INLINE float smol_remapd(double value, double low, double high, double new_low, double new_high) {
return ((value - low) / (high - low)) * (new_high - new_low) + new_low;
}
//smol_mixf - Linearily interpolates two values
//Arguments:
// - float a -- The value function returns when t = 0.0
// - float b -- The value function returns when t = 1.0
//Returns: float - containing the result
SMOL_INLINE float smol_mixf(float a, float b, float t) {
float r = 1.f - t;
return (a * t) + (b * t);
}
//smol_mixf - Linearily interpolates two values, but t is clamped to 0 and 1
//Arguments:
// - float a -- The value function returns when t = 0.0 or below
// - float b -- The value function returns when t = 1.0 or above
//Returns: float - containing the result
SMOL_INLINE float smol_clamped_mixf(float a, float b, float t) {
if(t <= 0.f) return a;
if(t >= 1.f) return b;
float r = 1.f - t;
return (a * t) + (b * t);
}
//smol_mixf - Linearily interpolates two values
//Arguments:
// - double a -- The value function returns when t = 0.0
// - double b -- The value function returns when t = 1.0
//Returns: double - containing the result
SMOL_INLINE double smol_mixd(double a, double b, double t) {
double r = 1. - t;
return (a * t) + (b * t);
}
//smol_mixf - Linearily interpolates two values, but t is clamped to 0 and 1
//Arguments:
// - double a -- The value function returns when t = 0.0 or below
// - double b -- The value function returns when t = 1.0 or above
//Returns: double - containing the result
SMOL_INLINE double smol_clamped_mixd(double a, double b, double t) {
if(t <= 0.f) return a;
if(t >= 1.f) return b;
double r = 1. - t;
return (a * t) + (b * t);
}
//smol_clampf - Clamps value between [low..high] range
//Arguments:
// - float v -- The value to be clamped
// - float low -- The lower bound of the clamp range
// - float high -- The higher bound of the clamp range
//Returns: float - containing the clamped value
SMOL_INLINE float smol_clampf(float v, float low, float high) {
if(v < low) return low;
if(v > high) return high;
return v;
}
//smol_clampd - Clamps value between [low..high] range
//Arguments:
// - double v -- The value to be clamped
// - double low -- The lower bound of the clamp range
// - double high -- The higher bound of the clamp range
//Returns: float - containing the clamped value
SMOL_INLINE double smol_clampd(double v, double low, double high) {
if(v < low) return low;
if(v > high) return high;
return v;
}
//smol_linear_stepf - Remaps value between [e0...e1] range and normalizes the result by e0..e1 range.
//Arguments:
// - float e0 -- The edge0
// - float e1 -- The edge1
// - float value -- The value remapped, normalized and clamped.
//Returns: float - containing the value between [0..1]
SMOL_INLINE float smol_linear_stepf(float e0, float e1, float value) {
return smol_clampf((value - e0) / (e1 - e0), 0.f, 1.f);
}
//smol_linear_stepd - Remaps value between [e0...e1] range and normalizes the result by e0..e1 range.
//Arguments:
// - double e0 -- The edge0
// - double e1 -- The edge1
// - double value -- The value remapped, normalized and clamped.
//Returns: double - containing the value between [0..1]
SMOL_INLINE double smol_linear_stepd(double e0, double e1, double value) {
return smol_clampd((value - e0) / (e1 - e0), 0., 1.);
}
//smol_smooth_stepf - Same as smol_linear_step, but applies a curve.
//Arguments:
// - float e0 -- The edge0
// - float e1 -- The edge1
// - float value -- The value remapped, normalized and clamped.
//Returns: float - containing the value between [0..1]
SMOL_INLINE float smol_smooth_stepf(float e0, float e1, float value) {
float res = smol_clampd((value - e0) / (e1 - e0), 0.f, 1.f);
res = (res * res * (3.f - 2.f) * res);
return res;
}
//smol_smooth_stepd - Same as smol_linear_step, but applies a curve.
//Arguments:
// - double e0 -- The edge0
// - double e1 -- The edge1
// - double value -- The value remapped, normalized and clamped.
//Returns: double - containing the value between [0..1]
SMOL_INLINE double smol_smooth_stepd(double e0, double e1, double value) {
double res = smol_clampd((value - e0) / (e1 - e0), 0., 1.);
res = (res * res * (3. - 2.) * res);
return res;
}
/* --------------------------------------- */
/* Dynamic Array (std::vector) like stuff */
/* --------------------------------------- */
//This macro can be used to define a vector type, or declare a local variable: smol_vector(int) int_vec;
#define smol_vector(type) \
struct { \
int allocation;\
int count; \
type* data; \
}
//smol_vector_init - Init a vector
//Arguments:
// - vec -- The vector to be initialized
// - init_alloc -- The initial allocation of the vector
#define smol_vector_init(vec, init_alloc) { \
(vec)->allocation = init_alloc; \
(vec)->count = 0; \
((void**)(&(vec)->data))[0] = SMOL_ALLOC(sizeof(*((vec)->data))*init_alloc); \
} (void)0
//smol_vector_push - Pushes an object into a vector
//Arguments:
// - vec -- The vector to be appended
// - value -- The element to be added
#define smol_vector_push(vec, value) { \
if((vec)->count >= (vec)->allocation) { \
(vec)->allocation *= 2; \
((void**)&(vec)->data)[0] = SMOL_REALLOC((vec)->data, sizeof(*((vec)->data)) * (vec)->allocation ); \
} \
SMOL_ASSERT((vec)->data); \
(vec)->data[(vec)->count++] = (value); \
} (void)0
#define smol_vector_resize(vec, size) { \
if(size > (vec)->allocation) { \
(vec)->allocation = ((size / (vec)->allocation)+1)*(vec)->allocation; \
((void**)&(vec)->data)[0] = SMOL_REALLOC((vec)->data, sizeof(*((vec)->data)) * (vec)->allocation); \
} \
(vec)->count = size; \
}
//smol_vector_pop - Pops the last object from the vector
//Arguments:
// - vec -- The vector to be popped from
//Returns type - containing the last object in the vector
#define smol_vector_pop(vec) \
(vec)->data[--((vec)->count)]
//smol_vector_front - Returns the front element of the vector
// - vec -- The vector to get the first element from
//Returns type - containing the first object in the vector
#define smol_vector_front(vec) \
(vec)->data[0]
//smol_vector_front - Returns the last element of the vector
// - vec -- The vector to get the last element from
//Returns type - containing the last object in the vector
#define smol_vector_back(vec) \
(vec)->data[(vec)->count-1]
//smol_vector_iterate - Iterates over vector elements
//Arguments:
// - vec - The vector to be iterated over
// - it - Variable name for the iterator
//Returns: type* - Containing the buffer pointer to be indexed in
#define smol_vector_iterate(vec, it) \
(vec)->data; \
SMOL_ASSERT((vec)->data); \
for(int it = 0; it < (vec)->count; it++)
//smol_vector_each - Iterates over each element
//Arguments:
// - vec - The vector to be iterated over
// - element_type - A type of individual element in the vector (MSVC doesn't have __typeof__ *sigh*)
// - it - Iterator variable name
#define smol_vector_each(vec, element_type, it) \
for(element_type* it = (vec)->data; (vec)->data && it != &((vec)->data[(vec)->count]); it++)
//smol_vector_clear - Clears the vector
//Arguments:
// - vec -- The vector to be cleared
#define smol_vector_clear(vec) { \
SMOL_ASSERT((vec)->data); \
((vec)->count = 0); \
} (void)0
//smol_vector_remove - Removes an element from the vector, by overwriting it with the last element, and decreasing vector size
//Arguments:
// - vec -- The vector to remove element from
// - element - The element index to be removed
#define smol_vector_remove(vec, element) { \
SMOL_ASSERT((vec)->data); \
(vec)->data[element] = (vec)->data[--(vec)->count]; \
} (void)0
//smol_vector_count - "Returns" the number of elements in the vector
//Arguments:
// - vec -- The vector you want count of
//Returns int - containing the number of elements in the vector
#define smol_vector_count(vec) \
((vec)->count)
//smol_vector_data - "Returns" the data buffer of the vector
// - vector -- The span you want the data of
//Returns type* - containing the pointer to the vector data
#define smol_vector_data(vec) \
((vec)->data)
//smol_span_at - "Returns" an element of the vector. NOT BOUNDS CHECKED.
// - vec -- The vector you want the element of
// - index -- The index of the element
//Returns type - containing the element at index
#define smol_vector_at(vec, index) \
((vec)->data[index])
//Frees a vector, and sets it's allocation and count to zero
#define smol_vector_free(vec) { \
if((vec)->data) { \
SMOL_FREE((void*)(vec)->data); \
(vec)->data = NULL; \
} \
(vec)->count = 0; \
(vec)->allocation = 0; \
} (void)0
//smol_vector_allocation - Retrieves vector allocation
//Arguments:
// - vec -- The vector you want alocation of
//Returns: int - containing the number of elements allocated in the vector.
#define smol_vector_allocation(vec) \
(vec)->allocation
/* --------------- */
/* Span like stuff */
/* --------------- */
//This macro can be used to define a span type, or for local variable similarily as smol_vector() macro works.
#define smol_span(type) \
struct { \
int count; \
type* data; \
}
//smol_span_init_from_slice - Initializes span from "span_like" object (vector for example)
//Arguments:
// - span -- The span you're initializing
// - span_like -- The data you're basing this span on
// - first_element -- Index of the first element
// - element_count -- Number of elements this span should contain
#define smol_span_init_from_slice(span, span_like, first_element, element_count) { \
(span)->count = (element_count); \
(span)->data = &((span_like)->data[first_element]); \
} (void)0
//smol_span_init_from_spanlike - Initializes span from "span_like" object (vector / span)
//Arguments:
// - span -- The span you're initializing
// - span_like -- The data you're basing this span on
#define smol_span_init_from_spanlike(span, span_like) { \
(span)->count = (span_like)->count; \
(span)->data = &((span_like)->data[0]); \
} (void)0
//smol_span_count - "Returns" the number of elements in the span
//Arguments:
// - span -- The span you want count of
//Returns int - containing the number of elements in the span
#define smol_span_count(span_like) \
((span_like)->count)
//smol_span_data - "Returns" the data buffer of the span
// - span -- The span you want the data of
//Returns type* - containing the pointer to the span data
#define smol_span_data(span_like) \
((span_like)->data)
//smol_span_at - "Returns" an element of the span. NOT BOUNDS CHECKED.
// - span -- The span you want the element of
// - index -- The index of the element
//Returns type - containing the element at index
#define smol_span_at(span_like, index) \
((span_like)->data[index])
/* ---------------------- */
/* QUEUE STUFF */
/* -----------------------*/
#define smol_queue(type) \
struct { \
int allocation; \
int first; \
int last; \
type* data; \
}
#define smol_queue_init(queue, initial_alloc) { \
(queue)->allocation = initial_alloc; \
(void*)((queue)->data) = SMOL_ALLOC(sizeof(*((queue)->data))*initial_alloc); \
(queue)->first = 0; \
(queue)->last = 0; \
} (void)0
#define smol_queue_free(queue) {\
if((queue)->data) SMOL_FREE((void*)((queue)->data)); \
(queue)->allocation = 0; \
(queue)->first = 0; \
(queue)->last = 0; \
} (void)0
#define smol_queue_clear(queue) \
((queue)->first = (queue)->last = 0)
#define smol_queue_count(queue) \
((queue)->last - (queue)->first)
#define smol_queue_push_back(queue, value) { \
if(smol_queue_count(queue) >= (queue)->allocation) { \
(queue)->allocation <<= 1; \
(void*)((queue)->data) = SMOL_REALLOC((void*)((queue)->data), sizeof(*((queue)->data))*(queue)->allocation); \
} \
(queue)->data[((queue)->allocation + (queue)->last++) % (queue)->allocation] = value; \
} (void)0
#define smol_queue_push_front(queue, value) { \
if(smol_queue_count(queue) >= (queue)->allocation) { \
(queue)->allocation <<= 1; \
(void*)((queue)->data) = SMOL_REALLOC((void*)((queue)->data), sizeof(*((queue)->data))*(queue)->allocation); \
} \
(queue)->data[((queue)->allocation + (queue)->first) % (queue)->allocation] = value; \
} (void)0
#define smol_queue_at(queue, index) \
((queue)->data[((queue)->allocation + (queue)->first + index) % (queue)->allocation])
#define smol_queue_pop_front(queue) {\
((queue)->data[((queue)->allocation + (queue)->first++) % (queue)->allocation]); \
if((queue)->first == (queue)->last) smol_queue_clear(queue); \
} (void)0
#define smol_queue_pop_back(queue) { \
((queue)->data[((queue)->allocation + --(queue)->last) % (queue)->allocation]); \
if((queue)->first == (queue)->last) smol_queue_clear(queue); \
} (void)0
#define smol_queue_front(queue) \
((queue)->data[((queue)->allocation + (queue)->first) % (queue)->allocation])
#define smol_queue_back(queue) \
((queue)->data[((queue)->allocation + ((queue)->last - 1)) % (queue)->allocation])
/* ------------------------------ */
/* SOME FILE SYSTEM FUNCTIONALITY */
/* ------------------------------ */
typedef struct _smol_file_info {
char is_folder;
char file_path[512];
} smol_file_info_t;
//smol_read_entire_file - Opens a file and reads it into a buffer.
//Arguments:
// - const char* file_path -- A path to a file
// - unsigned long long* size -- A pointer to 64bit-uint containing number of bytes read
//Returns: void* - a pointer to the buffer
void* smol_read_entire_file(const char* file_path, smol_size_t* size);
//smol_get_current_directory - Gets the current directory
//Returns: const char* - A pointer to static variable within the function where current directory path is stored.
const char* smol_get_current_directory(void);
//smol_change_directory - Changes current directory
//Arguments:
//const char* path - Relative / Absolute Path to navigate to
//Returns: int - If change was successful or not
int smol_change_directory(const char* path);
//smol_start_file_scan_session - Returns an exclusive random float berween [minimum...maximum)
//Arguments:
// - smol_file_info_t* -- A pointer to smol_file_info_t structure, contains the first file found in the current folder
//Returns: smol_file_scan_session_t - A handle to the file system scan session
smol_file_scan_session_t smol_start_file_scan_session(smol_file_info_t* info);
//smol_start_file_scan_session - Returns an exclusive random float berween [minimum...maximum)
//Arguments:
// - smol_file_scan_session_t -- A file scan session handle
// - smol_file_info_t* -- A pointer to smol_file_info_t structure, contains the next file found in the current folder
//Returns: smol_file_scan_session_t - A handle to the file system scan session
int smol_file_scan_session_next(smol_file_scan_session_t session, smol_file_info_t* info);
#ifdef SMOL_UTILS_IMPLEMENTATION
#ifdef _MSC_VER
# ifndef SMOL_BREAKPOINT
# define SMOL_BREAKPOINT __debugbreak //MSVC uses this
# endif
#else
# ifndef SMOL_BREAKPOINT
# define SMOL_BREAKPOINT __builtin_trap //Clang and GCC uses this, AFAIK
# endif
#endif
#ifndef SMOL_SYMBOLIFY
#define SMOL_SYMBOLIFY(x) #x
#endif
#ifndef SMOL_STRINGIFY
#define SMOL_STRINGIFY( x ) SMOL_SYMBOLIFY(x)
#endif
#ifndef SMOL_ASSERT
#define SMOL_ASSERT(condition) \
if(!(condition)) \
puts(\
"SMOL FRAME ASSERTION FAILED!\n" \
"CONDITION: " #condition "\n" \
"IN FILE: '" __FILE__ "'\n" \
"ON LINE: " SMOL_STRINGIFY(__LINE__) "\n" \
), \
SMOL_BREAKPOINT()
#endif
#pragma region Unicode stuff
//https://en.wikipedia.org/wiki/UTF-8#Encoding
int smol_utf8_to_utf32(const char* utf8, unsigned int* utf32) {
int clen = 0;
if((utf8[0] & 0x80) == 0x00) clen = 1;
if((utf8[0] & 0xC0) == 0xC0) clen = 2;
if((utf8[0] & 0xE0) == 0xE0) clen = 3;
if((utf8[0] & 0xF0) == 0xF0) clen = 4;
if((utf8[0] & 0xF8) == 0xF8) clen = 5;
if((utf8[0] & 0xFC) == 0xFC) clen = 6;
switch(clen) {
case 1: *utf32 = (utf8[0] & 0x7F); break;
case 2: *utf32 = (utf8[0] & 0x1F) << 0x06 | (utf8[1] & 0x3F); break;
case 3: *utf32 = (utf8[0] & 0x0F) << 0x0C | (utf8[1] & 0x3F) << 0x06 | (utf8[2] & 0x3F); break;
case 4: *utf32 = (utf8[0] & 0x07) << 0x12 | (utf8[1] & 0x3F) << 0x0C | (utf8[2] & 0x3F) << 0x06 | (utf8[3] & 0x3F); break;
case 5: *utf32 = (utf8[0] & 0x03) << 0x18 | (utf8[1] & 0x3F) << 0x12 | (utf8[2] & 0x3F) << 0x0C | (utf8[3] & 0x3F) << 0x06 | (utf8[4] & 0x3F); break;
case 6: *utf32 = (utf8[0] & 0x01) << 0x1E | (utf8[1] & 0x3F) << 0x18 | (utf8[2] & 0x3F) << 0x12 | (utf8[3] & 0x3F) << 0x12 | (utf8[4] & 0x3F) << 0x06 | (utf8[5] & 0x3F); break;
}
return clen;
}
int smol_utf32_to_utf8(unsigned int utf32, int buf_len, char* utf8) {
int clen = 1;
if(utf32 >= 0x0000080) clen = 2;
if(utf32 >= 0x0000800) clen = 3;
if(utf32 >= 0x0010000) clen = 4;
if(utf32 >= 0x010F800) clen = 5;
if(utf32 >= 0x3FFFFFF) clen = 6;
if(clen > buf_len) return 0;
char* buf_byte = utf8;
switch(clen) {
case 0:
break;
case 1:
*buf_byte++ = 0x00 | ((utf32 >> 0x00) & 0xFF);
break;
case 2:
*buf_byte++ = 0xC0 | ((utf32 >> 0x06) & 0x1F);
*buf_byte++ = 0x80 | ((utf32 >> 0x00) & 0x3F);
break;
case 3:
*buf_byte++ = 0xE0 | ((utf32 >> 0x0C) & 0x0F);
*buf_byte++ = 0x80 | ((utf32 >> 0x06) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x00) & 0x3F);
break;
case 4:
*buf_byte++ = 0xF0 | ((utf32 >> 0x12) & 0x07);
*buf_byte++ = 0x80 | ((utf32 >> 0x0C) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x06) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x00) & 0x3F);
break;
case 5:
*buf_byte++ = 0xF8 | ((utf32 >> 0x18) & 0x03);
*buf_byte++ = 0x80 | ((utf32 >> 0x12) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x0C) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x06) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x00) & 0x3F);
break;
case 6:
*buf_byte++ = 0xFC | ((utf32 >> 0x1F) & 0x01);
*buf_byte++ = 0x80 | ((utf32 >> 0x18) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x12) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x0C) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x06) & 0x3F);
*buf_byte++ = 0x80 | ((utf32 >> 0x00) & 0x3F);
break;
}
return clen;
}
//https://en.wikipedia.org/wiki/UTF-16#Examples
int smol_utf16_to_utf32(const unsigned short* utf16, unsigned int* utf32) {
utf32[0] = 0;
if(utf16[0] >= 0xD800) {
utf32[0] |= (unsigned int)(utf16[0] - 0xD800) << 0xA;
utf32[0] |= (unsigned int)(utf16[1] - 0xDC00) << 0x0;
utf32[0] += 0x10000;
return 2;
} else {
utf32[0] = (unsigned int)utf16[0];
return 1;
}
return 0;
}
int smol_utf32_to_utf16(unsigned int utf32, int buf_len, unsigned short* utf16) {
if(utf32 > 0x10000) {
if(2 > buf_len) return 0;
utf32 -= 0x10000;
utf16[0] = (unsigned short)(utf32 >> 0x0A) + 0xD800;
utf16[1] = (unsigned short)(utf32 & 0x3FF) + 0xDC00;
return 2;
} else {
utf16[0] = (unsigned short)utf32;
return 1;
}
return 0;
}
int smol_utf16_to_utf8(const unsigned short* utf16, int buf_len, char* utf8) {
unsigned int utf32 = 0;
if(smol_utf16_to_utf32(utf16, &utf32) == 0)
return 0;
return smol_utf32_to_utf8(utf32, buf_len, utf8);
}
int smol_utf8_to_utf16(const char* utf8, int buf_len, unsigned short* utf16) {
unsigned int utf32 = 0;
if(smol_utf8_to_utf32(utf8, &utf32))
return 0;
return smol_utf32_to_utf16(utf32, buf_len, utf16);
}
#pragma endregion
#pragma region Sorting utilities
void smol_sort(void* data, int num_elements, int element_size, smol_sort_proc compare, void* user_data) {
char* first = ((char*)data);
char* last = first + (num_elements * element_size);
#ifdef _MSC_VER
#define SMOL_SORT_SWAP(a, b) { \
char* tmp = alloca(element_size); \
memcpy((void*)tmp, (const void*)a, element_size); \
memcpy((void*)a, (const void*)b, element_size); \
memcpy((void*)b, (const void*)tmp, element_size); \
}
#else
#define SMOL_SORT_SWAP(a, b) { \
char tmp[element_size]; \
memcpy(tmp, a, element_size); \
memcpy(a, b, element_size); \
memcpy(b, tmp, element_size); \
}
#endif
if((last - first) > element_size) {
char* part_idx = first;
{
char* pivot = last - element_size;
for(char* it = first; it != last; it += element_size) {
if(compare((const void*)pivot, (const void*)it, user_data) > 0) {
SMOL_SORT_SWAP(part_idx, it);
part_idx += element_size;
}
}
SMOL_SORT_SWAP(part_idx, pivot);
}
smol_sort((void*)first, (part_idx - first) / element_size, element_size, compare, user_data);
smol_sort((void*)(part_idx + element_size), (last - (part_idx + element_size)) / element_size, element_size, compare, user_data);
}
#undef SMOL_SORT_SWAP
}
#pragma endregion
#pragma region Linear Congruential PRNG
static unsigned int smol__rand_state;
void smol_randomize(unsigned int seed) {
smol__rand_state = seed;
}
//Le zoinked from here https://www.sanfoundry.com/c-program-implement-linear-congruential-generator-pseudo-random-number-generation/
unsigned int smol_rand() {
return (smol__rand_state = (smol__rand_state * 1103515245 + 12345) & SMOL_RAND_MAX);
}
float smol_randf() {
unsigned int bits = 0x3F800000 | (smol_rand() >> 8);
return (*((float*)&bits)) - 1.f;
}
int smol_rnd(int minimum, int maximum) {
return minimum + smol_rand() % (maximum - minimum);
}
float smol_rndf(float minimum, float maximum) {
return minimum + smol_randf() * (maximum - minimum);
}
#pragma endregion
#if defined(SMOL_PLATFORM_WINDOWS)
//A nasty global :|
double smol__perf_freq;
double smol_timer(void) {
if(smol__perf_freq == 0.0) {
LARGE_INTEGER freq;
if(QueryPerformanceFrequency(&freq) == TRUE) {
smol__perf_freq = 1.0 / (double)freq.QuadPart;
} else {
SMOL_ASSERT(!"Failed to query perfomance frequency!");
return 0.;
}
}
LARGE_INTEGER ctr = {0};
SMOL_ASSERT("Failed to query performance counter!" && QueryPerformanceCounter(&ctr));
return (double)ctr.QuadPart * smol__perf_freq;
}
const char* smol_get_current_directory(void) {
static char buffer[512] = { 0 };
#ifdef UNICODE
{
wchar_t buf[512];
GetCurrentDirectory(512, buf);
BOOL subst = FALSE;
WideCharToMultiByte(CP_UTF8, 0, buf, lstrlenW(buf), buffer, 1024, "?", &subst);
}
#else
GetCurrentDirectory(512, buffer);
#endif
return buffer;
}
int smol_change_directory(const char* dir) {
#ifdef UNICODE
wchar_t path[512] = { 0 };
MultiByteToWideChar(CP_UTF8, MB_COMPOSITE, dir, strlen(dir), path, 512);
#else
const char* path = dir;
#endif
return SetCurrentDirectory(path);
}
smol_file_scan_session_t smol_start_file_scan_session(smol_file_info_t* info) {
smol_file_scan_session_t session;
WIN32_FIND_DATA file_data;
session = (smol_file_scan_session_t)FindFirstFile(TEXT(".\\*"), &file_data);
#ifdef UNICODE
BOOL subst = FALSE;
WideCharToMultiByte(CP_UTF8, 0, file_data.cFileName, lstrlenW(file_data.cFileName), info->file_path, 512, "?", &subst);
#else
memcpy(info->file_path, file_data.cFileName, strlen(file_data.cFileName));
#endif
info->is_folder = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? SMOL_TRUE : SMOL_FALSE;
return session;
}
int smol_file_scan_session_next(smol_file_scan_session_t session, smol_file_info_t* info) {
WIN32_FIND_DATA file_data;
if(FindNextFile(session, &file_data) == FALSE)
return SMOL_FALSE;
#ifdef UNICODE
BOOL subst = FALSE;
WideCharToMultiByte(CP_UTF8, 0, file_data.cFileName, lstrlenW(file_data.cFileName), info->file_path, 512, "?", &subst);
#else
memcpy(info->file_path, file_data.cFileName, strlen(file_data.cFileName));
#endif
info->is_folder = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? SMOL_TRUE : SMOL_FALSE;
return SMOL_TRUE;
}
void* smol_read_entire_file(const char* file_path, smol_size_t* size) {
void* buffer = NULL;
# ifdef UNICODE
wchar_t path[512] = { 0 };
MultiByteToWideChar(CP_UTF8, MB_COMPOSITE, file_path, strlen(file_path), path, 512);
HANDLE file = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
# else
HANDLE file = CreateFile(file_path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
#endif
if(file == INVALID_HANDLE_VALUE) {
fprintf(stderr, "FILE '%s' NOT FOUND!\n", file_path);
return NULL;
}
DWORD high = 0;
DWORD low = GetFileSize(file, &high);
*size = ((smol_size_t)low | ((smol_size_t)high << 32ULL));