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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
|
#ifndef KIT_INL_H
#define KIT_INL_H
/************************************************************************
* *
* *
* FILE: ./source/kit/status.h *
* *
* *
************************************************************************/
#ifndef KIT_STATUS_H
#define KIT_STATUS_H
#ifdef __cplusplus
extern "C" {
#endif
enum {
KIT_OK,
KIT_ERROR_BAD_ALLOC,
KIT_ERROR_MKDIR_FAILED,
KIT_ERROR_RMDIR_FAILED,
KIT_ERROR_UNLINK_FAILED,
KIT_ERROR_FILE_ALREADY_EXISTS,
KIT_ERROR_FILE_DO_NOT_EXIST,
KIT_ERROR_PATH_TOO_LONG
};
typedef int kit_status_t;
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/allocator.h *
* *
* *
************************************************************************/
#ifndef KIT_ALLOCATOR_H
#define KIT_ALLOCATOR_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
enum { KIT_ALLOCATE, KIT_DEALLOCATE, KIT_REALLOCATE };
typedef void *(*kit_allocate_fn)(int request, void *state,
ptrdiff_t size,
ptrdiff_t previous_size,
void *pointer);
typedef struct {
void *state;
kit_allocate_fn allocate;
} kit_allocator_t;
/* Application should implement this function if custom allocator
* dispatch is enabled.
*
* See KIT_ENABLE_CUSTOM_ALLOC_DISPATCH
*/
void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
ptrdiff_t size, ptrdiff_t previous_size,
void *pointer);
kit_allocator_t kit_alloc_default(void);
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/array_ref.h *
* *
* *
************************************************************************/
#ifndef KIT_ARRAY_REF_H
#define KIT_ARRAY_REF_H
#include <stddef.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef int (*kit_ar_compare_fn)(void const *left, void const *right);
int kit_ar_equal_bytes(ptrdiff_t left_element_size,
ptrdiff_t left_size, void const *left_data,
ptrdiff_t right_element_size,
ptrdiff_t right_size, void const *right_data);
int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
void const *left_data,
ptrdiff_t right_element_size, ptrdiff_t right_size,
void const *right_data, kit_ar_compare_fn compare);
#define KIT_AR_MUT(type_) \
struct { \
ptrdiff_t size; \
type_ *values; \
}
#define KIT_AR(type_) \
struct { \
ptrdiff_t size; \
type_ const *values; \
}
#define KIT_AR_MUT_WRAP(name_, element_type_, array_) \
struct { \
ptrdiff_t size; \
element_type_ *values; \
} name_ = { .size = (sizeof(array_) / sizeof((array_)[0])), \
.values = (array_) }
#define KIT_AR_WRAP(name_, element_type_, array_) \
struct { \
ptrdiff_t size; \
element_type_ const *values; \
} name_ = { .size = (sizeof(array_) / sizeof((array_)[0])), \
.values = (array_) }
#define KIT_AR_EQUAL(left_, right_) \
kit_ar_equal_bytes(sizeof((left_).values[0]), (left_).size, \
(left_).values, sizeof((right_).values[0]), \
(right_).size, (right_).values)
#define KIT_AR_COMPARE(left_, right_, compare_) \
kit_ar_compare(sizeof((left_).values[0]), (left_).size, \
(left_).values, sizeof((right_).values[0]), \
(right_).size, (right_).values, \
(kit_ar_compare_fn) (compare_))
#ifndef KIT_DISABLE_SHORT_NAMES
# define ar_compare_fn kit_ar_compare_fn
# define ar_equal_bytes kit_ar_equal_bytes
# define ar_compare kit_ar_compare
# define AR_MUT KIT_AR_MUT
# define AR KIT_AR
# define AR_MUT_WRAP KIT_AR_MUT_WRAP
# define AR_WRAP KIT_AR_WRAP
# define AR_EQUAL KIT_AR_EQUAL
# define AR_COMPARE KIT_AR_COMPARE
#endif
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/dynamic_array.h *
* *
* *
************************************************************************/
#ifndef KIT_DYNAMIC_ARRAY_H
#define KIT_DYNAMIC_ARRAY_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
ptrdiff_t capacity;
ptrdiff_t size;
void *values;
kit_allocator_t alloc;
} kit_da_void_t;
void kit_da_init(kit_da_void_t *array, ptrdiff_t element_size,
ptrdiff_t size, kit_allocator_t alloc);
void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
ptrdiff_t size);
/* Dynamic array type definition.
*/
#define KIT_DA(element_type_) \
struct { \
ptrdiff_t capacity; \
ptrdiff_t size; \
element_type_ *values; \
kit_allocator_t alloc; \
}
/* Initialize dynamic array.
*/
#define KIT_DA_INIT(array_, size_, alloc_) \
kit_da_init((kit_da_void_t *) &(array_), \
sizeof((array_).values[0]), (size_), (alloc_))
/* Declare and initialize dynamic array.
*/
#define KIT_DA_CREATE(name_, element_type_, size_) \
KIT_DA(element_type_) name_; \
KIT_DA_INIT(name_, (size_), kit_alloc_default())
/* Destroy dynamic array.
*/
#define KIT_DA_DESTROY(array_) \
do { \
if ((array_).values != NULL) \
kit_alloc_dispatch((array_).alloc, KIT_DEALLOCATE, 0, 0, \
(array_).values); \
} while (0)
/* Resize dynamic array.
*/
#define KIT_DA_RESIZE(array_, size_) \
kit_da_resize((kit_da_void_t *) &(array_), \
sizeof((array_).values[0]), size_)
/* Append a value to dynamic array.
*/
#define KIT_DA_APPEND(array_, value_) \
do { \
ptrdiff_t const kit_index_back_ = (array_).size; \
KIT_DA_RESIZE((array_), kit_index_back_ + 1); \
if (kit_index_back_ < (array_).size) \
(array_).values[kit_index_back_] = (value_); \
} while (0)
/* Insert a value into dynamic array.
*/
#define KIT_DA_INSERT(array_, index_, value_) \
do { \
ptrdiff_t kit_i_; \
ptrdiff_t const kit_index_back_ = (array_).size; \
ptrdiff_t const kit_indert_n_ = (index_); \
KIT_DA_RESIZE((array_), kit_index_back_ + 1); \
if (kit_index_back_ + 1 == (array_).size) { \
for (kit_i_ = kit_index_back_; kit_i_ > kit_indert_n_; \
kit_i_--) \
(array_).values[kit_i_] = (array_).values[kit_i_ - 1]; \
(array_).values[kit_indert_n_] = (value_); \
} \
} while (0)
/* Erase a value from dynamic array.
*/
#define KIT_DA_ERASE(array_, index_) \
do { \
ptrdiff_t i_; \
for (i_ = (index_) + 1; i_ < (array_).size; i_++) \
(array_).values[i_ - 1] = (array_).values[i_]; \
KIT_DA_RESIZE((array_), (array_).size - 1); \
} while (0)
typedef KIT_DA(char) kit_string_t;
#ifndef KIT_DISABLE_SHORT_NAMES
# define da_void_t kit_da_void_t
# define da_init kit_da_init
# define da_resize kit_da_resize
# define string_t kit_string_t
# define DA KIT_DA
# define DA_INIT KIT_DA_INIT
# define DA_CREATE KIT_DA_CREATE
# define DA_DESTROY KIT_DA_DESTROY
# define DA_RESIZE KIT_DA_RESIZE
# define DA_APPEND KIT_DA_APPEND
# define DA_INSERT KIT_DA_INSERT
# define DA_ERASE KIT_DA_ERASE
#endif
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/string_ref.h *
* *
* *
************************************************************************/
#ifndef KIT_STRING_REF_H
#define KIT_STRING_REF_H
#ifdef __cplusplus
extern "C" {
#endif
typedef KIT_AR_MUT(char) kit_string_mut_t;
typedef KIT_AR(char) kit_string_ref_t;
typedef kit_string_mut_t kit_out_str_t;
typedef kit_string_ref_t kit_str_t;
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunknown-pragmas"
# pragma GCC push_options
# pragma GCC optimize("O3")
#endif
static kit_str_t kit_str(ptrdiff_t const size,
char const *const static_string) {
kit_str_t const s = { .size = size, .values = static_string };
return s;
}
#ifdef __GNUC__
# pragma GCC pop_options
# pragma GCC diagnostic pop
#endif
#define KIT_SZ(static_str_) \
kit_str(sizeof(static_str_) - 1, (static_str_))
#define KIT_WRAP_STR(string_) \
kit_str((string_).size, (string_).values)
#ifndef KIT_DISABLE_SHORT_NAMES
# define string_mut_t kit_string_mut_t
# define string_ref_t kit_string_ref_t
# define out_str_t kit_out_str_t
# define str_t kit_str_t
# define SZ KIT_SZ
# define WRAP_STR KIT_WRAP_STR
#endif
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/file.h *
* *
* *
************************************************************************/
#ifndef KIT_FILE_H
#define KIT_FILE_H
#ifdef __cplusplus
extern "C" {
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
# define KIT_PATH_DELIM '\\'
# define KIT_ENV_HOME "USERPROFILE"
#else
# define KIT_PATH_DELIM '/'
# define KIT_ENV_HOME "HOME"
#endif
typedef enum {
KIT_PATH_NONE,
KIT_PATH_FILE,
KIT_PATH_FOLDER
} kit_path_type_t;
enum { KIT_FILE_SIZE_ERROR = -1 };
kit_string_t kit_path_norm(kit_str_t path, kit_allocator_t alloc);
kit_string_t kit_path_join(kit_str_t left, kit_str_t right,
kit_allocator_t alloc);
kit_string_t kit_path_user(kit_allocator_t alloc);
kit_str_t kit_path_index(kit_str_t path, ptrdiff_t index);
kit_str_t kit_path_take(kit_str_t path, ptrdiff_t count);
kit_status_t kit_file_create_folder(kit_str_t path);
kit_status_t kit_file_create_folder_recursive(kit_str_t path);
kit_status_t kit_file_remove(kit_str_t path);
kit_status_t kit_file_remove_folder(kit_str_t path);
kit_status_t kit_file_remove_recursive(kit_str_t path,
kit_allocator_t alloc);
kit_path_type_t kit_path_type(kit_str_t path);
typedef struct {
kit_status_t status;
int64_t time_modified_sec;
int32_t time_modified_nsec;
int64_t size;
} kit_file_info_t;
kit_file_info_t kit_file_info(kit_str_t path);
typedef struct {
kit_status_t status;
KIT_DA(kit_string_t) files;
} kit_path_list_t;
kit_path_list_t kit_file_enum_folder(kit_str_t path,
kit_allocator_t alloc);
void kit_path_list_destroy(kit_path_list_t list);
#ifndef KIT_DISABLE_SHORT_NAMES
# define path_norm kit_path_norm
# define path_join kit_path_join
# define path_user kit_path_user
# define path_index kit_path_index
# define path_take kit_path_take
# define file_create_folder kit_file_create_folder
# define file_create_folder_recursive \
kit_file_create_folder_recursive
# define file_remove kit_file_remove
# define file_remove_folder kit_file_remove_folder
# define file_remove_recursive kit_file_remove_recursive
# define path_type kit_path_type
# define file_size_result_t kit_file_size_result_t
# define file_size kit_file_size
# define path_type_t kit_path_type_t
# define path_list_t kit_path_list_t
# define file_enum_folder kit_file_enum_folder
# define path_list_destroy kit_path_list_destroy
# define PATH_NONE KIT_PATH_NONE
# define PATH_FILE KIT_PATH_FILE
# define PATH_FOLDER KIT_PATH_FOLDER
#endif
#ifdef __cplusplus
}
#endif
#endif
/************************************************************************
* *
* *
* FILE: ./source/kit/allocator.c *
* *
* *
************************************************************************/
#include <assert.h>
#ifndef KIT_DISABLE_SYSTEM_MALLOC
# include <stdlib.h>
#endif
static void *allocate(int request, void *state, ptrdiff_t size,
ptrdiff_t previous_size, void *pointer) {
#ifndef KIT_DISABLE_SYSTEM_MALLOC
switch (request) {
case KIT_ALLOCATE:
assert(previous_size == 0);
assert(pointer == NULL);
return malloc(size);
case KIT_DEALLOCATE:
assert(size == 0);
assert(pointer != NULL);
free(pointer);
return NULL;
case KIT_REALLOCATE:
/* FIXME
* Not implemented.
*/
assert(0);
return NULL;
default: assert(0);
}
#else
assert(0);
#endif
return NULL;
}
#ifndef KIT_ENABLE_CUSTOM_ALLOC_DISPATCH
void *kit_alloc_dispatch(kit_allocator_t alloc, int request,
ptrdiff_t size, ptrdiff_t previous_size,
void *pointer) {
assert(alloc.allocate != NULL);
if (alloc.allocate == NULL)
return NULL;
return alloc.allocate(request, alloc.state, size, previous_size,
pointer);
}
#endif
kit_allocator_t kit_alloc_default(void) {
kit_allocator_t alloc = { .state = NULL, .allocate = allocate };
return alloc;
}
/************************************************************************
* *
* *
* FILE: ./source/kit/array_ref.c *
* *
* *
************************************************************************/
#include <string.h>
int kit_ar_equal_bytes(ptrdiff_t left_element_size,
ptrdiff_t left_size, void const *left_data,
ptrdiff_t right_element_size,
ptrdiff_t right_size, void const *right_data) {
ptrdiff_t i;
if (left_element_size != right_element_size)
return 0;
if (left_size != right_size)
return 0;
for (i = 0; i < left_size; i++)
if (memcmp((char const *) left_data + i * left_element_size,
(char const *) right_data + i * left_element_size,
left_element_size) != 0)
return 0;
return 1;
}
int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size,
void const *left_data,
ptrdiff_t right_element_size, ptrdiff_t right_size,
void const *right_data,
kit_ar_compare_fn compare) {
ptrdiff_t i;
if (left_element_size < right_element_size)
return -1;
if (left_element_size > right_element_size)
return 1;
for (i = 0; i < left_size && i < right_size; i++) {
int const c = compare(
(char const *) left_data + i * left_element_size,
(char const *) right_data + i * left_element_size);
if (c != 0)
return c;
}
if (left_size < right_size)
return -1;
if (left_size > right_size)
return 1;
return 0;
}
/************************************************************************
* *
* *
* FILE: ./source/kit/dynamic_array.c *
* *
* *
************************************************************************/
#include <assert.h>
#include <string.h>
void kit_da_init(kit_da_void_t *array, ptrdiff_t element_size,
ptrdiff_t size, kit_allocator_t alloc) {
assert(array != NULL);
assert(element_size > 0);
assert(size >= 0);
assert(alloc.allocate != NULL);
memset(array, 0, sizeof(kit_da_void_t));
if (size > 0)
array->values = kit_alloc_dispatch(alloc, KIT_ALLOCATE,
element_size * size, 0, NULL);
if (array->values != NULL) {
array->capacity = size;
array->size = size;
}
array->alloc = alloc;
}
static ptrdiff_t eval_capacity(ptrdiff_t current_cap,
ptrdiff_t required_cap) {
if (current_cap == 0)
return required_cap;
ptrdiff_t cap = current_cap;
while (cap < required_cap) cap *= 2;
return cap;
}
void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size,
ptrdiff_t size) {
assert(array != NULL);
assert(element_size > 0);
assert(size >= 0);
if (size <= array->capacity) {
array->size = size;
} else {
ptrdiff_t capacity = eval_capacity(array->capacity, size);
assert(array->alloc.allocate != NULL);
void *bytes = kit_alloc_dispatch(
array->alloc, KIT_ALLOCATE, element_size * capacity, 0, NULL);
if (bytes != NULL) {
if (array->size > 0)
memcpy(bytes, array->values, element_size * array->size);
if (array->values != NULL)
kit_alloc_dispatch(array->alloc, KIT_DEALLOCATE, 0, 0,
array->values);
array->capacity = capacity;
array->size = size;
array->values = bytes;
}
}
}
/************************************************************************
* *
* *
* FILE: ./source/kit/file.c *
* *
* *
************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
enum { PATH_BUF_SIZE = 4096 };
#if defined(_WIN32) && !defined(__CYGWIN__)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN 1
# endif
# include <stdint.h>
# include <Windows.h>
#else
# include <dirent.h>
# include <sys/stat.h>
# include <unistd.h>
#endif
#ifdef __APPLE__
# define st_mtim st_mtimespec
#endif
static int is_delim(char const c) {
return c == '/' || c == '\\';
}
kit_string_t kit_path_norm(kit_str_t const path,
kit_allocator_t const alloc) {
str_t const parent = SZ("..");
ptrdiff_t i, i1, j;
string_t norm;
DA_INIT(norm, path.size, alloc);
assert(norm.size == path.size);
if (norm.size != path.size)
return norm;
memcpy(norm.values, path.values, path.size);
for (i1 = 0, i = 0; i < path.size; i++) {
if (!is_delim(path.values[i]))
continue;
str_t const s = { .size = i - i1 - 1,
.values = path.values + i1 + 1 };
if (AR_EQUAL(s, parent)) {
int have_parent = 0;
ptrdiff_t i0 = 0;
for (j = 0; j < i1; j++) {
if (norm.values[j] != '\0')
have_parent = 1;
if (is_delim(norm.values[j]))
i0 = j;
}
if (have_parent) {
memset(norm.values + i0, '\0', i - i0);
if (!is_delim(path.values[i0]))
norm.values[i] = '\0';
}
}
i1 = i;
}
ptrdiff_t size = 0;
for (i = 0; i < norm.size; i++) {
if (norm.values[i] != '\0') {
if (is_delim(norm.values[i]))
norm.values[size] = KIT_PATH_DELIM;
else
norm.values[size] = norm.values[i];
size++;
}
}
norm.size = size;
return norm;
}
kit_string_t kit_path_join(kit_str_t const left,
kit_str_t const right,
kit_allocator_t const alloc) {
ptrdiff_t left_size = left.size;
ptrdiff_t right_size = right.size;
char const *right_values = right.values;
if (left_size > 0 && is_delim(left.values[left_size - 1]))
left_size--;
if (right_size > 0 && is_delim(right.values[0])) {
right_size--;
right_values++;
}
kit_string_t joined;
DA_INIT(joined, left_size + right_size + 1, alloc);
assert(joined.size == left_size + right_size + 1);
if (joined.size != left_size + right_size + 1)
return joined;
memcpy(joined.values, left.values, left_size);
joined.values[left_size] = KIT_PATH_DELIM;
memcpy(joined.values + left_size + 1, right_values, right_size);
return joined;
}
kit_string_t kit_path_user(kit_allocator_t const alloc) {
char *home = getenv(KIT_ENV_HOME);
ptrdiff_t const size = home != NULL ? (ptrdiff_t) strlen(home) : 0;
string_t user;
DA_INIT(user, size, alloc);
assert(user.size == size);
if (user.size == size)
memcpy(user.values, home, user.size);
else {
DA_RESIZE(user, 1);
assert(user.size == 1);
if (user.size == 1)
user.values[0] = '.';
}
return user;
}
kit_str_t kit_path_index(kit_str_t const path,
ptrdiff_t const index) {
str_t s = { .size = 0, .values = NULL };
ptrdiff_t i0 = 0;
ptrdiff_t i = 0;
ptrdiff_t n = 0;
for (; i < path.size; i++) {
if (!is_delim(path.values[i]))
continue;
if (i0 < i) {
if (n++ == index) {
s.values = path.values + i0;
s.size = i - i0;
return s;
}
}
i0 = i + 1;
}
if (n == index) {
s.values = path.values + i0;
s.size = i - i0;
}
return s;
}
kit_str_t kit_path_take(kit_str_t const path, ptrdiff_t const count) {
str_t s = { .size = 0, .values = path.values };
ptrdiff_t i0 = 0;
ptrdiff_t i = 0;
ptrdiff_t n = 0;
for (; i < path.size; i++) {
if (!is_delim(path.values[i]))
continue;
if (i0 < i) {
if (n++ == count) {
s.size = i;
return s;
}
}
i0 = i + 1;
}
if (n == count)
s.size = i;
return s;
}
#if defined(_WIN32) && !defined(__CYGWIN__)
static void win32_prepare_path_(WCHAR *const buf,
kit_str_t const path) {
assert(path.size == 0 || path.values != NULL);
assert(path.size + 5 < PATH_BUF_SIZE);
memset(buf, 0, PATH_BUF_SIZE);
buf[0] = L'\\';
buf[1] = L'\\';
buf[2] = L'?';
buf[3] = L'\\';
if (path.size > 0 && path.size + 5 < PATH_BUF_SIZE)
for (ptrdiff_t i = 0; i < path.size; i++) {
if (path.values[i] == '/')
buf[4 + i] = L'\\';
else
buf[4 + i] = path.values[i];
}
}
# define PREPARE_PATH_BUF_ \
WCHAR buf[PATH_BUF_SIZE]; \
win32_prepare_path_(buf, path)
#else
static void unix_prepare_path_(char *const buf,
kit_str_t const path) {
assert(path.size == 0 || path.values != NULL);
assert(path.size + 1 < PATH_BUF_SIZE);
memset(buf, 0, PATH_BUF_SIZE);
if (path.size > 0 && path.size + 1 < PATH_BUF_SIZE)
memcpy(buf, path.values, path.size);
}
# define PREPARE_PATH_BUF_ \
char buf[PATH_BUF_SIZE]; \
unix_prepare_path_(buf, path)
#endif
kit_status_t kit_file_create_folder(kit_str_t const path) {
PREPARE_PATH_BUF_;
#if defined(_WIN32) && !defined(__CYGWIN__)
return CreateDirectoryW(buf, NULL) ? KIT_OK
: KIT_ERROR_MKDIR_FAILED;
#else
return mkdir(buf, 0755) == 0 ? KIT_OK : KIT_ERROR_MKDIR_FAILED;
#endif
}
kit_status_t kit_file_create_folder_recursive(kit_str_t const path) {
ptrdiff_t i;
for (i = 0;; i++) {
str_t const part = kit_path_take(path, i);
int const type = kit_path_type(part);
if (type == KIT_PATH_FILE)
return KIT_ERROR_FILE_ALREADY_EXISTS;
if (type == KIT_PATH_NONE) {
kit_status_t const s = kit_file_create_folder(part);
if (s != KIT_OK)
return s;
}
if (part.size == path.size)
break;
}
return KIT_OK;
}
kit_status_t kit_file_remove(kit_str_t const path) {
PREPARE_PATH_BUF_;
#if defined(_WIN32) && !defined(__CYGWIN__)
return DeleteFileW(buf) ? KIT_OK : KIT_ERROR_UNLINK_FAILED;
#else
return unlink(buf) == 0 ? KIT_OK : KIT_ERROR_UNLINK_FAILED;
#endif
}
kit_status_t kit_file_remove_folder(kit_str_t const path) {
PREPARE_PATH_BUF_;
#if defined(_WIN32) && !defined(__CYGWIN__)
return RemoveDirectoryW(buf) ? KIT_OK : KIT_ERROR_RMDIR_FAILED;
#else
return rmdir(buf) == 0 ? KIT_OK : KIT_ERROR_RMDIR_FAILED;
#endif
}
kit_status_t kit_file_remove_recursive(kit_str_t const path,
kit_allocator_t const alloc) {
int type = kit_path_type(path);
ptrdiff_t i;
switch (type) {
case KIT_PATH_FILE: return kit_file_remove(path);
case KIT_PATH_FOLDER: {
kit_path_list_t list = kit_file_enum_folder(path, alloc);
if (list.status != KIT_OK) {
kit_path_list_destroy(list);
return list.status;
}
for (i = 0; i < list.files.size; i++) {
str_t const s = { .size = list.files.values[i].size,
.values = list.files.values[i].values };
kit_file_remove_recursive(s, alloc);
}
kit_path_list_destroy(list);
return kit_file_remove_folder(path);
}
default:;
}
return KIT_ERROR_FILE_DO_NOT_EXIST;
}
kit_path_type_t kit_path_type(kit_str_t const path) {
PREPARE_PATH_BUF_;
#if defined(_WIN32) && !defined(__CYGWIN__)
if (PathFileExistsW(buf)) {
if ((GetFileAttributesW(buf) & FILE_ATTRIBUTE_DIRECTORY) != 0)
return KIT_PATH_FOLDER;
else
return KIT_PATH_FILE;
}
#else
struct stat info;
if (stat(buf, &info) == 0) {
if (S_ISREG(info.st_mode))
return KIT_PATH_FILE;
if (S_ISDIR(info.st_mode))
return KIT_PATH_FOLDER;
}
#endif
return KIT_PATH_NONE;
}
kit_file_info_t kit_file_info(kit_str_t const path) {
kit_file_info_t result;
memset(&result, 0, sizeof result);
PREPARE_PATH_BUF_;
#if defined(_WIN32) && !defined(__CYGWIN__)
HANDLE f = CreateFileW(buf, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (f != INVALID_HANDLE_VALUE) {
FILETIME ft;
if (GetFileTime(f, NULL, NULL, &ft) != 0) {
uint64_t const nsec100 = (((uint64_t) ft.dwHighDateTime)
<< 32) |
(uint64_t) ft.dwLowDateTime;
result.time_modified_sec = (int64_t) (nsec100 / 10000000);
result.time_modified_nsec = (int32_t) (100 *
(nsec100 % 10000000));
} else {
assert(0);
}
DWORD high;
DWORD low = GetFileSize(f, &high);
result.size = (int64_t) ((((uint64_t) high) << 32) |
(uint64_t) low);
result.status = KIT_OK;
CloseHandle(f);
return result;
}
#else
struct stat info;
if (stat(buf, &info) == 0 && S_ISREG(info.st_mode)) {
result.size = (int64_t) info.st_size;
# ifndef st_mtime
/* No support for nanosecond timestamps.
*/
result.time_modified_sec = (int64_t) info.st_mtime;
# else
result.time_modified_sec = (int64_t) info.st_mtim.tv_sec;
result.time_modified_nsec = (int32_t) info.st_mtim.tv_nsec;
# endif
result.status = KIT_OK;
return result;
}
#endif
result.status = KIT_ERROR_FILE_DO_NOT_EXIST;
return result;
}
kit_path_list_t kit_file_enum_folder(kit_str_t const path,
kit_allocator_t const alloc) {
PREPARE_PATH_BUF_;
kit_path_list_t result = { .status = KIT_OK };
DA_INIT(result.files, 0, alloc);
#if defined(_WIN32) && !defined(__CYGWIN__)
if (path.size + 7 >= PATH_BUF_SIZE) {
result.status = KIT_ERROR_PATH_TOO_LONG;
return result;
}
buf[path.size + 4] = '\\';
buf[path.size + 5] = '*';
WIN32_FIND_DATAW data;
HANDLE find = FindFirstFileW(buf, &data);
if (find == INVALID_HANDLE_VALUE)
return result;
do {
ptrdiff_t const n = result.files.size;
DA_RESIZE(result.files, n + 1);
if (result.files.size != n + 1) {
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
ptrdiff_t size = 0;
while (size < MAX_PATH && data.cFileName[size] != L'\0') size++;
DA_INIT(result.files.values[n], size, alloc);
if (result.files.values[n].size != size) {
DA_RESIZE(result.files, n);
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
for (ptrdiff_t i = 0; i < size; i++)
result.files.values[n].values[i] = data.cFileName[i];
} while (FindNextFileW(find, &data) != 0);
FindClose(find);
#else
DIR *directory = opendir(buf);
if (directory == NULL)
return result;
for (;;) {
struct dirent *entry = readdir(directory);
if (entry == NULL)
break;
if (entry->d_name[0] == '.')
continue;
ptrdiff_t const n = result.files.size;
DA_RESIZE(result.files, n + 1);
if (result.files.size != n + 1) {
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
ptrdiff_t const size = (ptrdiff_t) strlen(entry->d_name);
DA_INIT(result.files.values[n], size, alloc);
if (result.files.values[n].size != size) {
DA_RESIZE(result.files, n);
result.status = KIT_ERROR_BAD_ALLOC;
break;
}
if (size > 0)
memcpy(result.files.values[n].values, entry->d_name, size);
}
closedir(directory);
#endif
return result;
}
void kit_path_list_destroy(kit_path_list_t list) {
ptrdiff_t i;
for (i = 0; i < list.files.size; i++)
DA_DESTROY(list.files.values[i]);
DA_DESTROY(list.files);
}
#endif
|