Skip to content

Commit

Permalink
allow older Matlab and GNU Octave to work
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Dec 11, 2024
1 parent b7c7364 commit fb13dbf
Show file tree
Hide file tree
Showing 60 changed files with 421 additions and 334 deletions.
23 changes: 14 additions & 9 deletions +stdlib/absolute.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@
% non-existant path is made absolute relative to pwd

function c = absolute(p, base, expand_tilde, use_java)
arguments
p (1,1) string
base (1,1) string = ""
expand_tilde (1,1) logical = true
use_java (1,1) logical = false
end
% arguments
% p (1,1) string
% base (1,1) string = ""
% expand_tilde (1,1) logical = true
% use_java (1,1) logical = false
% end
if nargin < 2, base = ""; end
if nargin < 3, expand_tilde = true; end
if nargin < 4, use_java = false; end

c = pwd();

cwd = stdlib.posix(pwd());
cwd = stdlib.posix(c);

Lb = stdlib.len(base);

Expand Down Expand Up @@ -70,5 +75,5 @@
end


%!assert(absolute('', '', 0,0), posix(pwd))
%!assert(absolute('a/b', '', 0,0), posix(fullfile(pwd, 'a/b')))
%!assert(absolute('', ''), posix(pwd))
%!assert(absolute('a/b', ''), posix(fullfile(pwd, 'a/b')))
18 changes: 10 additions & 8 deletions +stdlib/canonical.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getCanonicalPath()

function c = canonical(p, expand_tilde, use_java)
arguments
p (1,1) string
expand_tilde (1,1) logical = true
use_java (1,1) logical = false
end
% arguments
% p (1,1) string
% expand_tilde (1,1) logical = true
% use_java (1,1) logical = false
% end
if nargin < 2, expand_tilde = true; end
if nargin < 3, use_java = false; end

if expand_tilde
c = stdlib.expanduser(p, use_java);
Expand Down Expand Up @@ -59,6 +61,6 @@

end

%!assert(canonical("", 0,0), "")
%!assert(canonical("~",1,0), homedir())
%!assert(canonical("a/b/..",0,0), "a")
%!assert(canonical(""), "")
%!assert(canonical("~"), homedir())
%!assert(canonical("a/b/.."), "a")
13 changes: 6 additions & 7 deletions +stdlib/create_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#createSymbolicLink(java.nio.file.Path,java.nio.file.Path,java.nio.file.attribute.FileAttribute...)

function ok = create_symlink(target, link)

arguments
target (1,1) string
link (1,1) string
end
% arguments
% target (1,1) string
% link (1,1) string
% end

if stdlib.isoctave()

[err, msg] = symlink(target, link);
ok = err == 0;
if ~ok
warning("symlink: %s", msg)
warning("create_symlink: %s", msg)
end

elseif ispc || isMATLABReleaseOlderThan("R2024b")
Expand Down Expand Up @@ -61,5 +60,5 @@

%!test
%! if !ispc
%! create_symlink(tempname, tempname)
%! assert(create_symlink(tempname, tempname))
%! endif
6 changes: 3 additions & 3 deletions +stdlib/diskfree.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
% Ref: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getUsableSpace()

function f = diskfree(d)
arguments
d (1,1) string {mustBeFolder}
end
% arguments
% d (1,1) string
% end

if stdlib.isoctave()
o = javaObject("java.io.File", d);
Expand Down
8 changes: 4 additions & 4 deletions +stdlib/drop_slash.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
%% DROP_SLASH drop repeated and trailing slash

function d = drop_slash(p)
arguments
p (1,1) string
end
% arguments
% p (1,1) string
% end

s = stdlib.posix(p);

Expand All @@ -12,7 +12,7 @@

L = stdlib.len(d);

if d == '/' || ~L
if strcmp(d, '/') || ~L
return;
end

Expand Down
10 changes: 5 additions & 5 deletions +stdlib/exists.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
%

function ok = exists(p, use_java)
arguments
p (1,1) string
use_java (1,1) logical = false
end

% arguments
% p (1,1) string
% use_java (1,1) logical = false
% end
if nargin < 2, use_java = false; end

if stdlib.isoctave()
ok = javaObject("java.io.File", p).exists();
Expand Down
21 changes: 11 additions & 10 deletions +stdlib/expanduser.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
% * e: expanded path

function e = expanduser(p, use_java)
arguments
p (1,1) string
use_java (1,1) logical = false
end
% arguments
% p (1,1) string
% use_java (1,1) logical = false
% end
if nargin < 2, use_java = false; end

e = stdlib.drop_slash(p);

Expand Down Expand Up @@ -49,9 +50,9 @@
end


%!assert(expanduser('',0), '')
%!assert(expanduser("~",0), homedir())
%!assert(expanduser("~/",0), homedir())
%!assert(expanduser("~user",0), "~user")
%!assert(expanduser("~user/",0), "~user")
%!assert(expanduser("~///c",0), strcat(homedir(), "/c"))
%!assert(expanduser(''), '')
%!assert(expanduser("~"), homedir())
%!assert(expanduser("~/"), homedir())
%!assert(expanduser("~user"), "~user")
%!assert(expanduser("~user/"), "~user")
%!assert(expanduser("~///c"), strcat(homedir(), "/c"))
9 changes: 5 additions & 4 deletions +stdlib/file_size.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
% FILE_SIZE size in bytes of file

function s = file_size(p, use_java)
arguments
p (1,1) string {mustBeFile}
use_java (1,1) logical = false
end
% arguments
% p (1,1) string {mustBeFile}
% use_java (1,1) logical = false
% end
if nargin < 2, use_java = false; end

if stdlib.isoctave()
s = javaObject("java.io.File", p).length();
Expand Down
6 changes: 3 additions & 3 deletions +stdlib/filename.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
% filename (including suffix) without directory

function f = filename(p)
arguments
p (1,1) string
end
% arguments
% p (1,1) string
% end

% NOT https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html#getName()
% because by our definition, a trailing directory component is not part of the filename
Expand Down
2 changes: 1 addition & 1 deletion +stdlib/get_pid.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

function pid = get_pid()

if stdlib.isoctave
if stdlib.isoctave()
pid = getpid();
else
pid = feature("getpid");
Expand Down
8 changes: 6 additions & 2 deletions +stdlib/get_shell.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
% return value of environment variable SHELL
% this is mostly relevant on unix-like systems

function shell = get_shell()
function s = get_shell()

shell = string(getenv("SHELL"));
s = getenv("SHELL");

try %#ok<TRYNC>
s = string(s);
end

end
13 changes: 7 additions & 6 deletions +stdlib/h5exists.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
% * exists: boolean

function exists = h5exists(file, variable)
arguments
file (1,1) string {mustBeFile}
variable (1,1) string
end
% arguments
% file (1,1) string {mustBeFile}
% variable (1,1) string
% end

exists = false;

Expand All @@ -19,7 +19,8 @@
exists = true;
catch e
if stdlib.isoctave
if ~strfind(e.message, 'does not exist')
disp(e.message)
if strcmp(e.identifier, "Octave:undefined-function") || isempty(strfind(e.message, 'does not exist'))
rethrow(e)
end
else
Expand All @@ -35,7 +36,7 @@
%! pkg load hdf5oct
%! fn = 'test_h5exists.h5';
%! ds = '/a';
%! delete(fn)
%! if isfile(fn), delete(fn); end
%! h5create(fn, ds, [1])
%! assert(h5exists(fn, ds))
%! delete(fn)
3 changes: 2 additions & 1 deletion +stdlib/h5save_exist.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ function h5save_exist(filename, varname, A, sizeA)
end

%!test
%! pkg load hdf5oct
%! fn = 'test_h5save_exist.h5';
%! ds = '/a';
%! a = [1,2];
%! b = [3,4];
%! delete(fn)
%! if isfile(fn), delete(fn); end
%! h5save_new(fn, ds, a, size(a), 0)
%! h5save_exist(fn, ds, b, size(b))
%! assert(h5read(fn, ds), b)
Expand Down
3 changes: 2 additions & 1 deletion +stdlib/h5save_new.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ function h5save_new(filename, varname, A, sizeA, compressLevel)
end

%!test
%! pkg load hdf5oct
%! fn = 'test_h5save_new.h5';
%! ds = '/a';
%! a = [1,2];
%! delete(fn)
%! if isfile(fn), delete(fn); end
%! h5save_new(fn, ds, a, size(a), 0)
%! assert(h5read(fn, ds), a)
%! delete(fn)
10 changes: 5 additions & 5 deletions +stdlib/h5size.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
% fsize: vector of variable size per dimension. Empty if scalar variable.

function fsize = h5size(file, variable)
arguments
file (1,1) string {mustBeFile}
variable (1,1) string {mustBeNonzeroLengthText}
end
% arguments
% file (1,1) string {mustBeFile}
% variable (1,1) string {mustBeNonzeroLengthText}
% end

dsi = h5info(file, variable).Dataspace;

Expand All @@ -28,7 +28,7 @@
%! fn = 'test_h5size.h5';
%! ds = '/a';
%! a = [1,2];
%! delete(fn)
%! if isfile(fn), delete(fn); end
%! h5save_new(fn, ds, a, size(a), 0)
%! assert(h5size(fn, ds), uint64([1,2]))
%! delete(fn)
15 changes: 8 additions & 7 deletions +stdlib/h5variables.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
% * names: variable names

function names = h5variables(file, group)
arguments
file (1,1) string {mustBeFile}
group (1,1) string = ""
end
% arguments
% file (1,1) string {mustBeFile}
% group (1,1) string = ""
% end
if nargin < 2, group = ""; end

if stdlib.len(group) == 0
if ~stdlib.len(group)
finf = h5info(file);
else
finf = h5info(file, group);
Expand All @@ -23,7 +24,7 @@
ds = finf.Datasets;

if ischar(file)
if isempty(ds) %#ok<UNRCH>
if isempty(ds)
names = [];
else
names = {ds.Name};
Expand All @@ -43,7 +44,7 @@
%! pkg load hdf5oct
%! fn = 'test_h5variables.h5';
%! ds = '/a';
%! delete(fn)
%! if isfile(fn), delete(fn); end
%! h5create(fn, ds, [1])
%! assert(h5variables(fn, ''), {'/a'})
%! delete(fn)
8 changes: 4 additions & 4 deletions +stdlib/handle2filename.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
%% HANDLE2FILENAME Convert a file handle to a filename

function name = handle2filename(fileHandle)
function n = handle2filename(fileHandle)
arguments
fileHandle (1,1) {mustBeInteger}
fileHandle (1,1) {mustBeInteger}
end

if fileHandle >= 0
name = stdlib.posix(fopen(fileHandle));
n = stdlib.posix(string(fopen(fileHandle)));
else
name = string.empty;
n = string.empty;
end

end
2 changes: 2 additions & 0 deletions +stdlib/has_java.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
ok = h;

end

%!assert(islogical(has_java()))
Loading

0 comments on commit fb13dbf

Please sign in to comment.