-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpath.h
More file actions
1340 lines (1164 loc) · 51.3 KB
/
path.h
File metadata and controls
1340 lines (1164 loc) · 51.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
#ifndef MODULE_PATH
#define MODULE_PATH
#include "assert.h"
#include "log.h"
#include "string.h"
#include "scratch.h"
// This is a not exhaustive filepath handling facility.
// We require all strings to pass through some basic parsing and be wrapped in Path struct.
// This makes it easy to distinguish designated paths from any other strings.
// Further we define Path_Builder which is guaranteed to always be in normalized form.
//
// Path_Info Represents the following:
//
// \\?\C:/Users/Program_Files/./../Dir/file.txt
// <--><-><-------------------------->|<------>
// P R D | F <->
// M* E
//
// Where:
// P - prefix_size - this is OS specific (win32) prefix that carries meta data
// R - root_size
// D - directories_size
// F - filename_size
// E - extension_size
// M* - This / is explicitly not including in directory_size.
// This is because non normalized directory paths can but dont have to end
// on /. This makes sure that both cases have the same size.
//
// All handling in this file respects the above categories and nothing more.
// Notably prefix is ignored in almost all operations but still is properly propagated when appending.
//
// Path_Builder is in normalized form. The following algorithm is used for normalization:
// (modified version of the one use std::filesystem::path. We respect "/" trailing to denote directories. We also respect windows prefixes.)
// 1 - If the path is empty, stop (normal form of an empty path is an empty path).
// 2 - Replace each directory-separator (which may consist of multiple /) with a single /
// 3 - Replace each slash character in the root-name with / (but not in prefix which is left unchanged!)
// 4 - Remove each dot and any immediately following /.
// 5 - Remove each non-dot-dot filename immediately followed by a / and a dot-dot.
// 6 - If there is root-directory, remove all dot-dots and any / immediately following them.
// 7 - Remove trailing /.
// 8 - If the path is empty, add a dot.
// 8 - Insert back trailing / if is directory path.
//
// The canonical path has the following consistency:
// 1) Path_Info is up to date including segment_count.
// 2) Path_Info.is_directory <=> ends with /
// 3) Includes only / (and not \)
// 4) Absolute paths do not contain any "." or ".." segments
// 5) Relative paths are either only "." and nothing more or dont contain "." at all.
// Relative paths contain ".." segments only as the first segment.
//
//@NOTE: We attempt to cover a few edge cases and gain as much insight into the
// path as we can but we by no means attempt to be exhaustively correct
// for all special windows cases. As such this should be viewed as an
// approximation of the exact solution rather than the final product.
typedef enum Path_Root_Kind {
PATH_ROOT_NONE = 0,
PATH_ROOT_SLASH,
PATH_ROOT_SLASH_SLASH,
PATH_ROOT_SERVER,
PATH_ROOT_WIN,
PATH_ROOT_UNKNOWN,
} Path_Root_Kind;
typedef struct Path_Info {
i32 prefix_size;
i32 root_content_from;
i32 root_content_to;
i32 root_size;
i32 directories_size;
i32 filename_size;
i32 extension_size;
i32 segment_count; //0 unless is_normalized
Path_Root_Kind root_kind;
bool is_absolute;
bool is_directory;
bool is_normalized;
bool has_trailing_slash;
//is_normalized denotes if is in the canonical representation
// is only set for Path_Info from Path_Builder, but is still
// useful since we use Path as the interface type and thus
// the is_normalized propagates. This can be used to not waste time
// renormalizing again already normalized path.
} Path_Info;
typedef union Path {
struct {
String string;
Path_Info info;
};
struct {
const char* data;
isize count;
};
} Path;
EXTERNAL bool is_path_sep(char c);
EXTERNAL isize string_find_first_path_separator(String string, isize from);
EXTERNAL isize string_find_last_path_separator(String string, isize from);
EXTERNAL Path path_parse(String path);
EXTERNAL Path path_parse_cstring(const char* path);
EXTERNAL void path_parse_root(String path, Path_Info* info);
EXTERNAL void path_parse_rest(String path, Path_Info* info);
EXTERNAL bool path_is_empty(Path path);
EXTERNAL bool path_is_equal(Path a, Path b); //Compares the textual representations of a and b for equality. To get more acurate results normalize the paths first.
EXTERNAL bool path_is_equal_except_prefix(Path a, Path b); //Compares the textual representations of a and b without prefix for equality. To get more acurate results normalize the paths first.
EXTERNAL String path_get_prefix(Path path);
EXTERNAL String path_get_root(Path path);
EXTERNAL String path_get_directory(Path path);
EXTERNAL String path_get_extension(Path path);
EXTERNAL String path_get_filename(Path path);
EXTERNAL String path_get_root_content(Path path);
EXTERNAL String path_get_filename_without_extension(Path path);
EXTERNAL String path_get_without_trailing_slash(Path path);
EXTERNAL String path_get_segments(Path path);
EXTERNAL Path path_strip_prefix(Path path);
EXTERNAL Path path_strip_root(Path path);
EXTERNAL Path path_strip_trailing_slash(Path path);
EXTERNAL Path path_strip_last_segment(Path path, String* last_segment_or_null);
EXTERNAL Path path_strip_first_segment(Path path, Path* first_segment_or_null);
EXTERNAL Path path_strip_to_containing_directory(Path path);
typedef struct Path_Segement_Iterator {
String segment;
isize segment_number; //one based segment index
isize segment_from;
isize segment_to;
} Path_Segement_Iterator;
EXTERNAL bool path_segment_iterate_string(Path_Segement_Iterator* it, String path, isize till_root_size);
EXTERNAL bool path_segment_iterate(Path_Segement_Iterator* it, Path path);
typedef union Path_Builder {
struct {
String_Builder builder;
Path_Info info;
};
struct {
Allocator* allocator;
isize capacity;
union {
String string;
struct {
char* data;
isize count;
};
};
};
struct {
Allocator* _1;
isize _2;
Path path;
};
} Path_Builder;
enum {
PATH_FLAG_APPEND_EVEN_WITH_ERROR = 1, //Allows append: C:/hello/world + C:/file.txt == C:/hello/world/file.txt but still returns false
PATH_FLAG_NO_REMOVE_DOT = 4, //Treats "." segement as any other segment
PATH_FLAG_NO_REMOVE_DOT_DOT = 8, //Treats ".." segement as any other segment
PATH_FLAG_BACK_SLASH = 16, //Changes to use '\' instead of '/'
PATH_FLAG_TRANSFORM_TO_DIR = 32, //Adds trailing /
PATH_FLAG_TRANSFORM_TO_FILE = 64, //Removes trailing /
PATH_FLAG_NO_ROOT = 128, //Does not append root (for normalize this meens the result will not have root).
PATH_FLAG_NO_PREFIX = 256, //Does not append prefix (for normalize this meens the result will not have prefix)
};
EXTERNAL void path_builder_deinit(Path_Builder* builder);
EXTERNAL void path_builder_init(Path_Builder* builder, Allocator* alloc_or_null, isize initial_capacity_or_zero);
EXTERNAL Path_Builder path_builder_make(Allocator* alloc_or_null, isize initial_capacity_or_zero);
EXTERNAL bool path_builder_append(Path_Builder* into, Path path, int flags);
EXTERNAL void path_builder_assign(Path_Builder* into, Path path, int flags);
EXTERNAL Path_Builder path_builder_dup(Allocator* alloc, Path_Builder to_copy);
EXTERNAL void path_builder_clear(Path_Builder* builder);
EXTERNAL void path_normalize_in_place(Path_Builder* path, int flags);
//Normalizes the given path removing '..', '.', double slashes, converting slashes to '/'.
//When given relative resp. absolute path the output is relative resp. absolute.
//When given path with prefix output path will have the same prefix.
//Accepts additional flags to tweak some of the behaviour.
EXTERNAL Path_Builder path_normalize(Allocator* alloc, Path path, int flags);
EXTERNAL Path_Builder path_concat(Allocator* alloc, Path a, Path b);
EXTERNAL Path_Builder path_concat_many(Allocator* alloc, const Path* paths, isize path_count);
EXTERNAL void path_make_relative_into(Path_Builder* into, Path relative_to, Path path);
EXTERNAL void path_make_absolute_into(Path_Builder* into, Path relative_to, Path path);
EXTERNAL Path_Builder path_make_relative(Allocator* alloc, Path relative_to, Path path);
EXTERNAL Path_Builder path_make_absolute(Allocator* alloc, Path relative_to, Path path);
EXTERNAL Path path_get_executable();
EXTERNAL Path path_get_executable_directory();
EXTERNAL Path path_get_startup_working_directory();
EXTERNAL Path_Builder path_get_current_working_directoryXXX(Allocator* alloc, Platform_Error* error_or_null);
#endif
#if (defined(MODULE_IMPL_ALL) || defined(MODULE_IMPL_PATH)) && !defined(MODULE_HAS_IMPL_PATH)
#define MODULE_HAS_IMPL_PATH
#ifndef PROFILE_START
#define PROFILE_START(...)
#define PROFILE_STOP(...)
#endif
EXTERNAL bool is_path_sep(char c)
{
return c == '/' || c == '\\';
}
EXTERNAL isize string_find_first_path_separator(String string, isize from)
{
for(isize i = from; i < string.count; i++)
if(string.data[i] == '/' || string.data[i] == '\\')
return i;
return -1;
}
EXTERNAL isize string_find_last_path_separator(String string, isize from)
{
for(isize i = from; i-- > 0; )
if(string.data[i] == '/' || string.data[i] == '\\')
return i;
return -1;
}
EXTERNAL void path_parse_root(String path, Path_Info* info)
{
PROFILE_START();
info->prefix_size = 0;
info->root_content_from = 0;
info->root_content_to = 0;
info->root_size = 0;
info->root_kind = PATH_ROOT_NONE;
info->is_absolute = false;
String prefix_path = path;
//https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
String win32_file_namespace = STRING("\\\\?\\"); // "\\?\"
String win32_device_namespace = STRING("\\\\.\\"); // "\\.\"
//Attempt to parse windows prefixes
if(string_is_prefixed_with(prefix_path, win32_file_namespace))
{
info->prefix_size = (i32) win32_file_namespace.count;
}
else if(string_is_prefixed_with(prefix_path, win32_device_namespace))
{
info->prefix_size = (i32) win32_device_namespace.count;
}
i32 root_from = info->prefix_size;
String root_path = string_tail(prefix_path, info->prefix_size);
if(root_path.count == 0)
{
info->is_absolute = false;
info->is_normalized = true;
}
else
{
//Windows UNC server path //My_Root
if(root_path.count >= 2 && is_path_sep(root_path.data[0]) && is_path_sep(root_path.data[1]))
{
if(root_path.count == 2)
{
info->root_content_from = 0;
info->root_content_to = 0;
info->root_size = 2;
info->root_kind = PATH_ROOT_SLASH_SLASH;
}
else
{
isize root_end = string_find_first_path_separator(root_path, 2);
if(root_end == -1)
{
info->root_content_from = root_from + 2;
info->root_content_to = (i32) root_path.count;
info->root_size = (i32) root_path.count;
}
else
{
info->root_content_from = root_from + 2;
info->root_content_to = root_from + (i32) root_end;
info->root_size = (i32) root_end + 1;
}
info->root_kind = PATH_ROOT_SERVER;
}
info->is_absolute = true;
}
//unix style root
else if(root_path.count >= 1 && is_path_sep(root_path.data[0]))
{
info->root_content_from = root_from;
info->root_content_to = root_from;
info->is_absolute = true;
info->root_size = 1;
info->root_kind = PATH_ROOT_SLASH;
}
//Windows style root
else if(root_path.count >= 2 && char_is_alpha(root_path.data[0]) && root_path.data[1] == ':')
{
info->root_content_from = root_from;
info->root_content_to = root_from + 1;
//In windows "C:some_file" means relative path on the drive C
// while "C:/some_file" is absolute path starting from root C
if(root_path.count >= 3 && is_path_sep(root_path.data[2]))
{
info->is_absolute = true;
info->root_size = 3;
}
else
{
info->is_absolute = false;
info->root_size = 2;
}
info->root_kind = PATH_ROOT_WIN;
}
}
PROFILE_STOP();
}
EXTERNAL void path_parse_rest(String path, Path_Info* info)
{
PROFILE_START();
//Clear the overriden
info->is_directory = false;
info->is_directory = false;
info->directories_size = 0;
info->filename_size = 0;
info->extension_size = 0;
String root_path = string_tail(path, info->prefix_size);
String directory_path = string_tail(path, info->prefix_size + info->root_size);
if(root_path.count <= 0)
{
info->is_directory = true; //empty path is sometimes current directory. Thus is a directory
info->is_normalized = true; //Empty path is invarinat
}
if(directory_path.count <= 0)
{
info->is_directory = true; //just root is considered a directory
}
else
{
//We consider path a directory path if it ends with slash. This incldues just "/" directory
isize last = root_path.count - 1;
ASSERT(last >= 0);
info->is_directory = is_path_sep(root_path.data[last]);
if(info->is_directory)
{
info->directories_size = (i32) directory_path.count - 1;
info->has_trailing_slash = true;
}
}
if(info->is_directory == false)
{
//Find the last directory segment
isize file_i = 0;
isize dir_i = string_find_last_path_separator(directory_path, directory_path.count);
if(dir_i < 0)
dir_i = 0;
else
file_i = dir_i + 1;
info->directories_size = (i32) dir_i;
//Parse filename
String filename_path = string_safe_tail(directory_path, file_i);
if(filename_path.count > 0)
{
//If is . or .. then is actually a directory
if(string_is_equal(filename_path, STRING(".")) || string_is_equal(filename_path, STRING("..")))
{
info->is_directory = true;
info->directories_size = (i32) directory_path.count;
}
else
{
//find the extension if any
isize dot_i = string_find_last_char(filename_path, '.', 0);
if(dot_i == -1)
dot_i = filename_path.count;
else
dot_i += 1;
info->filename_size = (i32) filename_path.count;
info->extension_size = (i32) (filename_path.count - dot_i);
}
}
}
PROFILE_STOP();
}
EXTERNAL bool path_is_empty(Path path)
{
return path.string.count <= path.info.prefix_size;
}
EXTERNAL bool path_is_equal(Path a, Path b)
{
return string_is_equal(a.string, b.string);
}
EXTERNAL bool path_is_equal_except_prefix(Path a, Path b)
{
return string_is_equal(path_strip_prefix(a).string, path_strip_prefix(b).string);
}
EXTERNAL Path path_parse(String path)
{
PROFILE_START();
Path out_path = {path};
path_parse_root(path, &out_path.info);
path_parse_rest(path, &out_path.info);
PROFILE_STOP();
return out_path;
}
EXTERNAL Path path_parse_cstring(const char* path)
{
return path_parse(string_of(path));
}
EXTERNAL String path_get_prefix(Path path)
{
return string_head(path.string, path.info.prefix_size);
}
EXTERNAL String path_get_root(Path path)
{
return string_range(path.string, path.info.prefix_size, path.info.prefix_size + path.info.root_size);
}
EXTERNAL String path_get_root_content(Path path)
{
return string_range(path.string, path.info.root_content_from, path.info.root_content_to);
}
EXTERNAL String path_get_directory(Path path)
{
isize from = path.info.prefix_size + path.info.root_size;
return string_range(path.string, from, from + path.info.directories_size);
}
EXTERNAL String path_get_without_trailing_slash(Path path)
{
if(path.info.has_trailing_slash)
return path.string;
else
return string_head(path.string, path.string.count - 1);
}
EXTERNAL String path_get_segments(Path path)
{
isize from = path.info.prefix_size + path.info.root_size;
isize to = path.string.count;
if(path.info.has_trailing_slash)
to -= 1;
return string_range(path.string, from, to);
}
EXTERNAL String path_get_filename(Path path)
{
return string_range(path.string, path.string.count - path.info.filename_size, path.string.count);
}
EXTERNAL String path_get_filename_without_extension(Path path)
{
String filename = path_get_filename(path);
if(path.info.extension_size > 0)
filename = string_head(filename, filename.count - path.info.extension_size - 1);
return filename;
}
EXTERNAL String path_get_extension(Path path)
{
return string_range(path.string, path.string.count - path.info.extension_size, path.string.count);
}
EXTERNAL Path path_strip_prefix(Path path)
{
Path out = path;
out.string = string_tail(path.string, path.info.prefix_size);
out.info.prefix_size = 0;
return out;
}
EXTERNAL Path path_strip_root(Path path)
{
Path out = path;
out.string = string_tail(path.string, path.info.prefix_size + path.info.root_size);
out.info.prefix_size = 0;
out.info.root_size = 0;
out.info.root_kind = PATH_ROOT_NONE;
out.info.root_content_from = 0;
out.info.root_content_to = 0;
return out;
}
EXTERNAL Path path_strip_trailing_slash(Path path)
{
Path out = path;
if(path.info.has_trailing_slash)
{
out.string.count = MAX(out.string.count - 1, 0);
path_parse_rest(out.string, &out.info);
}
return out;
}
//Splits "C:/path/to/dir/" --> "C:/path/to/" + "dir"
// "path/to/file.txt" --> "path/to/" + "file.txt"
EXTERNAL Path path_strip_last_segment(Path path, String* last_segment_or_null)
{
Path no_trailing = path_strip_trailing_slash(path);
isize split_i = string_find_last_path_separator(no_trailing.string, no_trailing.string.count);
isize root_till = path.info.root_size + path.info.prefix_size;
if(split_i < root_till)
split_i = root_till;
else
split_i += 1;
if(last_segment_or_null)
*last_segment_or_null = string_tail(no_trailing.string, split_i);
Path out = no_trailing;
out.string = string_head(no_trailing.string, split_i);
path_parse_rest(out.string, &out.info);
return out;
}
//Splits "C:/path/to/dir/" --> "C:/path/" + "to/dir/"
// "path/to/file.txt" --> "path/" + "to/file.txt"
EXTERNAL Path path_strip_first_segment(Path path, Path* first_segment_or_null)
{
isize root_till = path.info.root_size + path.info.prefix_size;
isize split_i = string_find_first_path_separator(path.string, root_till);
if(split_i == -1)
split_i = path.string.count;
else
split_i += 1;
if(first_segment_or_null)
{
first_segment_or_null->string = string_head(path.string, split_i);
first_segment_or_null->info = path.info;
path_parse_rest(first_segment_or_null->string, &first_segment_or_null->info);
}
Path out = path;
out.string = string_tail(path.string, split_i);
path_parse_root(out.string, &out.info);
return out;
}
EXTERNAL Path path_strip_to_containing_directory(Path path)
{
if(path.info.is_directory)
return path;
else
return path_strip_last_segment(path, NULL);
}
EXTERNAL bool path_segment_iterate_string(Path_Segement_Iterator* it, String path, isize till_root_size)
{
isize segment_from = till_root_size;
if(it->segment_number != 0)
segment_from = it->segment_to + 1;
if(segment_from >= path.count)
return false;
isize segment_to = string_find_first_path_separator(path, segment_from);
if(segment_to == -1)
segment_to = path.count;
it->segment_number += 1;
it->segment_from = segment_from;
it->segment_to = segment_to;
it->segment = string_range(path, segment_from, segment_to);
return true;
}
EXTERNAL bool path_segment_iterate(Path_Segement_Iterator* it, Path path)
{
return path_segment_iterate_string(it, path.string, path.info.prefix_size + path.info.root_size);
}
EXTERNAL void path_builder_deinit(Path_Builder* builder)
{
builder_deinit(&builder->builder);
memset(builder, 0, sizeof *builder);
}
EXTERNAL void path_builder_init(Path_Builder* builder, Allocator* alloc_or_null, isize initial_capacity_or_zero)
{
builder_init_with_capacity(&builder->builder, alloc_or_null, initial_capacity_or_zero);
memset(&builder->info, 0, sizeof builder->info);
}
EXTERNAL Path_Builder path_builder_make(Allocator* alloc_or_null, isize initial_capacity_or_zero)
{
Path_Builder builder = {builder_make(alloc_or_null, initial_capacity_or_zero)};
return builder;
}
EXTERNAL void path_builder_clear(Path_Builder* builder)
{
memset(&builder->info, 0, sizeof builder->info);
builder_clear(&builder->builder);
}
EXTERNAL bool path_builder_append(Path_Builder* into, Path path, int flags)
{
PROFILE_START();
//@NOTE: this function is the main normalization function. It expects
// into to be in a valid state.
builder_reserve(&into->builder, path.string.count*9/8 + 5);
char slash = (flags & PATH_FLAG_BACK_SLASH) ? '\\' : '/';
bool remove_dot = (flags & PATH_FLAG_NO_REMOVE_DOT) == 0;
bool remove_dot_dot = (flags & PATH_FLAG_NO_REMOVE_DOT_DOT) == 0;
bool transform_dir = (flags & PATH_FLAG_TRANSFORM_TO_DIR) > 0;
bool transform_file = (flags & PATH_FLAG_TRANSFORM_TO_FILE) > 0;
bool add_prefix = (flags & PATH_FLAG_NO_PREFIX) == 0;
bool add_root = (flags & PATH_FLAG_NO_ROOT) == 0;
bool ignore_error = (flags & PATH_FLAG_APPEND_EVEN_WITH_ERROR) == 0;
#ifdef DO_ASSERTS_SLOW
bool has_trailing_slash = false;
String except_root = path_strip_root(into->path).string;
if(except_root.count > 0 && is_path_sep(except_root.data[except_root.count - 1]))
has_trailing_slash = true;
ASSERT(into->info.has_trailing_slash == has_trailing_slash);
#endif
if(into->info.has_trailing_slash)
{
ASSERT(into->info.segment_count > 0);
builder_resize(&into->builder, into->builder.count - 1, '\0');
into->info.has_trailing_slash = false;
}
bool was_empty = path_is_empty(into->path);
bool state = true;
if(add_prefix && was_empty && into->info.prefix_size == 0)
{
String prefix = path_get_prefix(path);
builder_append(&into->builder, prefix);
into->info.prefix_size = (i32) prefix.count;
}
if(path_is_empty(path) == false)
{
if(add_root && path.info.root_kind != PATH_ROOT_NONE)
{
//Only set root when empty else bad bad.
if(was_empty)
{
String root_content = path_get_root_content(path);
ASSERT(into->info.root_kind == PATH_ROOT_NONE);
ASSERT(into->info.root_size == 0);
ASSERT(into->info.root_content_from == 0);
ASSERT(into->info.root_content_to == 0);
switch(path.info.root_kind)
{
case PATH_ROOT_NONE: {
ASSERT(path.info.is_absolute == false);
} break;
case PATH_ROOT_SLASH: {
builder_push(&into->builder, slash);
} break;
case PATH_ROOT_SLASH_SLASH: {
builder_push(&into->builder, slash);
builder_push(&into->builder, slash);
} break;
case PATH_ROOT_SERVER: {
builder_push(&into->builder, slash);
builder_push(&into->builder, slash);
if(path.info.root_content_to > path.info.root_content_from)
{
builder_append(&into->builder, root_content);
builder_push(&into->builder, slash);
}
else
LOG_WARN("path", "Empty prefix '%.*s' with PATH_ROOT_SERVER", STRING_PRINT(root_content));
} break;
case PATH_ROOT_WIN: {
char c = 'C';
if(root_content.count > 0 && char_is_alpha(root_content.data[0]))
c = root_content.data[0];
else
LOG_WARN("path", "Strange prefix '%.*s' with PATH_ROOT_WIN", STRING_PRINT(root_content));
//to uppercase
if('a' <= c && c <= 'z')
c = c - 'a' + 'A';
builder_push(&into->builder, c);
builder_push(&into->builder, ':');
if(path.info.is_absolute)
builder_push(&into->builder, slash);
} break;
case PATH_ROOT_UNKNOWN: {
builder_append(&into->builder, path_get_root(path));
} break;
}
if(path.info.root_kind != PATH_ROOT_NONE)
path_parse_root(into->string, &into->info);
}
else
{
state = false;
}
}
#ifdef DO_ASSERTS_SLOW
Path_Info new_info = path_parse(into->string).info;
ASSERT_SLOW(new_info.prefix_size == into->info.prefix_size);
ASSERT_SLOW(new_info.root_kind == into->info.root_kind);
ASSERT_SLOW(new_info.root_size == into->info.root_size);
ASSERT_SLOW(new_info.root_content_from == into->info.root_content_from);
ASSERT_SLOW(new_info.root_content_to == into->info.root_content_to);
#endif
if(state || ignore_error)
{
//@TODO: inline the loop and root_till!
isize root_till = into->info.root_size + into->info.prefix_size;
for(Path_Segement_Iterator it = {0}; path_segment_iterate(&it, path);)
{
bool push_segment = true;
String segment = it.segment;
//Multiple separators next to each otehr
if(segment.count == 0)
push_segment = false;
//Single dot segment
else if(remove_dot && string_is_equal(segment, STRING(".")))
push_segment = false;
//pop segment
else if(remove_dot_dot && string_is_equal(segment, STRING("..")))
{
if(into->info.is_absolute)
push_segment = false;
else
push_segment = true;
//If there was no segment to pop push the ".." segment
if(into->info.segment_count > 0)
{
isize last_segment_i = string_find_last_path_separator(into->string, into->string.count);
if(last_segment_i < root_till)
last_segment_i = root_till;
String last_segement = string_tail(into->string, last_segment_i);
if(string_is_equal(last_segement, STRING("..")) == false)
{
builder_resize(&into->builder, last_segment_i, '\0');
into->info.segment_count -= 1;
push_segment = false;
}
}
}
//push segment
if(push_segment)
{
if(into->info.segment_count > 0)
builder_push(&into->builder, slash);
builder_append(&into->builder, segment);
into->info.segment_count += 1;
}
}
}
if(path_is_empty(into->path))
{
builder_push(&into->builder, '.');
into->info.segment_count += 1;
}
path_parse_rest(into->string, &into->info);
//We know it 100% does not have trialing slash
ASSERT(into->info.has_trailing_slash == false);
if(into->info.segment_count > 0)
{
//1) If it is a directory but is not trailing slash
// it must be '.' or '..' then we add slash
// because thats the normal form.
//2) If we explicitly want to make a directory
//3) If the path was a directory and we dont want explicitly want to make a file
if(into->info.is_directory || transform_dir || (path.info.is_directory && transform_file == false))
{
builder_push(&into->builder, slash);
path_parse_rest(into->string, &into->info);
}
}
}
else
{
path_parse_rest(into->string, &into->info);
}
into->info.is_normalized = true;
#ifdef DO_ASSERTS_SLOW
Path_Info new_info = path_parse(into->string).info;
new_info.is_normalized = into->info.is_normalized;
new_info.segment_count = into->info.segment_count;
ASSERT(memcmp(&new_info, &into->info, sizeof(new_info)) == 0);
#endif
PROFILE_STOP();
return state;
}
EXTERNAL void path_builder_assign(Path_Builder* into, Path path, int flags)
{
path_builder_clear(into);
path_builder_append(into, path, flags);
}
EXTERNAL Path_Builder path_builder_dup(Allocator* alloc, Path_Builder to_copy)
{
Path_Builder duped = {0};
duped.builder = builder_of(alloc, to_copy.string);
duped.info = to_copy.info;
return duped;
}
EXTERNAL void path_normalize_in_place(Path_Builder* into, int flags)
{
SCRATCH_SCOPE(arena) {
String_Builder copy = builder_of(arena.alloc, into->string);
Path path = path_parse(copy.string);
path_builder_assign(into, path, flags);
}
}
EXTERNAL Path_Builder path_normalize(Allocator* alloc, Path path, int flags)
{
Path_Builder builder = path_builder_make(alloc, 0);
path_builder_append(&builder, path, flags);
return builder;
}
EXTERNAL Path_Builder path_concat_many(Allocator* alloc, const Path* paths, isize path_count)
{
//A simple heuristic to try to guess the needed capacity
isize combined_cap = 10;
for(isize i = 0; i < path_count; i++)
combined_cap += paths->string.count*9/8;
Path_Builder builder = path_builder_make(alloc, combined_cap);
for(isize i = 0; i < path_count; i++)
path_builder_append(&builder, paths[i], 0);
return builder;
}
EXTERNAL Path_Builder path_concat(Allocator* alloc, Path a, Path b)
{
Path paths[2] = {a, b};
return path_concat_many(alloc, paths, 2);
}
EXTERNAL void path_make_relative_into(Path_Builder* into, Path relative_to, Path path)
{
path_builder_clear(into);
PROFILE_START();
//If path is relative path we and the relative_to path is absolute then
// we cannot make it any more relative than it currently is.
//Same happens vice versa.
//If both are empty the result is also empty
if((path.info.is_absolute == false && relative_to.info.is_absolute)
|| (path.info.is_absolute && relative_to.info.is_absolute == false)
|| (path_is_empty(relative_to) && path_is_empty(path)))
{
path_builder_assign(into, path, 0);
}
else
{
SCRATCH_SCOPE(arena)
{
//Make paths normalized if they are not invarinat already.
// It is very likely that at least relative_to will be invarinat since
// most often it will be a path to the current executable which is cached
// in normalized form.
Path reli = path_strip_to_containing_directory(relative_to);
Path pathi = path;
Path_Builder reli_builder = {0};
Path_Builder pathi_builder = {0};
if(relative_to.info.is_normalized == false)
{
reli_builder = path_normalize(arena.alloc, relative_to, 0);
reli = reli_builder.path;
}
if(path.info.is_normalized == false)
{
pathi_builder = path_normalize(arena.alloc, path, 0);
pathi = pathi_builder.path;
}
//If roots differ we cannot make it more relative
if(string_is_equal(path_get_root(reli), path_get_root(pathi)) == false)
path_builder_assign(into, path, 0);
else
{
Path_Segement_Iterator rel_it = {0};
Path_Segement_Iterator path_it = {0};
while(true)
{
bool has_rel = path_segment_iterate(&rel_it, reli);
bool has_path = path_segment_iterate(&path_it, pathi);
bool are_equal = string_is_equal(rel_it.segment, path_it.segment);
path_builder_append(into, path_parse(path_get_prefix(pathi)), 0);
//If both are present and same do nothing
if(has_rel && has_path && are_equal)
{
//nothing
}
//If they were same and end the same then also do nothing
else if(has_rel == false && has_path == false && are_equal)
{
path_builder_append(into, path_parse_cstring("."), 0);
break;
}
else
{
//If rel is shorter than path add all remainig segments of `path` into `into`
if(has_rel == false)
{
builder_append(&into->builder, path_it.segment);
while(path_segment_iterate(&path_it, pathi))
{
builder_push(&into->builder, '/');
builder_append(&into->builder, path_it.segment);
}
}
//If there was a difference in the path or path is shorter
// we add appropriate amountof ".." segments then the rest of the path
else
{
builder_append(&into->builder, STRING(".."));
while(path_segment_iterate(&rel_it, reli))
builder_append(&into->builder, STRING("/.."));
builder_push(&into->builder, '/');
builder_append(&into->builder, path_it.segment);
while(path_segment_iterate(&path_it, pathi))
{
builder_push(&into->builder, '/');
builder_append(&into->builder, path_it.segment);
}
}
path_normalize_in_place(into, path.info.is_directory ? PATH_FLAG_TRANSFORM_TO_DIR : PATH_FLAG_TRANSFORM_TO_FILE);
break;
}
}
}
}
}
PROFILE_STOP();
}