Skip to content

Commit 8945e7c

Browse files
committed
add join() posix throughout
1 parent 7fba4ee commit 8945e7c

16 files changed

+45
-30
lines changed

+stdlib/+fileio/absolute_path.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818

1919
if ~stdlib.fileio.is_absolute_path(abspath)
2020
% .toAbsolutePath() default is Documents/Matlab, which is probably not wanted.
21-
abspath = fullfile(pwd, abspath);
21+
abspath = stdlib.fileio.join(pwd, abspath);
2222
end
2323

24-
abspath = string(java.io.File(abspath).getAbsolutePath());
24+
abspath = stdlib.posix(java.io.File(abspath).getAbsolutePath());
2525

2626
end % function

+stdlib/+fileio/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
if ~stdlib.fileio.is_absolute_path(c)
2020
if isfile(c) || isfolder(c)
2121
% workaround Java/Matlab limitations
22-
c = fullfile(pwd, c);
22+
c = stdlib.fileio.join(pwd, c);
2323
else
2424
% for non-existing path, return normalized relative path
2525
% like C++ filesystem weakly_canonical()

+stdlib/+fileio/expanduser.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
home = stdlib.fileio.homedir();
1414

1515
if ~isempty(home)
16-
e = fullfile(home, extractAfter(e, 1));
16+
e = stdlib.fileio.join(home, extractAfter(e, 1));
1717
end
1818

1919
end %function

+stdlib/+fileio/join.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function p = join(a, b)
2+
%% JOIN: join two paths with posix file separator
3+
arguments
4+
a string {mustBeScalarOrEmpty}
5+
b string {mustBeScalarOrEmpty}
6+
end
7+
8+
p = stdlib.fileio.posix(fullfile(a, b));
9+
10+
end

+stdlib/+fileio/posix.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
function ppath = posix(file)
1+
function p = posix(p)
22

33
arguments
4-
file string
4+
p string
55
end
66

7-
8-
ppath = fullfile(file);
9-
107
if ispc
11-
ppath = strrep(ppath, "\", "/");
8+
p = strrep(p, "\", "/");
129
end
1310

1411
end

+stdlib/+fileio/resolve.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
if ~stdlib.fileio.is_absolute_path(c)
2121
% .toAbsolutePath() default is Documents/Matlab, which is probably not wanted.
22-
c = fullfile(pwd, c);
22+
c = stdlib.fileio.join(pwd, c);
2323
end
2424

2525
% similar benchmark time as java method

+stdlib/+fileio/which.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
for name = names
4242

4343
for p = fpath
44-
exe = fullfile(p, name);
44+
exe = stdlib.fileio.join(p, name);
4545
if stdlib.fileio.is_exe(exe)
4646
return
4747
end

+stdlib/+fileio/with_suffix.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
function filename = with_suffix(filename, suffix)
22

33
arguments
4-
filename string
4+
filename string {mustBeScalarOrEmpty}
55
suffix (1,1) string
66
end
77

88
[direc, name, ext] = fileparts(filename);
99
if ext ~= suffix
10-
filename = fullfile(direc, name + suffix);
10+
filename = stdlib.fileio.join(direc, name + suffix);
1111
end
1212

1313
end

+stdlib/join.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function p = join(a, b)
2+
%% JOIN: join two paths with posix file separator
3+
arguments
4+
a string {mustBeScalarOrEmpty}
5+
b string {mustBeScalarOrEmpty}
6+
end
7+
8+
p = stdlib.fileio.join(a, b);
9+
10+
end

test/TestFileImpure.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function test_expanduser(tc)
2525
tc.verifyEqual(stdlib.expanduser("~"), h)
2626

2727
e = stdlib.expanduser("~/foo");
28-
tc.verifyEqual(e, stdlib.posix(fullfile(h, "foo")))
28+
tc.verifyEqual(e, stdlib.fileio.join(h, "foo"))
2929

3030
end
3131

@@ -54,8 +54,7 @@ function test_canonical(tc)
5454

5555
% test existing file
5656
r = stdlib.parent(mfilename('fullpath'));
57-
rp = fullfile(r, "..");
58-
tc.verifyEqual(stdlib.canonical(rp), stdlib.parent(r))
57+
tc.verifyEqual(stdlib.canonical(fullfile(r, "..")), stdlib.parent(r))
5958

6059
h = stdlib.fileio.homedir;
6160
tc.verifyEqual(stdlib.canonical("~"), h)
@@ -105,16 +104,16 @@ function test_resolve(tc)
105104

106105
% test existing file
107106
r = stdlib.parent(mfilename('fullpath'));
108-
rp = fullfile(r, "..");
107+
rp = stdlib.parent(r);
109108
tc.verifyEqual(stdlib.resolve(rp), stdlib.parent(r))
110109

111110
h = stdlib.fileio.homedir;
112111
tc.verifyEqual(stdlib.resolve("~"), h)
113112
tc.verifyEqual(stdlib.resolve("~/"), h)
114113
tc.verifyEqual(stdlib.resolve("~/.."), stdlib.parent(h))
115114

116-
tc.verifyEqual(stdlib.resolve("nobody.txt"), fullfile(td, "nobody.txt"))
117-
tc.verifyEqual(stdlib.resolve("../nobody.txt"), fullfile(stdlib.parent(td), "nobody.txt"))
115+
tc.verifyEqual(stdlib.resolve("nobody.txt"), stdlib.join(td, "nobody.txt"))
116+
tc.verifyEqual(stdlib.resolve("../nobody.txt"), stdlib.join(stdlib.parent(td), "nobody.txt"))
118117

119118
end
120119

0 commit comments

Comments
 (0)