-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.h
More file actions
952 lines (770 loc) · 26.2 KB
/
Copy pathutils.h
File metadata and controls
952 lines (770 loc) · 26.2 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
/*
_ _ _ _
| | (_) | | |
_ _| |_ _| |___ | |__
| | | | __| | / __| | '_ \
| |_| | |_| | \__ \_| | | |
\__,_|\__|_|_|___(_)_| |_|
Simple utilities for C
by Filip Krol
https://github.com/MightyPancake/utils
*/
#ifndef UTILS_H
#define UTILS_H
#ifndef UTILS_H_PICK
#define UTILS_H_ALL
#endif
/*
general KENOBI
General quality of life macros :)
*/
#ifndef UTILS_H_KENOBI_H
#define UTILS_H_KENOBI_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_KENOBI) //Avoid defining if module was turned off
//Define types/macros here
#ifndef kenobi_perror
#include <stdio.h>
#define kenobi_perror(MSG) perror(MSG)
#endif
#ifndef kenobi_abort
#include <stdlib.h>
#define kenobi_abort() abort()
#endif
#define kenobi_new_struct(NAME, ...) typedef struct NAME{ \
__VA_ARGS__ \
}NAME
#define kenobi_new_struct_free(NAME, ...) kenobi_new_struct(NAME, __VA_ARGS__); \
void NAME##_free(NAME)
#define kenobi_new_enum(NAME, ...) typedef enum NAME{ \
__VA_ARGS__ \
}NAME
void kenobi_panic(const char* MSG);
//end of types
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_KENOBI_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
void kenobi_panic(const char* MSG) {
kenobi_perror(MSG);
kenobi_abort();
}
//end of variables
#endif //UTILS_H_KENOBI_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_KENOBI
#endif //UTILS_H_KENOBI_H
/*
Dynamic ARRays
An implementation of dynamic array
*/
#ifndef UTILS_H_DARR_H
#define UTILS_H_DARR_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_DARR) //Avoid defining if module was turned off
//Define types/macros here
#ifndef darr_bool_t
#include <stdbool.h>
#define darr_bool_t bool
#endif
#ifndef darr_size_t
#include <stddef.h>
#define darr_size_t size_t
#endif
#ifndef darr_len_t
#define darr_len_t darr_size_t
#endif
typedef struct darr_header {
darr_size_t len;
darr_size_t cap;
} darr_header;
#define darr_default_cap 8
#define darr_prefix_sz (sizeof(darr_header))
//Helpers
//Internal check if growth of array A is needed to fit in an extra element and resize if it is.
#define _darr_grow_if_needed(A) do { \
if (!(A)) darr_init(A); \
darr_header* _h = darr_get_header(A); \
if (_h->len >= _h->cap) { \
darr_size_t _new_cap = _h->cap * 2; \
darr_resize(A, _new_cap); \
} \
} while(0)
//Get header from array pointer
#define darr_get_header(A) ((darr_header*)((char*)(A) - darr_prefix_sz))
//Type used in declarations for dynamic array of type T
#define darr(T) T*
//Resize array A to hold NEW_CAP elements
#define darr_resize(A, NEW_CAP) do { \
darr_header* _h = darr_get_header(A); \
size_t _new_sz = darr_prefix_sz + (sizeof(*(A)) * (NEW_CAP)); \
darr_header* _new_h = (darr_header*)realloc(_h, _new_sz); \
if (_new_h) { \
_new_h->cap = (NEW_CAP); \
(A) = (void*)((char*)_new_h + darr_prefix_sz); \
} \
} while(0)
//Create a new array of type T
#define darr_new(T, ...) ({ \
struct { darr_size_t cap; darr_size_t len; void* src; } _opt = { \
.cap = darr_default_cap, \
.len = 0, \
.src = NULL, \
##__VA_ARGS__ \
}; \
if (_opt.cap < _opt.len) _opt.cap = _opt.len; \
size_t _total_sz = darr_prefix_sz + (sizeof(T) * _opt.cap); \
darr_header* _raw = (darr_header*)malloc(_total_sz); \
_raw->len = _opt.len; \
_raw->cap = _opt.cap; \
T* _data_ptr = (T*)((char*)_raw + darr_prefix_sz); \
if (_opt.src) memcpy(_data_ptr, _opt.src, sizeof(T) * _opt.len); \
_data_ptr; \
})
//Initialize array for variable A
#define darr_init(A) ((A) = darr_new(__typeof__(*(A))))
//Access
//Length of the array
#define darr_len(A) (darr_get_header(A)->len)
//Capacity of the array
#define darr_cap(A) (darr_get_header(A)->cap)
//Value of first element of the array A
#define darr_first(A) ((A)[0])
//Value of the last element of the array A
#define darr_last(A) ((A)[darr_len(A) - 1])
//Push / pop
//Push element E to array A
#define darr_push(A, E) ({ \
_darr_grow_if_needed(A); \
__typeof__(E) _darr_push_val = (E); \
(A)[darr_len(A)++] = _darr_push_val; \
_darr_push_val; \
})
//Pop element from array A
#define darr_pop(A) ({ \
darr_get_header(A)->len--; \
(A)[darr_len(A)]; \
})
//Free
//Free the array
#define darr_free(A) do { \
if(A) free(darr_get_header(A)); \
(A) = NULL; \
} while(0)
#define for_darr(I, E, A) \
for (darr_size_t I=0; I<darr_len(A); I++) \
for (int E##_DARR_CONTROL=0;E##_DARR_CONTROL==0;) \
for (__typeof__((A)[0]) E=(A)[I];E##_DARR_CONTROL==0;E##_DARR_CONTROL=1) \
#define for_darr_elems(V, A) for (__typeof__(A) V = (A); V < (A) + darr_len(A); V++)
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_DARR_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
//end of variables
#endif //UTILS_H_DARR_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_DARR
#endif //UTILS_H_DARR_H
/*
Shamelessly copied from:
https://stackoverflow.com/questions/40159892/using-asprintf-on-windows
reply by Fonic (https://stackoverflow.com/users/1976617/fonic)
based on reply by 7vujy0f0hy (https://stackoverflow.com/users/6314667/7vujy0f0hy)
*/
#ifndef UTILS_H_ASPRINTF_H
#define UTILS_H_ASPRINTF_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_ASPRINTF) //Avoid defining if module was turned off
#if defined(__GNUC__) && ! defined(_GNU_SOURCE)
#define _GNU_SOURCE /* needed for (v)asprintf, affects '#include <stdio.h>' */
#endif
#include <stdio.h> /* needed for vsnprintf */
#include <stdlib.h> /* needed for malloc, free */
#include <stdarg.h> /* needed for va_* */
/*
* vscprintf:
* MSVC implements this as _vscprintf, thus we just 'symlink' it here
* GNU-C-compatible compilers do not implement this, thus we implement it here
*/
#ifdef _MSC_VER
#define vscprintf _vscprintf
#endif
//end of types
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_ASPRINTF_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
#ifdef __GNUC__
int vscprintf(const char *format, va_list ap)
{
va_list ap_copy;
va_copy(ap_copy, ap);
int retval = vsnprintf(NULL, 0, format, ap_copy);
va_end(ap_copy);
return retval;
}
#endif
/*
* asprintf, vasprintf:
* MSVC does not implement these, thus we implement them here
* GNU-C-compatible compilers implement these with the same names, thus we
* don't have to do anything
*/
#ifdef _MSC_VER
int vasprintf(char **strp, const char *format, va_list ap)
{
int len = vscprintf(format, ap);
if (len == -1)
return -1;
char *str = (char*)malloc((size_t) len + 1);
if (!str)
return -1;
int retval = vsnprintf(str, len + 1, format, ap);
if (retval == -1) {
free(str);
return -1;
}
*strp = str;
return retval;
}
int asprintf(char **strp, const char *format, ...)
{
va_list ap;
va_start(ap, format);
int retval = vasprintf(strp, format, ap);
va_end(ap);
return retval;
}
#endif
//end of variables
#endif //UTILS_H_ASPRINTF_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_ASPRINTF
#endif // UTILS_H_ASPRINTF_H
/*
QUAKE
Chunked arena allocator
*/
#ifndef UTILS_H_QUAKE_H
#define UTILS_H_QUAKE_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_QUAKE) //Avoid defining if module was turned off
//Define types/macros here
#ifndef quake_size_t
#include <stddef.h>
#define quake_size_t size_t
#endif
#ifndef quake_byte
#define quake_byte unsigned char
#endif
#include <stdarg.h>
typedef struct quake_chunk {
struct quake_chunk* next;
quake_size_t cap;
quake_size_t used;
quake_byte data[];
} quake_chunk;
typedef struct quake {
quake_chunk* head;
quake_chunk* tail;
quake_size_t chunk_cap;
} quake;
#define quake_default_chunk_cap 1024
#define quake_new(...) ({ \
struct { quake_size_t chunk_cap; } _opt = { \
.chunk_cap = quake_default_chunk_cap, \
##__VA_ARGS__ \
}; \
quake _arena; \
quake_init(&_arena, _opt.chunk_cap); \
_arena; \
})
#define quake_one(A, T) ((T*)quake_alloc((A), sizeof(T)))
#define quake_one_cpy(A, V) ({ \
__typeof__(V)* _quake_one_cpy_tmp = quake_one((A), __typeof__(V)); \
if (_quake_one_cpy_tmp) *_quake_one_cpy_tmp = (V); \
_quake_one_cpy_tmp; \
})
void quake_init(quake* arena, quake_size_t chunk_cap);
void quake_reset(quake* arena);
void quake_free(quake* arena);
quake_size_t quake_allocated_sz(quake* arena);
void* quake_alloc(quake* arena, quake_size_t bytes);
void* quake_calloc(quake* arena, quake_size_t count, quake_size_t bytes);
char* quake_strdup(quake* arena, const char* src);
int quake_vasprintf(quake* arena, char** out, const char* format, va_list ap);
int quake_asprintf(quake* arena, char** out, const char* format, ...);
char* quake_strus_newf(quake* arena, const char* fmt, ...);
#define quake_darr_new(A, T, ...) ({ \
struct { quake_size_t cap; quake_size_t len; void* src; } _opt = { \
.cap = darr_default_cap, \
.len = 0, \
.src = NULL, \
##__VA_ARGS__ \
}; \
T* _result = NULL; \
if (_opt.cap < _opt.len) _opt.cap = _opt.len; \
size_t _total_sz = darr_prefix_sz + (sizeof(T) * _opt.cap); \
darr_header* _raw = (darr_header*)quake_alloc((A), _total_sz); \
if (_raw) { \
_raw->len = _opt.len; \
_raw->cap = _opt.cap; \
T* _data_ptr = (T*)((char*)_raw + darr_prefix_sz); \
if (_opt.src) memcpy(_data_ptr, _opt.src, sizeof(T) * _opt.len); \
_result = _data_ptr; \
} \
_result; \
})
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_QUAKE_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
static quake_size_t _quake_align_up(quake_size_t value, quake_size_t alignment) {
quake_size_t mask = alignment - 1;
return (value + mask) & ~mask;
}
static quake_chunk* _quake_new_chunk(quake_size_t cap) {
quake_chunk* chunk = (quake_chunk*)malloc(sizeof(quake_chunk) + cap);
if (!chunk) return NULL;
chunk->next = NULL;
chunk->cap = cap;
chunk->used = 0;
return chunk;
}
void quake_init(quake* arena, quake_size_t chunk_cap) {
if (!arena) return;
arena->head = NULL;
arena->tail = NULL;
arena->chunk_cap = chunk_cap ? chunk_cap : quake_default_chunk_cap;
}
void quake_reset(quake* arena) {
if (!arena) return;
for (quake_chunk* chunk = arena->head; chunk; chunk = chunk->next)
chunk->used = 0;
arena->tail = arena->head;
}
void quake_free(quake* arena) {
if (!arena) return;
quake_chunk* chunk = arena->head;
while (chunk) {
quake_chunk* next = chunk->next;
free(chunk);
chunk = next;
}
arena->head = NULL;
arena->tail = NULL;
}
quake_size_t quake_allocated_sz(quake* arena) {
if (!arena) return 0;
quake_size_t total = 0;
for (quake_chunk* chunk = arena->head; chunk; chunk = chunk->next)
total += chunk->cap;
return total;
}
void* quake_alloc(quake* arena, quake_size_t bytes) {
if (!arena || bytes == 0) return NULL;
quake_size_t alignment = sizeof(max_align_t);
quake_size_t required = bytes + alignment;
if (!arena->tail) {
quake_size_t first_cap = arena->chunk_cap > required ? arena->chunk_cap : required;
quake_chunk* first = _quake_new_chunk(first_cap);
if (!first) return NULL;
arena->head = first;
arena->tail = first;
}
quake_chunk* chunk = arena->tail;
quake_size_t aligned_used = _quake_align_up(chunk->used, alignment);
if (aligned_used + bytes > chunk->cap) {
if (chunk->next) {
arena->tail = chunk->next;
chunk = arena->tail;
aligned_used = _quake_align_up(chunk->used, alignment);
} else {
quake_size_t new_cap = arena->chunk_cap;
if (new_cap < required) new_cap = required;
quake_chunk* next = _quake_new_chunk(new_cap);
if (!next) return NULL;
chunk->next = next;
arena->tail = next;
chunk = next;
aligned_used = 0;
}
if (aligned_used + bytes > chunk->cap) return NULL;
}
void* ptr = chunk->data + aligned_used;
chunk->used = aligned_used + bytes;
return ptr;
}
void* quake_calloc(quake* arena, quake_size_t count, quake_size_t bytes) {
if (!count || !bytes) return NULL;
if (count > ((quake_size_t)-1) / bytes) return NULL;
quake_size_t total = count * bytes;
void* ptr = quake_alloc(arena, total);
if (!ptr) return NULL;
memset(ptr, 0, total);
return ptr;
}
char* quake_strdup(quake* arena, const char* src) {
if (!src) return NULL;
quake_size_t len = strlen(src) + 1;
char* out = (char*)quake_alloc(arena, len);
if (!out) return NULL;
memcpy(out, src, len);
return out;
}
int quake_vasprintf(quake* arena, char** out, const char* format, va_list ap) {
if (!arena || !out || !format) return -1;
va_list ap_copy;
va_copy(ap_copy, ap);
int len = vsnprintf(NULL, 0, format, ap_copy);
va_end(ap_copy);
if (len < 0) {
*out = NULL;
return -1;
}
char* buffer = (char*)quake_alloc(arena, (quake_size_t)len + 1);
if (!buffer) {
*out = NULL;
return -1;
}
int written = vsnprintf(buffer, (quake_size_t)len + 1, format, ap);
if (written < 0) {
*out = NULL;
return -1;
}
*out = buffer;
return written;
}
int quake_asprintf(quake* arena, char** out, const char* format, ...) {
va_list ap;
va_start(ap, format);
int written = quake_vasprintf(arena, out, format, ap);
va_end(ap);
return written;
}
char* quake_strus_newf(quake* arena, const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
va_list ap_copy;
va_copy(ap_copy, ap);
int len = vsnprintf(NULL, 0, fmt, ap_copy);
va_end(ap_copy);
if (len < 0) {
va_end(ap);
return NULL;
}
char* out = (char*)quake_alloc(arena, (quake_size_t)len + 1);
if (!out) {
va_end(ap);
return NULL;
}
if (vsnprintf(out, (quake_size_t)len + 1, fmt, ap) < 0) {
va_end(ap);
return NULL;
}
va_end(ap);
return out;
}
//end of variables
#endif //UTILS_H_QUAKE_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_QUAKE
#endif //UTILS_H_QUAKE_H
/*
STRing UtilS
String manipulation and creation
*/
#ifndef UTILS_H_STRUS_H
#define UTILS_H_STRUS_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_STRUS) //Avoid defining if module was turned off
//Define types/macros here
#ifndef strus_byte
#define strus_byte char
#endif
#ifndef strus_sz
#include <string.h>
#define strus_sz size_t
#endif
#ifndef strus_len
#include <string.h>
#define strus_len(S) strlen(S)
#endif
#ifndef strus_cmp
#include <string.h>
#define strus_cmp(S1, S2) strcmp(S1, S2)
#endif
#ifndef strus_malloc
#include <stdlib.h>
#define strus_malloc(S) malloc(S)
#endif
#ifndef strus_realloc
#include <stdlib.h>
#define strus_realloc(P, S) realloc(P, S)
#endif
#ifndef strus_memcpy
#include <string.h>
#define strus_memcpy(D, S, SZ) memcpy(D, S, SZ)
#endif
#ifndef strus_asprintf
#define strus_asprintf(D, FMT, ...) ({ \
if(asprintf(D, FMT __VA_OPT__(,) __VA_ARGS__) < 0) kenobi_panic("asprintf failed"); \
})
#endif
#ifndef strus_sprintf
#define strus_sprintf(D, FMT, ...) sprintf(D, FMT __VA_OPT__(,) __VA_ARGS__)
#endif
extern int strus_return;
#define strus_index(S, P) ((strus_sz)(strus_sz)(P-S))
#ifndef strus_find_ptr
#include <string.h>
#define strus_find_ptr(S, P) strstr(S, P)
#endif
#ifndef strus_find
#define strus_find(S, P) ({ \
strus_byte* strus_result = strus_find_ptr(S, P); \
strus_result == NULL ? -1 : strus_index(S, strus_result); \
})
#endif
#define strus_new ({ \
strus_byte* strus_result = (strus_byte*)strus_malloc(sizeof(strus_byte)*(1)); \
strus_result[0] = '\0'; \
strus_result; \
})
#define strus_newf(FMT, ...) ({ \
strus_byte* strus_result; \
strus_asprintf(&strus_result, FMT __VA_OPT__(,) __VA_ARGS__); \
strus_result; \
})
#define strus_copy(S) ({ \
strus_sz strus_slen = strus_len(S); \
strus_byte* strus_result = (strus_byte*)strus_malloc(sizeof(strus_byte)*(strus_slen+1)); \
strus_memcpy(strus_result, S, sizeof(strus_byte)*(strus_slen+1)); \
strus_result; \
})
#define strus_eq(S1, S2) (strus_cmp(S1, S2) == 0)
#define strus_cat(D, S) ({ \
strus_sz strus_dlen = strus_len(D); \
strus_sz strus_slen = strus_len(S); \
D = (strus_byte*)strus_realloc(D, sizeof(strus_byte)*(strus_dlen+strus_slen+1)); \
strus_memcpy(&(D[strus_dlen]), S, sizeof(strus_byte)*(strus_slen+1)); \
D; \
})
#define for_str(I, V, S) \
for(int I=0; I<strlen(S); I++) \
for(int I##_STRUS_FOR_CONTROL=0; I##_STRUS_FOR_CONTROL==0;) \
for(char V=S[I]; I##_STRUS_FOR_CONTROL==0; I##_STRUS_FOR_CONTROL=1)
#define strus_count(D, P) ({ \
strus_sz strus_result = 0; \
strus_byte* strus_lookat = D; \
while((strus_lookat = strus_find_ptr(strus_lookat, P))){ \
strus_lookat+=strus_len(P); \
strus_result++; \
} \
strus_result; \
})
#define strus_replace(D, P, S) ({ \
strus_sz strus_pos = strus_find(D, P); \
strus_sz strus_D_len; \
strus_sz strus_P_len; \
strus_sz strus_S_len; \
strus_byte* strus_tmp; \
if (strus_pos){ \
strus_D_len = strus_len(D); \
strus_P_len = strus_len(P); \
strus_S_len = strus_len(S); \
D[strus_pos] = '\0'; \
if (strus_P_len<strus_S_len){ \
strus_tmp = strus_malloc(sizeof(strus_byte)*(strus_D_len+strus_S_len-strus_P_len+1)); \
strus_sprintf(strus_tmp, "%s%s%s", D, S, &(D[strus_pos+strus_P_len])); \
free(D); \
D = strus_tmp; \
}else{ \
strus_memcpy(&(D[strus_pos]), S, strus_S_len); \
for (strus_sz strus_it=strus_pos+strus_S_len; strus_it<strus_D_len+1; strus_it++){ \
D[strus_it] = D[strus_it+strus_P_len-strus_S_len]; \
} \
} \
} \
D; \
})
#define strus_switch(S, C1) if (strus_eq(S, C1))
#define strus_case(S, C1) else if (strus_eq(S, C1))
#define strus_is_empty(S) (strus_len(S) == 0)
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_STRUS_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
int strus_return = 0;
//end of variables
#endif //UTILS_H_STRUS_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_STRUS
#endif //UTILS_H_STRUS_H
/*
Ansii ESCape
Collection of macros for ANSII escape codes for use in terminal programs
ie. colors, blinking, erasing etc.
*/
#ifndef UTILS_H_AESC_H
#define UTILS_H_AESC_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_AESC) //Avoid defining if module was turned off
//Define types here
#ifndef aesc_size_t
#include <stdlib.h>
#define aesc_size_t size_t
#endif
#ifndef aesc_printf
#include <stdio.h>
#define aesc_printf(FMT, ...) printf(FMT, ##__VAR_ARGS__)
#endif
#define aesc_do(C) aesc_printf(C)
#define aesc_seq(S) "\x1B"S
#define aesc_style(S) aesc_seq("["S"m")
// Colors
#define aesc_reset aesc_style("0")
#define aesc_black aesc_style("30")
#define aesc_red aesc_style("31")
#define aesc_green aesc_style("32")
#define aesc_yellow aesc_style("33")
#define aesc_blue aesc_style("34")
#define aesc_magenta aesc_style("35")
#define aesc_cyan aesc_style("36")
#define aesc_white aesc_style("37")
#define aesc_default aesc_style("39")
#define aesc_fg_color(V) aesc_seq("[38;5;"#V"m")
#define aesc_black_bg aesc_style("40")
#define aesc_red_bg aesc_style("41")
#define aesc_green_bg aesc_style("42")
#define aesc_yellow_bg aesc_style("43")
#define aesc_blue_bg aesc_style("44")
#define aesc_magenta_bg aesc_style("45")
#define aesc_cyan_bg aesc_style("46")
#define aesc_white_bg aesc_style("47")
#define aesc_default_bg aesc_style("49")
//Bold
#define aesc_bold_on aesc_style("1")
#define aesc_bold_off aesc_style("22")
#define aesc_bold(S) aesc_bold_on S aesc_bold_off
//Italic
#define aesc_italic_on aesc_style("3")
#define aesc_italic_off aesc_style("23")
#define aesc_italic(S) aesc_italic_on S aesc_italic_off
//Underline
#define aesc_begin_underline aesc_style("4")
#define aesc_end_underline aesc_style("24")
#define aesc_underline(S) aesc_begin_underline S aesc_end_underline
//Reverse
#define aesc_begin_reverse aesc_style("7")
#define aesc_end_reverse aesc_style("27")
#define aesc_reverse(S) aesc_begin_reverse S aesc_end_reverse
//Cursor
#define aesc_home aesc_seq("[H")
#define aesc_mv(X, Y) aesc_seq("[" #X ";" #Y "f")
#define aesc_up(V) aesc_seq("[" #V "A")
#define aesc_down(V) aesc_seq("[" #V "B")
#define aesc_right(V) aesc_seq("[" #V "C")
#define aesc_left(V) aesc_seq("[" #V "D")
//Erase
#define aesc_clear aesc_seq("[J")
#define aesc_erase aesc_seq("[K")
//Map
#define aesc_map(I, S) aesc_seq("[" I ";" S ";p")
// extern const char* aesc_colors[];
// extern char* aesc_hue[] = {A_FC(196), A_FC(202), A_FC(208), A_FC(214), A_FC(220), A_FC(226), A_FC(190), A_FC(154), A_FC(118), A_FC(82), A_FC(46), A_FC(47), A_FC(48), A_FC(49), A_FC(50), A_FC(51), A_FC(45), A_FC(39), A_FC(33), A_FC(27), A_FC(21), A_FC(57), A_FC(93), A_FC(129), A_FC(165), A_FC(201), A_FC(200), A_FC(199), A_FC(198), A_FC(197)};
// extern const aesc_size_t aesc_hue_len;
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_AESC_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
#define A_FC(V) aesc_fg_color(V)
const char* aesc_colors[] = {
A_FC(0), A_FC(1), A_FC(2), A_FC(3), A_FC(4), A_FC(5), A_FC(6), A_FC(7), A_FC(8), A_FC(9),
A_FC(10), A_FC(11), A_FC(12), A_FC(13), A_FC(14), A_FC(15), A_FC(16), A_FC(17), A_FC(18), A_FC(19),
A_FC(20), A_FC(21), A_FC(22), A_FC(23), A_FC(24), A_FC(25), A_FC(26), A_FC(27), A_FC(28), A_FC(29),
A_FC(30), A_FC(31), A_FC(32), A_FC(33), A_FC(34), A_FC(35), A_FC(36), A_FC(37), A_FC(38), A_FC(39),
A_FC(40), A_FC(41), A_FC(42), A_FC(43), A_FC(44), A_FC(45), A_FC(46), A_FC(47), A_FC(48), A_FC(49),
A_FC(50), A_FC(51), A_FC(52), A_FC(53), A_FC(54), A_FC(55), A_FC(56), A_FC(57), A_FC(58), A_FC(59),
A_FC(60), A_FC(61), A_FC(62), A_FC(63), A_FC(64), A_FC(65), A_FC(66), A_FC(67), A_FC(68), A_FC(69),
A_FC(70), A_FC(71), A_FC(72), A_FC(73), A_FC(74), A_FC(75), A_FC(76), A_FC(77), A_FC(78), A_FC(79),
A_FC(80), A_FC(81), A_FC(82), A_FC(83), A_FC(84), A_FC(85), A_FC(86), A_FC(87), A_FC(88), A_FC(89),
A_FC(90), A_FC(91), A_FC(92), A_FC(93), A_FC(94), A_FC(95), A_FC(96), A_FC(97), A_FC(98), A_FC(99),
A_FC(100), A_FC(101), A_FC(102), A_FC(103), A_FC(104), A_FC(105), A_FC(106), A_FC(107), A_FC(108), A_FC(109),
A_FC(110), A_FC(111), A_FC(112), A_FC(113), A_FC(114), A_FC(115), A_FC(116), A_FC(117), A_FC(118), A_FC(119),
A_FC(120), A_FC(121), A_FC(122), A_FC(123), A_FC(124), A_FC(125), A_FC(126), A_FC(127), A_FC(128), A_FC(129),
A_FC(130), A_FC(131), A_FC(132), A_FC(133), A_FC(134), A_FC(135), A_FC(136), A_FC(137), A_FC(138), A_FC(139),
A_FC(140), A_FC(141), A_FC(142), A_FC(143), A_FC(144), A_FC(145), A_FC(146), A_FC(147), A_FC(148), A_FC(149),
A_FC(150), A_FC(151), A_FC(152), A_FC(153), A_FC(154), A_FC(155), A_FC(156), A_FC(157), A_FC(158), A_FC(159),
A_FC(160), A_FC(161), A_FC(162), A_FC(163), A_FC(164), A_FC(165), A_FC(166), A_FC(167), A_FC(168), A_FC(169),
A_FC(170), A_FC(171), A_FC(172), A_FC(173), A_FC(174), A_FC(175), A_FC(176), A_FC(177), A_FC(178), A_FC(179),
A_FC(180), A_FC(181), A_FC(182), A_FC(183), A_FC(184), A_FC(185), A_FC(186), A_FC(187), A_FC(188), A_FC(189),
A_FC(190), A_FC(191), A_FC(192), A_FC(193), A_FC(194), A_FC(195), A_FC(196), A_FC(197), A_FC(198), A_FC(199),
A_FC(200), A_FC(201), A_FC(202), A_FC(203), A_FC(204), A_FC(205), A_FC(206), A_FC(207), A_FC(208), A_FC(209),
A_FC(210), A_FC(211), A_FC(212), A_FC(213), A_FC(214), A_FC(215), A_FC(216), A_FC(217), A_FC(218), A_FC(219),
A_FC(220), A_FC(221), A_FC(222), A_FC(223), A_FC(224), A_FC(225), A_FC(226), A_FC(227), A_FC(228), A_FC(229),
A_FC(230), A_FC(231), A_FC(232), A_FC(233), A_FC(234), A_FC(235), A_FC(236), A_FC(237), A_FC(238), A_FC(239),
A_FC(240), A_FC(241), A_FC(242), A_FC(243), A_FC(244), A_FC(245), A_FC(246), A_FC(247), A_FC(248), A_FC(249),
A_FC(250), A_FC(251), A_FC(252), A_FC(253), A_FC(254), A_FC(255), A_FC(256), A_FC(257), A_FC(258), A_FC(259)
};
char* aesc_hue[] = {A_FC(196), A_FC(202), A_FC(208), A_FC(214), A_FC(220), A_FC(226), A_FC(190), A_FC(154), A_FC(118), A_FC(82), A_FC(46), A_FC(47), A_FC(48), A_FC(49), A_FC(50), A_FC(51), A_FC(45), A_FC(39), A_FC(33), A_FC(27), A_FC(21), A_FC(57), A_FC(93), A_FC(129), A_FC(165), A_FC(201), A_FC(200), A_FC(199), A_FC(198), A_FC(197)};
const aesc_size_t aesc_hue_len = (sizeof(aesc_hue)/sizeof(aesc_hue[0]));
#undef A_FC
//end of variables
#endif //UTILS_H_AESC_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_AESC
#endif //UTILS_H_AESC_H
/*
MEMory
Set of memory related utilities
*/
#ifndef UTILS_H_MEM_H
#define UTILS_H_MEM_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_MEM) //Avoid defining if module was turned off
//Define types/macros here
#ifndef mem_typeof
#define mem_typeof(V) __typeof__(V)
#endif
#define mem_base_type(PT) mem_typeof(* ( (PT) 0 ))
#define mem_ptr_type(T) mem_typeof(T)*
#ifndef mem_malloc
#include <stdlib.h>
#define mem_malloc(S) malloc(S)
#endif
#define mem_one(T) ((mem_ptr_type(T))mem_malloc(sizeof(T)))
#define mem_one_cpy(V) ({ \
mem_ptr_type(mem_typeof(V)) MEM_TMP_VAL = mem_one(mem_typeof(V)); \
*MEM_TMP_VAL = V; \
MEM_TMP_VAL; \
})
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_MEM_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
//end of variables
#endif //UTILS_H_MEM_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_MEM
#endif //UTILS_H_MEM_H
/*
FIL.h
Description for FIL.h
*/
#ifndef UTILS_H_FIL_H
#define UTILS_H_FIL_H
#if defined(UTILS_H_ALL) || defined(UTILS_H_FIL) //Avoid defining if module was turned off
//Define types/macros here
#include <stdio.h>
#include <string.h>
size_t read_file_to_string(const char *filename, char **out_string);
//end of types/macros
#if defined(UTILS_H_IMPLEMENTATION) || defined(UTILS_H_FIL_IMPLEMENTATION) //Implementation part only gets compiled once
//Declare variables here
size_t read_file_to_string(const char *filename, char **out_string) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Error opening file");
return 0;
}
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
*out_string = (char *)malloc(size + 1);
if (!*out_string) {
perror("Memory allocation failed");
fclose(file);
return 0;
}
size_t read_size = fread(*out_string, 1, size, file);
(*out_string)[read_size] = '\0';
fclose(file);
return read_size;
}
//end of variables
#endif //UTILS_H_FIL_IMPLEMENTATION
#endif //UTILS_H_ALL || UTILS_H_FIL
#endif //UTILS_H_FIL_H
#endif //UTILS.H