Skip to content

Commit aad9830

Browse files
committed
expanduser,homedir,absolute: optional java
1 parent 94f42ba commit aad9830

File tree

11 files changed

+34
-29
lines changed

11 files changed

+34
-29
lines changed

+stdlib/absolute.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function c = absolute(p, base, expand_tilde)
1+
function c = absolute(p, base, expand_tilde, use_java)
22
%% absolute(p)
33
% path need not exist
44
% absolute path will be relative to pwd if not exist
@@ -15,10 +15,11 @@
1515
p (1,1) string
1616
base string {mustBeScalarOrEmpty} = string.empty
1717
expand_tilde (1,1) logical = true
18+
use_java (1,1) logical = false
1819
end
1920

2021
if expand_tilde
21-
c = stdlib.expanduser(p);
22+
c = stdlib.expanduser(p, use_java);
2223
else
2324
c = p;
2425
end
@@ -31,7 +32,7 @@
3132
if isempty(base) || strlength(base) == 0
3233
c = stdlib.join(pwd, c);
3334
else
34-
c = stdlib.join(stdlib.absolute(base), c);
35+
c = stdlib.join(stdlib.absolute(base, string.empty, expand_tilde, use_java), c);
3536
end
3637
end
3738

+stdlib/canonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
end
2323

2424
if expand_tilde
25-
c = stdlib.expanduser(p);
25+
c = stdlib.expanduser(p, use_java);
2626
else
2727
c = p;
2828
end

+stdlib/expanduser.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function e = expanduser(p)
1+
function e = expanduser(p, use_java)
22
%% expanduser(path)
33
% expands tilde ~ into user home directory
44
%
@@ -10,6 +10,7 @@
1010
% * expanded: expanded path
1111
arguments
1212
p (1,1) string
13+
use_java (1,1) logical = false
1314
end
1415

1516
e = p;
@@ -18,7 +19,7 @@
1819
return
1920
end
2021

21-
home = stdlib.homedir();
22+
home = stdlib.homedir(use_java);
2223

2324
if ~isempty(home)
2425
e = stdlib.join(home, extractAfter(e, 1));

+stdlib/homedir.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
function home = homedir()
1+
function home = homedir(use_java)
22
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#getProperty(java.lang.String)
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/System.html#getProperties()
4-
persistent h;
5-
6-
if ~isempty(h)
7-
home = h;
8-
return
4+
arguments
5+
use_java (1,1) logical = false
96
end
107

11-
home = stdlib.posix(java.lang.System.getProperty("user.home"));
8+
if(use_java)
9+
home = java.lang.System.getProperty("user.home");
10+
elseif ispc
11+
home = getenv("USERPROFILE");
12+
else
13+
home = getenv("HOME");
14+
end
1215

13-
h = home;
16+
home = stdlib.posix(home);
1417

1518
end

+stdlib/is_readable.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
function ok = is_readable(file, use_jvm)
1+
function ok = is_readable(file, use_java)
22
%% is_readable is file readable
33
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isReadable(java.nio.file.Path)
44

55
arguments
66
file (1,1) string
7-
use_jvm (1,1) logical = false
7+
use_java (1,1) logical = false
88
end
99

10-
if use_jvm
10+
if use_java
1111
% java is about 10x slower than fileattrib
1212
% needs absolute()
13-
file = stdlib.absolute(file);
13+
file = stdlib.absolute(file, string.empty, false, use_java);
1414

1515
ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
1616
else

+stdlib/is_regular_file.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
opt = java.nio.file.LinkOption.values;
77

88
% needs absolute()
9-
p = stdlib.absolute(p);
9+
p = stdlib.absolute(p, string.empty, false, true);
1010

1111
isreg = java.nio.file.Files.isRegularFile(java.io.File(p).toPath(), opt);
1212

13-
end
13+
end

+stdlib/is_symlink.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
if isMATLABReleaseOlderThan("R2024b")
1010
% must be absolute path
1111
% NOT .canonical or symlink is gobbled!
12-
p = stdlib.absolute(p);
12+
p = stdlib.absolute(p, string.empty, false, true);
1313
ok = java.nio.file.Files.isSymbolicLink(java.io.File(p).toPath());
1414
else
1515
ok = isSymbolicLink(p);

+stdlib/is_writable.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
function ok = is_writable(file, use_jvm)
1+
function ok = is_writable(file, use_java)
22
%% is_writable
33
% is path writable
44
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/nio/file/Files.html#isWritable(java.nio.file.Path)
55

66
arguments
77
file (1,1) string
8-
use_jvm (1,1) logical = false
8+
use_java (1,1) logical = false
99
end
1010

11-
if use_jvm
11+
if use_java
1212
% java is about 10x slower than fileattrib
1313
% needs absolute()
14-
file = stdlib.absolute(file);
14+
file = stdlib.absolute(file, string.empty, false, use_java);
1515

1616
ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
1717
else

+stdlib/read_symlink.m

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

1515
% must be absolute path
1616
% must not be .canonical or symlink is gobbled!
17-
r = stdlib.absolute(p);
17+
r = stdlib.absolute(p, string.empty, false, true);
1818

1919
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
2020
r = stdlib.posix(...

+stdlib/resolve.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
use_java (1,1) logical = false
1515
end
1616

17-
r = stdlib.canonical(stdlib.absolute(p, "", expand_tilde), false, use_java);
17+
r = stdlib.canonical(stdlib.absolute(p, string.empty, expand_tilde, use_java), false, use_java);
1818

1919
end

0 commit comments

Comments
 (0)