Skip to content

Commit

Permalink
generalize
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Dec 11, 2024
1 parent 50eda9c commit 11d27ca
Show file tree
Hide file tree
Showing 29 changed files with 142 additions and 60 deletions.
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)
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")
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
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/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: 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()))
13 changes: 8 additions & 5 deletions +stdlib/root_name.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
return
end

if ischar(p)
if p(2) == ':' && isletter(p(1))
r = p(1:2);
end
else
try
if extractBetween(p, 2, 2) == ":" && isletter(extractBetween(p, 1, 1))
r = extractBetween(p, 1, 2);
end
catch e
if ~strcmp(e.identifier, "Octave:undefined-function")
rethrow(e)
end
if p(2) == ':' && isletter(p(1))
r = p(1:2);
end
end

end
Expand Down
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
18 changes: 12 additions & 6 deletions +stdlib/winpath2wslpath.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
%% WINPATH2WSLPATH convert Windows path to WSL path
% input format like \\wsl$\Ubuntu\home\username\...
function wsl_path = winpath2wslpath(win_path)
arguments
win_path (1,1) string {mustBeNonzeroLengthText}
end

% arguments
% win_path (1,1) string
% end

assert(stdlib.has_wsl(), "stdlib:sys:winpath2wslpath:EnvironmentError", mfilename() + "only supported on Windows Matlab with WSL")
assert(stdlib.has_wsl(), "stdlib:sys:winpath2wslpath:EnvironmentError", "%s only supported on Windows Matlab with WSL", mfilename())

[stat, wsl_path] = system("wsl wslpath " + strrep(win_path, '\', '/'));

assert(stat == 0, "stdlib:sys:winpath2wslpath:IOError", "could not convert wslpath " + wsl_path)
assert(stat == 0, "stdlib:sys:winpath2wslpath:IOError", "could not convert wslpath %s", wsl_path)

try %#ok<TRYNC>
wsl_path = strip(wsl_path);
end

end

%!test
%! if stdlib.has_wsl()
%! assert(ischar(winpath2wslpath("c:/users/")))
%! endif
17 changes: 12 additions & 5 deletions +stdlib/wsl_tempfile.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
% Windows Subsystem for Linux (WSL) temporary file from Windows Matlab

function p = wsl_tempfile()
arguments (Output)
p (1,1) string {mustBeNonzeroLengthText}
end
% arguments (Output)
% p (1,1) string {mustBeNonzeroLengthText}
% end

assert(stdlib.has_wsl(), "stdlib:sys:wsl_tempfile:EnvironmentError", mfilename() + "only supported on Windows Matlab with WSL")
assert(stdlib.has_wsl(), "stdlib:sys:wsl_tempfile:EnvironmentError", "%s only supported on Windows Matlab with WSL", mfilename())

[stat, p] = system("wsl mktemp -u");

assert(stat == 0, "stdlib:sys:wsl_tempfile:IOError", "could not get wsl mktemp " + p)
assert(stat == 0, "stdlib:sys:wsl_tempfile:IOError", "could not get wsl mktemp %s", p)

try %#ok<TRYNC>
p = strip(p);
end

end

%!test
%! if stdlib.has_wsl()
%! assert(ischar(wsl_tempfile()))
%! endif
Loading

0 comments on commit 11d27ca

Please sign in to comment.