Skip to content

Commit

Permalink
simplify API
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Nov 25, 2024
1 parent ba53946 commit a09f846
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 26 deletions.
8 changes: 4 additions & 4 deletions +stdlib/absolute.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
function c = absolute(p, base, expand_tilde, use_java)
arguments
p (1,1) string
base string {mustBeScalarOrEmpty} = string.empty
base (1,1) string = ""
expand_tilde (1,1) logical = true
use_java (1,1) logical = false
end

cwd = stdlib.posix(pwd());

if (isempty(p) || strlength(p) == 0) && (isempty(base) || strlength(base) == 0)
if strlength(p) == 0 && strlength(base) == 0
c = cwd;
return
end
Expand All @@ -42,10 +42,10 @@
else
% .getAbsolutePath(), .toAbsolutePath()
% default is Documents/Matlab, which is probably not wanted.
if isempty(base) || strlength(base) == 0
if strlength(base) == 0
c = cwd + "/" + c;
else
d = stdlib.absolute(base, string.empty, expand_tilde, use_java);
d = stdlib.absolute(base, "", expand_tilde, use_java);
if isempty(c) || strlength(c) == 0
c = d;
else
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/h5save.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function h5save(filename, varname, A, opts)
varname (1,1) string {mustBeNonzeroLengthText}
A {mustBeNonempty}
opts.size (1,:) double {mustBeInteger,mustBeNonnegative} = []
opts.type string {mustBeScalarOrEmpty} = string.empty
opts.type (1,1) string = ""
end

if isnumeric(A)
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/h5variables.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
function names = h5variables(file, group)
arguments
file (1,1) string {mustBeFile}
group string {mustBeScalarOrEmpty} = string.empty
group (1,1) string = ""
end

if isempty(group) || strlength(group) == 0
if strlength(group) == 0
finf = h5info(file);
else
finf = h5info(file, group);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_readable.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if use_java
% java is about 10x slower than fileattrib
% needs absolute()
file = stdlib.absolute(file, string.empty, false, use_java);
file = stdlib.absolute(file, "", false, use_java);

ok = java.nio.file.Files.isReadable(java.io.File(file).toPath());
else
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_regular_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
opt = java.nio.file.LinkOption.values;

% needs absolute()
p = stdlib.absolute(p, string.empty, false, true);
p = stdlib.absolute(p, "", false, true);

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

Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if isMATLABReleaseOlderThan("R2024b")
% must be absolute path
% NOT .canonical or symlink is gobbled!
p = stdlib.absolute(p, string.empty, false, true);
p = stdlib.absolute(p, "", false, true);
ok = java.nio.file.Files.isSymbolicLink(java.io.File(p).toPath());
else
ok = isSymbolicLink(p);
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/is_writable.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
if use_java
% java is about 10x slower than fileattrib
% needs absolute()
file = stdlib.absolute(file, string.empty, false, use_java);
file = stdlib.absolute(file, "", false, use_java);

ok = java.nio.file.Files.isWritable(java.io.File(file).toPath());
else
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/ncsave.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function ncsave(filename, varname, A, opts)
varname (1,1) string {mustBeNonzeroLengthText}
A {mustBeNonempty}
opts.dims cell = {}
opts.type string {mustBeScalarOrEmpty} = string.empty
opts.type (1,1) string = ""
end

if isnumeric(A)
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/ncvariables.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
function names = ncvariables(file, group)
arguments
file (1,1) string {mustBeFile}
group string {mustBeScalarOrEmpty} = string.empty
group (1,1) string = ""
end

if isempty(group) || strlength(group) == 0
if strlength(group) == 0
finf = ncinfo(file);
else
finf = ncinfo(file, group);
Expand Down
4 changes: 2 additions & 2 deletions +stdlib/private/coerce_ds.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
% used by h5save and ncsave
arguments
A
dtype string {mustBeScalarOrEmpty}
dtype (1,1) string
end

if ischar(A)
A = string(A);
return
end

if isempty(dtype)
if strlength(dtype) == 0
return
end

Expand Down
2 changes: 1 addition & 1 deletion +stdlib/read_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

% must be absolute path
% must not be .canonical or symlink is gobbled!
r = stdlib.absolute(p, string.empty, false, true);
r = stdlib.absolute(p, "", false, true);

% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#readSymbolicLink(java.nio.file.Path)
t = java.nio.file.Files.readSymbolicLink(java.io.File(r).toPath());
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/resolve.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
use_java (1,1) logical = false
end

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

end
4 changes: 2 additions & 2 deletions +stdlib/samepath.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@

if use_java
% needs absolute
path1 = stdlib.absolute(path1, string.empty, false, true);
path2 = stdlib.absolute(path2, string.empty, false, true);
path1 = stdlib.absolute(path1, "", false, true);
path2 = stdlib.absolute(path2, "", false, true);

issame = java.nio.file.Files.isSameFile(...
java.io.File(path1).toPath(), ...
Expand Down
12 changes: 6 additions & 6 deletions +stdlib/subprocess_run.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
function [status, stdout, stderr] = subprocess_run(cmd, opt)
arguments
cmd (1,:) string
opt.env struct {mustBeScalarOrEmpty} = struct.empty
opt.cwd string {mustBeScalarOrEmpty} = string.empty
opt.stdin string {mustBeScalarOrEmpty} = string.empty
opt.env (1,1) struct = struct()
opt.cwd (1,1) string = ""
opt.stdin (1,1) string = ""
end

%% process instantiation
proc = java.lang.ProcessBuilder("");

if ~isempty(opt.env)
if ~isempty(fieldnames(opt.env))
% requires Parallel Computing Toolbox
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html#environment()
env = proc.environment();
Expand All @@ -46,7 +46,7 @@
end
end

if ~isempty(opt.cwd)
if strlength(opt.cwd) > 0
mustBeFolder(opt.cwd)
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ProcessBuilder.html#directory(java.io.File)
proc.directory(java.io.File(opt.cwd));
Expand All @@ -59,7 +59,7 @@
h = proc.start();

%% stdin pipe
if ~isempty(opt.stdin)
if strlength(opt.stdin) > 0
writer = java.io.BufferedWriter(java.io.OutputStreamWriter(h.getOutputStream()));
writer.write(opt.stdin);
writer.flush()
Expand Down

0 comments on commit a09f846

Please sign in to comment.