Skip to content

Commit

Permalink
generalize
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Dec 12, 2024
1 parent 50eda9c commit 9fbde32
Show file tree
Hide file tree
Showing 37 changed files with 174 additions and 62 deletions.
2 changes: 2 additions & 0 deletions +stdlib/absolute.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

try %#ok<TRYNC>
cwd = string(cwd);
p = string(p);
base = string(base);
end

cwd = stdlib.posix(cwd);
Expand Down
13 changes: 9 additions & 4 deletions +stdlib/auto_chunk_size.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
% * dims: proposed dataset dimensions (like size())

function csize = auto_chunk_size(dims)
arguments
dims (1,:) {mustBeInteger,mustBePositive}
end
% arguments
% dims (1,:) {mustBeInteger,mustBePositive}
% end

assert(isvector(dims), 'dims must be a vector')
mustBePositive(dims)

CHUNK_BASE = 16000; % Multiplier by which chunks are adjusted
CHUNK_MIN = 8000; % lower limit: 8 kbyte
Expand Down Expand Up @@ -55,4 +58,6 @@
i = i+1;
end

end % function
end

%!assert(auto_chunk_size([15,250,100]), [2,32,25])
10 changes: 6 additions & 4 deletions +stdlib/checkRAM.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
% certainly CAN'T create an array without digging deep into swap or worse.

function [OK,newSizeBytes,freebytes] = checkRAM(newSize, myclass)
arguments
newSize (1,:) {mustBeNumeric}
myclass (1,1) string = "double"
end
% arguments
% newSize (1,:) {mustBeNumeric}
% myclass (1,1) string
% end

% get available RAM
freebytes = stdlib.ram_free();
Expand All @@ -29,3 +29,5 @@
OK = newSizeBytes < freebytes;

end

%!assert(checkRAM([15,2,1], 'double'), true)
4 changes: 4 additions & 0 deletions +stdlib/exists.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%% EXISTS does path exist
%
% NOTE: in general on Windows existS("not-exist/..") is true, but on
% Unix it is false.
% In C/C++ access() or stat() the same behavior is observed Windows vs Unix.
%
%%% Inputs
% * p: path to check
%%% Outputs
Expand Down
9 changes: 3 additions & 6 deletions +stdlib/expanduser.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,11 @@
end

if ischar(e)
if e(1) ~= '~' || (L > 1 && e(1) == '~' && e(2) ~= '/')
return
end
ng = e(1) ~= '~' || (L > 1 && e(1) == '~' && e(2) ~= '/');
else
if ~startsWith(e, "~") || (L > 1 && ~startsWith(e, "~/"))
return
end
ng = ~startsWith(e, "~") || (L > 1 && ~startsWith(e, "~/"));
end
if ng, return, end

home = stdlib.homedir(use_java);

Expand Down
2 changes: 2 additions & 0 deletions +stdlib/extract_zstd.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ function extract_zstd_bin(archive, out_dir)
untar(tar_arc, out_dir)
delete(tar_arc)
end

%!testif 0
2 changes: 2 additions & 0 deletions +stdlib/h4exists.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
rethrow(e)
end
end

%!testif 0
2 changes: 2 additions & 0 deletions +stdlib/h4size.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@
fsize = cell2mat({sds(i).Dims.Size});

end

%!testif 0
2 changes: 2 additions & 0 deletions +stdlib/h4variables.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
names = string({ds.Name});

end

%!testif 0
3 changes: 3 additions & 0 deletions +stdlib/h5create_group.m
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@
end

end


%!testif 0
5 changes: 4 additions & 1 deletion +stdlib/h5save.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ function h5save(filename, varname, A, opts)
stdlib.h5save_new(filename, varname, A, opts.size, opts.compressLevel)
end

end % function
end


%!testif 0
20 changes: 14 additions & 6 deletions +stdlib/handle2filename.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
%% HANDLE2FILENAME Convert a file handle to a filename

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

mustBeInteger(fileHandle)

n = "";

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

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

end

%!assert(handle2filename(0), "stdin")
4 changes: 3 additions & 1 deletion +stdlib/hard_link_count.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@

end

%!assert(hard_link_count('hard_link_count.m') >= 1)
%!test
%! if ispc, return; end
%! assert(hard_link_count('hard_link_count.m') >= 1)
5 changes: 3 additions & 2 deletions +stdlib/has_wsl.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
persistent wsl;

if isempty(wsl)
wsl = false;
if ispc
[stat, ~] = system("wsl test 1");
wsl = stat == 0;
else
wsl = false;
end
end

ok = wsl;

end

%!assert(islogical(has_wsl()))
4 changes: 3 additions & 1 deletion +stdlib/ini2struct.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

fclose(f);

end % function
end


function i = is_comment(line)
Expand All @@ -59,6 +59,8 @@

end

%!testif 0

% Copyright (c) 2014, freeb
% Copyright (c) 2008, Andriy Nych
% Copyright (c) 2009-2010, Evgeny Prilepin aka Iroln
Expand Down
1 change: 1 addition & 0 deletions +stdlib/is_absolute.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
% https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/io/File.html#isAbsolute()
isabs = java.io.File(p).toPath().isAbsolute();
else
p = string(p);
L = strlength(p);
if ispc
s = "";
Expand Down
2 changes: 2 additions & 0 deletions +stdlib/is_parallel_worker.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
end

end

%!testif 0
22 changes: 15 additions & 7 deletions +stdlib/is_regular_file.m
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
%% IS_REGULAR_FILE check if path is a regular file

function isreg = is_regular_file(p)
arguments
p (1,1) string
end

opt = java.nio.file.LinkOption.values;
function r = is_regular_file(p)
% arguments
% p (1,1) string
% end

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

isreg = java.nio.file.Files.isRegularFile(java.io.File(p).toPath(), opt);
if stdlib.isoctave()
opt = javaMethod("values", "java.nio.file.LinkOption");
op = javaObject("java.io.File", p).toPath();
r = javaMethod("isRegularFile", "java.nio.file.Files", op, opt);
else
opt = java.nio.file.LinkOption.values;
op = java.io.File(p).toPath();
r = java.nio.file.Files.isRegularFile(op, opt);
end

end

%!assert(is_regular_file('is_regular_file.m'))
17 changes: 13 additions & 4 deletions +stdlib/is_symlink.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html#isSymbolicLink(java.nio.file.Path)

function ok = is_symlink(p)
arguments
p (1,1) string
end
% arguments
% p (1,1) string
% end

if isMATLABReleaseOlderThan("R2024b")
if stdlib.isoctave()
p = stdlib.absolute(p, "", false, true);
op = javaObject("java.io.File", p).toPath();
ok = javaMethod("isSymbolicLink", "java.nio.file.Files", op);
elseif isMATLABReleaseOlderThan("R2024b")
% must be absolute path
% NOT .canonical or symlink is gobbled!
p = stdlib.absolute(p, "", false, true);
Expand All @@ -18,3 +22,8 @@
end

end

%!test
%! p = tempname();
%! assert(create_symlink(mfilename("fullpath"), p))
%! assert(is_symlink(p))
10 changes: 6 additions & 4 deletions +stdlib/is_wsl_path.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
% Ref: https://learn.microsoft.com/en-us/windows/wsl/filesystems

function iswsl = is_wsl_path(p)
arguments
p (1,1) string
end
% arguments
% p (1,1) string
% end

if ispc
iswsl = any(startsWith(p, ["\\wsl$", "\\wsl.localhost"]));
iswsl = strncmp(p, "\\wsl$", 6) || strncmp(p, "\\wsl.localhost", 15);
else
iswsl = false;
end

end

%!assert(!is_wsl_path("C:/"))
8 changes: 5 additions & 3 deletions +stdlib/iswsl.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% Detects if Matlab or GNU Octave is installed and running from within
% Windows Subsystem for Linux

function yeswsl = iswsl()
function w = iswsl()

persistent wsl;

Expand All @@ -13,11 +13,13 @@
if fid >= 1
v = fscanf(fid,'%s');
fclose(fid);
wsl = contains(v, 'microsoft-standard');
wsl = ~isempty(strfind(v, 'microsoft-standard')); %#ok<*STREMP>
end
end
end

yeswsl=wsl; % has to be a separate line/variable for matlab
w = wsl; % has to be a separate line/variable for matlab

end

%!assert(islogical(iswsl()))
2 changes: 2 additions & 0 deletions +stdlib/join.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

end

p = stdlib.posix(p);

end

%!assert(join("", ""), "")
Expand Down
2 changes: 2 additions & 0 deletions +stdlib/len.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
end

end

%!assert(len('abc'), 3)
2 changes: 2 additions & 0 deletions +stdlib/ncsave.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ function ncsave(filename, varname, A, opts)
end

end

%!testif 0
2 changes: 1 addition & 1 deletion +stdlib/normalize.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
n = stdlib.posix(java.io.File(p).toPath().normalize().toString());
else

n = stdlib.posix(p);
n = stdlib.posix(string(p));

% use split to remove /../ and /./ and duplicated /
parts = split(n, '/');
Expand Down
2 changes: 2 additions & 0 deletions +stdlib/null_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
end

end

%!assert(ischar(null_file()))
2 changes: 1 addition & 1 deletion +stdlib/resolve.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

end

%!assert (resolve(''), pwd())
%!assert (resolve(''), stdlib.posix(pwd()))
6 changes: 5 additions & 1 deletion +stdlib/samepath.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
% true if inputs resolve to same path.
% Both paths must exist.
%
% NOTE: in general on Windows same(".", "not-exist/..") is true, but on
% Unix it is false.
% In C/C++ access() or stat() the same behavior is observed Windows vs Unix.
%
%%% Inputs
% * path1, path2: paths to compare
%%% Outputs
Expand All @@ -25,4 +29,4 @@

%!assert(samepath(".", "."))
%!assert(samepath(".", "./"))
%!assert(!samepath(".", "not-exist/.."))
%!assert(!samepath("not-exist", "not-exist/.."))
2 changes: 2 additions & 0 deletions +stdlib/subprocess_run.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@
reader.close()

end

%!testif 0
2 changes: 2 additions & 0 deletions +stdlib/which.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@
exe = string.empty;

end

%!testif 0
2 changes: 2 additions & 0 deletions +stdlib/windows_shortname.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
delete(fso);

end

%!testif 0
Loading

0 comments on commit 9fbde32

Please sign in to comment.