Skip to content

Commit 16ed8f5

Browse files
committed
add homedir() fcn, use java user.home with envvar fallback
1 parent 3d5cca6 commit 16ed8f5

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

+stdlib/+fileio/expanduser.m

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
expanded = p;
88

9-
if ispc
10-
home = getenv('USERPROFILE');
11-
else
12-
home = getenv('HOME');
13-
end
9+
home = stdlib.fileio.homedir();
1410

1511
if ~isempty(home)
1612
i = startsWith(expanded, "~");

+stdlib/+fileio/homedir.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function home = homedir()
2+
3+
persistent h;
4+
5+
if ~isempty(h)
6+
home = h;
7+
return
8+
end
9+
10+
try
11+
home = string(java.lang.System.getProperty("user.home"));
12+
catch excp
13+
% if Java not available use env vars, else error
14+
if excp.identifier ~= "MATLAB:undefinedVarOrClass"
15+
error("stdlib:fileio:homedir", excp.message)
16+
end
17+
18+
if ispc
19+
home = getenv('USERPROFILE');
20+
else
21+
home = getenv('HOME');
22+
end
23+
end
24+
25+
h = home;
26+
27+
end

0 commit comments

Comments
 (0)