-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPath.m
More file actions
1000 lines (1000 loc) · 37.6 KB
/
Copy pathPath.m
File metadata and controls
1000 lines (1000 loc) · 37.6 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
classdef Path < matlab.mixin.CustomDisplay
% Path Represents file system paths
%
% For details, visit the <a href="matlab:
% web('https://github.com/MartinKoch123/Path')">documentation on GitHub</a>.
properties (Access = private)
extension_
stem_
parent_
end
properties (Constant, Access=private, Hidden)
FILE_SEPARATOR_REGEX = regexptranslate("escape", filesep);
DOCUMENTATION_WEB_PAGE = "https://github.com/MartinKoch123/Path";
ROOT_REGEX_WINDOWS = "^(\\\\[^\\]+|[A-Za-z]:|)";
ROOT_REGEX_POSIX = "^(/[^/]*|)";
IS_WINDOWS = ispc;
ROOT_REGEX = ifThenElse(Path.IS_WINDOWS, Path.ROOT_REGEX_WINDOWS, Path.ROOT_REGEX_POSIX)
end
methods
function obj = Path(paths)
arguments (Repeating)
paths (1, :) string {Path.mustBeNonmissing};
end
% Default constructor
if isempty(paths)
paths = {"."};
end
paths = Path.clean(paths{:});
n = length(paths);
% Empty constructor
if isempty(paths)
obj = obj.empty;
return
end
obj(n) = obj;
fs = Path.FILE_SEPARATOR_REGEX;
for i = 1 : n
% Extract parent directory and name.
match = regexp(paths(i), "^(?<parent>.*?)(?<name>[^"+fs+"]+)$", "names");
% Extract stem and extension from name.
if match.name == ".." || match.name == "."
stem = match.name;
extension = "";
else
match2 = regexp(match.name, "^(?<stem>.*?)(?<extension>(\.[^\.]*|))$", "names");
extension = match2.extension;
stem = match2.stem;
end
obj(i).parent_ = match.parent;
obj(i).stem_ = stem;
obj(i).extension_ = extension;
end
end
%% Conversion
function result = string(objects)
if isempty(objects)
result = strings(1, 0);
return
end
result = [objects.parent_] + [objects.stem_] + [objects.extension_];
end
function result = char(obj)
arguments
obj (1, 1)
end
result = char(obj.string);
end
function result = cellstr(objects)
result = cellstr(objects.string);
end
function result = quote(objects)
result = """" + string(objects) + """";
end
%% Name
function result = name(objects)
result = Path(objects.nameString);
end
function result = setName(objects, varargin)
result = objects.parent.join(varargin{:});
end
function result = nameString(objects)
if isempty(objects)
result = strings(1, 0);
return
end
result = [objects.stem_] + [objects.extension_];
end
%% Stem
function result = stem(objects)
result = objects.selectString(@(obj) obj.stem_);
end
function results = setStem(objects, stem)
arguments
objects(1, :)
stem (1, :) string {Path.mustBeValidName, Path.mustBeEqualSizeOrScalar(stem, objects)}
end
if isscalar(stem)
stem = repmat(stem, 1, objects.count);
end
results = Path([objects.parent_] + stem + [objects.extension_]);
end
function objects = addStemSuffix(objects, suffix)
arguments
objects(1, :)
suffix (1, :) string {Path.mustBeValidName, Path.mustBeEqualSizeOrScalar(suffix, objects)}
end
if isscalar(suffix)
suffix = repmat(suffix, 1, objects.count);
end
for i = 1 : length(objects)
objects(i).stem_ = objects(i).stem_ + suffix(i);
end
end
%% Extension
function result = extension(objects)
result = objects.selectString(@(obj) obj.extension_);
end
function results = setExtension(objects, extension)
arguments
objects (1, :)
extension (1, :) string {Path.mustBeValidExtension, Path.mustBeEqualSizeOrScalar(extension, objects)}
end
missesDotAndIsNonEmpty = ~extension.startsWith(".") & strlength(extension) > 0;
extension(missesDotAndIsNonEmpty) = "." + extension(missesDotAndIsNonEmpty);
results = Path([objects.parent_] + [objects.stem_] + extension);
end
%% Parent
function result = parent(objects)
result = Path(objects.parentString);
end
function result = parentString(objects)
if isempty(objects)
result = strings(1, 0);
return
end
result = [objects.parent_];
result = regexprep(result, Path.FILE_SEPARATOR_REGEX + "$", "");
result(result == "") = ".";
end
function objects = setParent(objects, parent)
arguments
objects(1, :)
parent (1, :) {Path.mustBeEqualSizeOrScalar(parent, objects)}
end
if isscalar(parent)
parent = repmat(parent, 1, objects.count);
end
for i = 1 : length(objects)
objects(i).parent_ = parent(i).string + filesep;
end
end
function result = hasParent(objects)
result = objects.is("ParentNot", ".");
end
%% Root
function result = root(objects)
result = Path(objects.rootString);
end
function result = rootString(objects)
if isempty(objects)
result = strings(1, 0);
return
end
result = regexp(objects.string, Path.ROOT_REGEX, "match", "emptymatch", "once");
result(result == "") = ".";
end
function result = setRoot(objects, root)
arguments
objects
root (1, 1) string {Path.mustNotContainPathSeparator}
end
root = root + filesep;
result = objects.selectPath( ...
@(obj) Path(regexprep(obj.string, Path.ROOT_REGEX, root, "emptymatch")), ...
objects.empty);
end
%% Properties
function result = isRelative(objects)
result = [objects.root] == ".";
end
function result = isAbsolute(objects)
result = ~objects.isRelative;
end
function result = eq(objects, others)
result = objects.string == others.string;
end
function result = ne(objects, others)
result = ~objects.eq(others);
end
function result = parts(obj)
arguments
obj (1, 1)
end
result = regexp(obj.string, Path.FILE_SEPARATOR_REGEX, "split");
result(strlength(result) == 0) = [];
end
function result = strlength(obj)
result = obj.string.strlength;
end
%% Join
function varargout = join(objects, other)
arguments
objects(1, :)
end
arguments (Repeating)
other (1, :) string {Path.mustBeNonmissing}
end
other = Path.clean(other{:});
if isempty(objects) || isempty(other)
result = objects;
elseif isscalar(objects) || isscalar(other) || length(objects) == length(other)
result = Path(objects.string + filesep + other.string);
else
error("Path:join:LengthMismatch", "Length of Path array, %i, and length of joined array, %i, must either match or one of them must be scalar.", length(objects), length(other));
end
varargout = deal_(result, nargout);
end
function varargout = mrdivide(objects, other)
arguments
objects(1, :)
end
arguments (Repeating)
other (1, :) string {Path.mustBeNonmissing}
end
result = objects.join(other);
varargout = deal_(result, nargout);
end
function varargout = mldivide(objects, other)
result = objects.join(other);
varargout = deal_(result, nargout);
end
function result = addSuffix(objects, suffix)
arguments
objects(1, :) string
suffix (1, :) string {Path.mustBeNonmissing, Path.mustBeEqualSizeOrScalar(suffix, objects)}
end
result = Path(objects + suffix);
end
function result = plus(objects, suffix)
arguments
objects (1, :) string
suffix (1, :) string {Path.mustBeNonmissing, Path.mustBeEqualSizeOrScalar(suffix, objects)}
end
result = Path(objects + suffix);
end
%% Filter
function result = where(objects, options)
arguments
objects
options.Path (1, :) string = "*"
options.PathNot (1, :) string = strings(0);
options.Name (1, :) string = "*"
options.NameNot (1, :) string = strings(0)
options.Stem (1, :) string = "*"
options.StemNot (1, :) string = strings(0);
options.Extension (1, :) string = "*"
options.ExtensionNot (1, :) string = strings(0)
options.Parent (1, :) string = "*"
options.ParentNot (1, :) string = strings(0)
options.Root (1, :) string = "*"
options.RootNot (1, :) string = strings(0)
end
args = namedargs2cell(options);
keep = objects.is(args{:});
result = objects(keep);
if isempty(result)
result = Path.empty;
end
end
function result = is(objects, options)
arguments
objects
options.Path (1, :) string = "*"
options.PathNot (1, :) string = strings(0);
options.Name (1, :) string = "*"
options.NameNot (1, :) string = strings(0)
options.Stem (1, :) string = "*"
options.StemNot (1, :) string = strings(0);
options.Extension (1, :) string = "*"
options.ExtensionNot (1, :) string = strings(0)
options.Parent (1, :) string = "*"
options.ParentNot (1, :) string = strings(0)
options.Root (1, :) string = "*"
options.RootNot (1, :) string = strings(0)
end
for option = ["Path", "PathNot", "Name", "NameNot", "Stem", "StemNot", ...
"Extension", "ExtensionNot", "Parent", "ParentNot", "Root", "RootNot"]
options.(option) = Path.clean(options.(option));
end
paths = objects.string;
names = objects.nameString;
stems = objects.stem;
extensions = objects.extension;
parents = objects.parentString;
roots = objects.rootString;
result = ...
Path.matches(paths, options.Path, true) & ...
Path.matches(paths, options.PathNot, false) & ...
Path.matches(names, options.Name, true) & ...
Path.matches(names, options.NameNot, false) & ...
Path.matches(stems, options.Stem, true) & ...
Path.matches(stems, options.StemNot, false) & ...
Path.matches(extensions, options.Extension, true) & ...
Path.matches(extensions, options.ExtensionNot, false) & ...
Path.matches(parents, options.Parent, true) & ...
Path.matches(parents, options.ParentNot, false) & ...
Path.matches(roots, options.Root, true) & ...
Path.matches(roots, options.RootNot, false);
end
%% Absolute/Relative
function result = absolute(objects, referenceDir)
arguments
objects
referenceDir (1, 1) = Path(pwd)
end
if referenceDir.isRelative
referenceDir = referenceDir.absolute;
end
isRelative = objects.isRelative;
result = objects;
result(isRelative) = Path(referenceDir.string + filesep + objects(isRelative).string);
end
function result = relative(objects, referenceDir)
arguments
objects
referenceDir (1, 1) = Path(pwd)
end
paths = objects.absolute;
referenceDir = referenceDir.absolute;
referenceParts = referenceDir.parts;
nReferenceParts = length(referenceParts);
result = Path(strings(0));
for path = paths
parts = path.parts;
nParts = length(parts);
nLower = min([nParts, nReferenceParts]);
nEqualParts = find([parts(1:nLower) ~= referenceParts(1:nLower), true], 1) - 1;
if nEqualParts == 0
error("Path:relative:RootsDiffer", "Roots of path ""%s"" and reference directory ""%s"" differ.", path, referenceDir); end
dirUps = join([repmat("..", 1, nReferenceParts - nEqualParts), "."], filesep);
keptTail = join([".", parts(nEqualParts+1 : end)], filesep);
result(end+1) = Path(dirUps + filesep + keptTail);
end
end
%% Regex
function result = regexprep(objects, expression, replace, varargin)
arguments
objects (1, :)
expression
replace
end
arguments (Repeating)
varargin
end
result = Path(regexprep(objects.string, expression, replace, varargin{:}));
end
%% File systen interaction
function result = exists(objects)
result = objects.selectLogical(@(obj) isfile(obj.string) || isfolder(obj.string));
end
function result = isFile(objects)
result = objects.selectLogical(@(obj) isfile(obj.string));
end
function result = isDir(objects)
result = objects.selectLogical(@(obj) isfolder(obj.string));
end
function mustExist(objects)
for obj = objects
if ~obj.exists
throwAsCaller(obj.notFoundException);
end
end
end
function mustBeDir(objects)
objects.mustExist;
for obj = objects
if ~obj.isDir
MException("Path:NotADir", "Path '%s' exists but is not a directory.", obj.string).throwAsCaller;
end
end
end
function mustBeFile(objects)
objects.mustExist;
for obj = objects
if ~obj.isFile
MException("Path:NotAFile", "Path '%s' exists but is not a file.", obj.string).throwAsCaller;
end
end
end
function result = modifiedDate(objects)
result(objects.count) = datetime;
for i = 1 : objects.count
content = dir(objects(i).string);
if objects(i).isFile
datenum = content.datenum;
else
objects(i).mustBeDir
datenum = content({content.name} == ".").datenum;
end
result(i) = datetime(datenum, "ConvertFrom", "datenum");
end
end
function varargout = fopen(obj, varargin)
arguments
obj (1, 1)
end
arguments (Repeating)
varargin
end
[varargout{1:nargout}] = fopen(obj.string, varargin{:});
end
function [id, autoClose] = open(obj, permission, varargin)
arguments
obj (1, 1)
permission (1, 1) string = "r";
end
arguments (Repeating); varargin; end
if permission.startsWith("r")
obj.mustBeFile;
else
obj.parent.mkdir;
end
[id, errorMessage] = obj.fopen(permission, varargin{:});
if id == -1
error(errorMessage); end
if nargout == 2
autoClose = onCleanup(@() tryToClose(id)); end
end
function [id, autoClose] = openForReading(obj)
id = obj.open;
if nargout == 2
autoClose = onCleanup(@() tryToClose(id)); end
end
function [id, autoClose] = openForWriting(obj)
id = obj.open("w");
if nargout == 2
autoClose = onCleanup(@() tryToClose(id)); end
end
function [id, autoClose] = openForWritingText(obj)
id = obj.open("wt");
if nargout == 2
autoClose = onCleanup(@() tryToClose(id)); end
end
function [id, autoClose] = openForAppendingText(obj)
id = obj.open("at");
if nargout == 2
autoClose = onCleanup(@() tryToClose(id)); end
end
function createEmptyFile(objects)
for obj = objects
[~, autoClose] = obj.openForWriting;
end
end
function varargout = cd(obj)
arguments
obj (1, 1)
end
if nargout == 1
varargout = {Path.pwd};
end
try
cd(obj.string);
catch exception
throwAsCaller(exception);
end
end
function mkdir(objects)
for obj = objects
if obj.exists
return;
end
try
mkdir(obj.string);
catch exception
Path.extendError(exception, "MATLAB:MKDIR", "Error while creating directory ""%s"".", obj);
end
end
end
function result = listFiles(objects)
files = strings(1, 0);
objects.mustBeDir;
for obj = objects.unique_
contentInfo = dir(obj.string);
fileInfoList = contentInfo(~[contentInfo.isdir]);
for fileInfo = fileInfoList'
files(end+1) = obj.string + "\" + fileInfo.name;
end
end
result = Path(files);
end
function result = listDeepFiles(objects)
files = strings(1, 0);
objects.mustBeDir;
for obj = objects.unique_
files = [files, listDeepPaths(obj.string, true)];
end
result = Path(files);
end
function result = listDirs(objects)
dirs = strings(1, 0);
objects.mustBeDir;
for obj = objects.unique_
contentInfo = dir(obj.string);
dirInfoList = contentInfo([contentInfo.isdir]);
for dirInfo = dirInfoList'
if ismember(dirInfo.name, [".", ".."])
continue; end
dirs(end+1) = obj.string + "\" + dirInfo.name;
end
end
result = Path(dirs);
end
function result = listDeepDirs(objects)
dirs = strings(1, 0);
objects.mustBeDir;
for obj = objects.unique_
dirs = [dirs, listDeepPaths(obj.string, false)];
end
result = Path(dirs);
end
function delete(objects, varargin)
for obj = objects
if obj.isFile
delete(obj.string)
elseif obj.isDir
rmdir(obj.string, varargin{:});
end
end
end
function result = readText(obj)
arguments
obj (1, 1)
end
obj.mustBeFile;
result = string(fileread(obj.string));
result = result.replace(sprintf("\r\n"), newline);
end
function writeText(obj, text)
arguments
obj (1, 1)
text (1, 1) string
end
[fileId, autoClose] = obj.openForWritingText;
fprintf(fileId, "%s", text);
end
function result = bytes(objects)
objects.mustBeFile;
result = zeros(1, objects.count);
for i = 1:objects.count
result(i) = dir(objects(i).string).bytes;
end
end
%% Copy and move
function copy(objects, targets)
arguments
objects
targets (1, :)
end
objects.copyOrMove(targets, true, false)
end
function move(objects, targets)
arguments
objects
targets (1, :)
end
objects.copyOrMove(targets, false, false);
end
function copyToDir(objects, targets)
arguments
objects
targets (1, :)
end
objects.copyOrMove(targets, true, true);
end
function moveToDir(objects, targets)
arguments
objects
targets (1, :)
end
objects.copyOrMove(targets, false, true);
end
%% Save and load
function save(obj, variables)
arguments
obj (1, 1)
end
arguments (Repeating)
variables (1, 1) string {Path.mustBeValidVariableName}
end
if isempty(variables)
error("Path:load:MissingArgument", "Not enough inputs arguments.");
end
for variable = [variables{:}]
saveStruct.(variable) = evalin("caller", variable);
end
obj.parent.mkdir;
save(obj.string, "-struct", "saveStruct");
end
function varargout = load(obj, variables)
arguments
obj (1, 1)
end
arguments (Repeating)
variables (1, 1) string {Path.mustBeValidVariableName}
end
if nargout ~= length(variables)
error("Path:load:InputOutputMismatch", "The number of outputs, %i, must match the number of variables to load, %i.", nargout, length(variables)); end
data = load(obj.string, variables{:});
varargout = {};
for variable = string(variables)
if ~isfield(data, variable)
error("Path:load:VariableNotFound", "Variable ""%s"" not found in file ""%s"".", variable, obj); end
varargout{end+1} = data.(variable);
end
end
%% Array
function result = isEmpty(objects)
result = isempty(objects);
end
function result = count(objects)
result = numel(objects);
end
function [result, indices] = sort(objects, varargin)
[~, indices] = sort(objects.string, varargin{:});
result = objects(indices);
end
function varargout = unique_(objects, varargin)
[varargout{1:nargout}] = unique(objects.string, varargin{:});
varargout{1} = Path(varargout{1});
end
function varargout = deal(objects)
if nargout ~= objects.count
error("Path:deal:InvalidNumberOfOutputs", "Object array length does not match the number of output arguments."); end
for i = 1:nargout
varargout{i} = objects(i);
end
end
function result = vertcat(obj, varargin)
result = horzcat(obj, varargin);
end
function result = subsasgn(obj, s, varargin)
indices = s(end).subs;
if (length(indices) == 2 && indices{1} ~= 1) || length(indices) > 2
e = MException("Path:subsasgn:MultiRowsNotSupported", "Column vectors and 2D arrays are not supported. Use only one indexing dimension instead (""linear indexing""). Example: ""a(2:4) = b"".");
e.throwAsCaller;
end
result = builtin("subsasgn", obj, s, varargin{:});
end
function transpose(~)
MException("Path:transpose:NotSupported", "Transpose operation is not supported.").throwAsCaller;
end
function ctranspose(~)
MException("Path:transpose:NotSupported", "Transpose operation is not supported.").throwAsCaller;
end
function result = tempFileName(obj, n)
arguments
obj (1, 1)
n (1, 1) {mustBeNonnegative, mustBeInteger} = 1
end
result = Path.empty;
for i = 1:n
result(i) = tempname(obj.string);
end
end
end
methods (Static)
function result = ofMatlabFile(elements)
arguments
elements (1, :) string {Path.mustBeNonmissing}
end
result = Path.empty;
for element = elements
path = string(which(element));
% If the queried element happens to have the name of a
% variable in this function, temporarily rename that
% variable.
if path == "variable"
temp = eval(element);
clearvars(element);
path = string(which(element));
eval(element + " = temp");
end
if path.startsWith("built")
% Remove "build in" and brackets.
path = regexprep(path, ["^[^\(]*\(", "\)$"], "");
elseif path == ""
error("Path:ofMatlabFile:NotFound", "Element ""%s"" is not on the search path.", element);
end
result(end+1) = Path(path);
end
end
function result = this(level)
arguments
level (1, 1) double {mustBeInteger, mustBePositive} = 1
end
stack = dbstack("-completenames");
if length(stack) < level + 1
error("Path:this:NotAFile", "This method was not called from another file" + ifThenElse(level == 1, "", " at the requested stack level") + "."); end
callingFilePath = string(stack(level + 1).file);
callingFileBaseName = regexp(callingFilePath.string, "(+[\w\d_]+(\\|/))*[\w\d_\.]+$", "match", "once");
if callingFileBaseName.startsWith("LiveEditorEvaluationHelper")
error("Path:this:LiveScript", "Calling this method from a live script is not supported. Consider using 'Path.ofMatlabFile' instead. Example: Path.ofMatlabFile(""PathExamples.mlx"")."); end
result = Path.ofMatlabFile(callingFileBaseName);
end
function result = here(level)
arguments
level (1, 1) double {mustBeInteger, mustBePositive} = 1
end
result = Path.this(level + 1).parent;
end
function result = empty
result = Path;
result = result(double.empty(1, 0));
end
function result = pwd
result = Path(pwd);
end
function result = home
if Path.IS_WINDOWS
result = Path(getenv("USERPROFILE"));
else
result = Path(getenv("HOME"));
end
end
function result = matlab
result = Path(matlabroot);
end
function result = searchPath
result = Path(path);
end
function result = userPath
result = Path(userpath);
end
function result = tempFile(n)
arguments
n (1, 1) {mustBeNonnegative, mustBeInteger} = 1
end
result = Path.empty;
for i = 1:n
result(i) = Path(tempname);
end
end
function result = tempDir
result = Path(tempdir);
end
function help
web(Path.DOCUMENTATION_WEB_PAGE);
end
end
methods (Access = private)
function result = selectString(objects, fun)
result = strings(size(objects));
for i = 1 : numel(objects)
result(i) = fun(objects(i));
end
end
function result = selectLogical(objects, fun)
result = true(size(objects));
for i = 1 : numel(objects)
result(i) = fun(objects(i));
end
end
function result = selectPath(objects, fun, emptyValue)
result = emptyValue;
for i = numel(objects) : -1 : 1
result(i) = fun(objects(i));
end
end
function result = where_(objects, filterFun)
keep = true(1, length(objects));
for iObject = 1:length(objects)
keep(iObject) = filterFun(objects(iObject));
end
result = objects(keep);
end
function result = notFoundException(obj)
result = MException("Path:NotFound", "Path ""%s"" not found. ", obj.string);
if ~obj.hasParent || obj.parent.exists
return; end
dir_ = obj;
while true
if ~dir_.hasParent || dir_.parent.exists
causeException = dir_.notFoundException;
result = Path.extendError(causeException, missing, "%s", result.message);
return
end
dir_ = dir_.parent;
end
end
function copyOrMove(objects, targets, copy, toDirMode)
if objects.count == 1 && copy
objects = repmat(objects, 1, length(targets));
end
if targets.count == 1 && toDirMode
targets = repmat(targets, 1, length(objects));
end
if objects.count ~= length(targets)
error("Path:copyOrMove:InvalidNumberOfTargets", "Number of target paths must be equal the number of source paths.")
end
for i = 1 : objects.count
obj = objects(i);
obj.mustExist;
if toDirMode
target = targets(i) / obj.name;
else
target = targets(i);
end
if obj.isFile && target.isDir
error("Path:copy:TargetIsDir", "The source ""%s"" is a file but the target ""%s"" is an existing directory.", target)
end
try
target.parent.mkdir;
if copy
copyfile(obj.string, target.string);
else
movefile(obj.string, target.string);
end
catch exception
operationName = ifThenElse(copy, "copy", "move");
Path.extendError(exception, ["MATLAB:COPYFILE:", "MATLAB:MOVEFILE:", "MATLAB:MKDIR:"], "Unable to %s %s ""%s"" to ""%s"".", operationName, lower(class(obj)), obj, target);
end
end
end
end
methods (Access = protected)
function displayScalarObject(obj)
fprintf(" %s(""%s"")\n\n", class(obj), obj.string);
end
function displayNonScalarObject(objects)
fprintf(" %s <a href=""matlab:Path.help"">%s</a> array\n\n", matlab.mixin.CustomDisplay.convertDimensionsToString(objects), class(objects));
if isempty(objects)
return; end
for obj = objects
fprintf(" %s(""%s"")\n", class(obj), obj.string);
end
fprintf("\n");
end
function displayEmptyObject(obj)
obj.displayNonScalarObject;
end
end
methods (Static, Access = private)
function result = clean(varargin)
fs = Path.FILE_SEPARATOR_REGEX;
paths = [varargin{:}];
result = paths;
if isempty(paths)
return
end
paths = paths.join(pathsep).split(pathsep);
for i = 1 : length(paths)
s = paths(i);
s = s.strip;
% Replace / and \ with correct separator.
s = s.replace(["\", "/"], filesep);
% Remove repeating separators.
if Path.IS_WINDOWS
s = regexprep(s, "(?<!^)" + fs + "+", fs);
else
s = regexprep(s, fs + "+", fs);
end
% Remove current-directory-dots.
s = regexprep(s, ["(?<=(^|"+fs+"))(\."+fs+")", "("+fs+"\.)$"], "");
% Resolve dir-up-dots.
expression = "("+fs+"|^)[^"+fs+":]+(?<!\.\.)"+fs+"\.\."; % Directory name followed by dir-up dots.
while ~isempty(regexp(s, expression, 'once'))
s = regexprep(s, expression, "");
end
% Remove leading and trailing separators.
if Path.IS_WINDOWS
expression = ["^"+fs+"(?!"+fs+")", fs+"+$"];
else
expression = fs+"+$";
end
s = regexprep(s, expression, "");
if s == ""
s = ".";
end
result(i) = s;
end
end
function exception = extendError(exception, identifiers, messageFormat, messageArguments)
arguments
exception
identifiers
messageFormat (1, 1) string
end
arguments (Repeating)
messageArguments
end
if (isscalar(identifiers) && ismissing(identifiers)) || any(startsWith(exception.identifier, identifiers))
messageFormat = messageFormat + "\nCause: %s";
messageArguments{end+1} = exception.message;
message = sprintf(messageFormat, messageArguments{:});
exception = MException(exception.identifier, "%s", message);
if nargout == 0
throwAsCaller(exception);
end
else
exception.rethrow;
end
end
function result = matches(s, patterns, mode)
pattern = "^(" + regexptranslate("wildcard", patterns).join("|") + ")$";
indices = regexp(s, pattern, "once", "emptymatch");
if isscalar(s)
result = isempty(indices);
else
result = cellfun(@isempty, indices);
end
if mode
result = ~result;
end
end
%% Validator functions
function mustBeEqualSizeOrScalar(value, objects)
if ~isscalar(value) && ~isequal(numel(value), numel(objects))
throwAsCaller(MException("Path:Validation:InvalidSize", "Value must be scalar or size must equal size of the object array."));
end
end
function mustBeValidName(values)
if any(ismissing(values)) || any(values.contains(["\", "/", pathsep]))
throwAsCaller(MException("Path:Validation:InvalidName", "Value must be a valid file name."));
end
end
function mustNotContainPathSeparator(values)
if any(values.contains(pathsep))
throwAsCaller(MException("Path:Validation:ContainsPathsep", "Value must not contain a path separator character."));
end
end
function mustBeNonmissing(values)
if any(ismissing(values))
throwAsCaller(MException("Path:Validation:InvalidName", "Value must be non-missing."));
end
end
function mustBeValidExtension(values)
if any(values.contains(["\", "/", pathsep]))
throwAsCaller(MException("Path:Validation:InvalidExtension", "Value must be a valid extension."));
end
end
function mustBeValidVariableName(values)
if any(arrayfun(@(x) ~isvarname(x), values))
throwAsCaller(MException("Path:Validation:InvalidVariableName", "Value must be a valid variable name."));
end
end
end
%% Deprecated
methods (Static)
function result = ofCaller(level)
arguments
level (1, 1) double {mustBeInteger, mustBePositive} = 1
end
warning("Path:deprecated", "Method 'Path.ofCaller' is deprecated. User 'Path.this' instead.");
result = Path.this(level + 1);
end
end
end
function result = ifThenElse(condition, a, b)
if condition
result = a;
else
result = b;
end
end
function tryToClose(fileId)
try
fclose(fileId);
catch
end
end
function result = listDeepPaths(dir_, fileMode)
result = strings(0);
dirContents = dir(dir_)';
for dirContent = dirContents
path = dir_ + filesep + dirContent.name;
if dirContent.isdir
if ismember(dirContent.name, [".", ".."])
continue; end
if ~fileMode
result(end+1) = path; end
result = [result, listDeepPaths(path, fileMode)];
elseif fileMode
result(end+1) = path;
end
end
end
function result = deal_(paths, outputCount)
if outputCount > 1
try
[result{1:outputCount}] = paths.deal;
catch exception
if exception.identifier == "Path:deal:InvalidNumberOfOutputs"
throwAsCaller(exception)
end
rethrow(exception);
end
else
result{1} = paths;
end
end