Skip to content

Commit 47aee56

Browse files
committed
reorganized as stdlib
1 parent 0b64d8a commit 47aee56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+984
-61
lines changed

+hdf5nc/TestUnit.m

Lines changed: 0 additions & 17 deletions
This file was deleted.

+stdlib/+fileio/TestFileio.m

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
classdef TestFileio < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_expanduser(tc)
6+
import stdlib.fileio.expanduser
7+
tc.verifyFalse(startsWith(expanduser('~/foo'), "~"))
8+
tc.verifyFalse(any(startsWith(expanduser(["~/abc", "~/123"]), "~")))
9+
10+
tc.verifyTrue(endsWith(expanduser('~/foo'), "foo"))
11+
tc.verifyTrue(all(endsWith(expanduser(["~/abc", "~/123"]), ["abc", "123"])))
12+
13+
tc.verifyEmpty(expanduser(string.empty))
14+
tc.verifyEqual(expanduser(""), "")
15+
end
16+
17+
function test_posix(tc)
18+
import stdlib.fileio.posix
19+
20+
if ispc
21+
tc.verifyFalse(contains(posix("c:\foo"), "\"))
22+
tc.verifyFalse(any(contains(posix(["x:\123", "d:\abc"]), "\")))
23+
end
24+
25+
tc.verifyEmpty(posix(string.empty))
26+
end
27+
28+
function test_is_absolute_path(tc)
29+
30+
import stdlib.fileio.is_absolute_path
31+
% path need not exist
32+
tc.verifyTrue(is_absolute_path('~/foo'))
33+
if ispc
34+
tc.verifyTrue(is_absolute_path('x:/foo'))
35+
tc.verifyEqual(is_absolute_path(["x:/abc", "x:/123", "", "c"]), [true, true, false, false])
36+
tc.verifyTrue(all(is_absolute_path(["x:/abc"; "x:/123"])))
37+
tc.verifyFalse(is_absolute_path('/foo'))
38+
else
39+
tc.verifyTrue(is_absolute_path('/foo'))
40+
end
41+
42+
tc.verifyEmpty(is_absolute_path(string.empty))
43+
tc.verifyFalse(is_absolute_path(""))
44+
tc.verifyFalse(is_absolute_path("c"))
45+
end
46+
47+
function test_absolute_path(tc)
48+
49+
import stdlib.fileio.absolute_path
50+
51+
pabs = absolute_path('2foo');
52+
pabs2 = absolute_path('4foo');
53+
tc.verifyFalse(startsWith(pabs, "2"))
54+
tc.verifyTrue(strncmp(pabs, pabs2, 2))
55+
56+
par1 = absolute_path("../2foo");
57+
par2 = absolute_path("../4foo");
58+
tc.verifyFalse(startsWith(par1, ".."))
59+
tc.verifyTrue(strncmp(par2, pabs2, 2))
60+
61+
pt1 = absolute_path("bar/../2foo");
62+
tc.verifyFalse(contains(pt1, ".."))
63+
64+
va = absolute_path(["2foo", "4foo"]);
65+
tc.verifyFalse(any(startsWith(va, "2")))
66+
vs = extractBefore(va, 2);
67+
tc.verifyEqual(vs(1), vs(2))
68+
69+
tc.verifyEmpty(absolute_path(string.empty))
70+
tc.verifyEqual(absolute_path(""), string(pwd))
71+
end
72+
73+
function test_makedir(tc)
74+
d = tempname;
75+
stdlib.fileio.makedir(d)
76+
tc.assertTrue(isfolder(d))
77+
end
78+
79+
function test_which(tc)
80+
import stdlib.fileio.which
81+
82+
tc.verifyEmpty(which(string.empty))
83+
84+
n = "matlab";
85+
% assumes Matlab in environment variable PATH
86+
tc.assumeNotEmpty(which(n))
87+
88+
p = fullfile(matlabroot, "bin", n);
89+
90+
% full absolute path
91+
exe = which(p);
92+
93+
if ispc
94+
tc.verifyTrue(endsWith(exe, ".exe"))
95+
else
96+
tc.verifyFalse(endsWith(exe, ".exe"))
97+
end
98+
tc.assertTrue(isfile(exe))
99+
100+
end
101+
102+
function test_with_suffix(tc)
103+
import stdlib.fileio.with_suffix
104+
tc.verifyEqual(with_suffix("foo.h5", ".nc"), "foo.nc")
105+
if ~verLessThan("matlab", "9.9")
106+
% fileparts vectorized in R2020b
107+
tc.verifyEqual(with_suffix(["foo.h5", "bar.dat"], ".nc"), ["foo.nc", "bar.nc"])
108+
109+
tc.verifyEmpty(with_suffix(string.empty, ".nc"))
110+
tc.verifyEqual(with_suffix("", ""), "")
111+
tc.verifyEqual(with_suffix("c", ""), "c")
112+
tc.verifyEqual(with_suffix("c.nc", ""), "c")
113+
tc.verifyEqual(with_suffix("", ".nc"), ".nc")
114+
end
115+
end
116+
117+
function test_copyfile(tc)
118+
f1 = tempname;
119+
fclose(fopen(f1,'w'));
120+
stdlib.fileio.copyfile(f1, tempdir)
121+
end
122+
123+
end
124+
125+
end

+stdlib/+fileio/TestIni.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
classdef TestIni < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_example(tc)
6+
import matlab.unittest.constraints.IsFile
7+
import stdlib.fileio.ini2struct
8+
9+
cwd = fileparts(mfilename('fullpath'));
10+
example = fullfile(cwd, "example.ini");
11+
12+
tc.assumeThat(example, IsFile)
13+
14+
s = ini2struct(example);
15+
tc.verifyClass(s, 'struct')
16+
tc.verifyEqual(s.DATA.keyNum, 113);
17+
18+
end
19+
20+
function test_lint(tc)
21+
22+
cwd = fileparts(mfilename('fullpath'));
23+
tc.verifyEmpty(checkcode(fullfile(cwd, "ini2struct.m")))
24+
25+
end
26+
27+
end
28+
29+
end

+stdlib/+fileio/TestIntg.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
classdef TestIntg < matlab.unittest.TestCase
2+
3+
methods (Test)
4+
5+
function test_checkRAM(tc)
6+
import stdlib.sys.checkRAM
7+
tc.assumeNotEmpty(pyversion)
8+
9+
tc.assertTrue(islogical(checkRAM(1)))
10+
end
11+
12+
function test_diskfree(tc)
13+
import stdlib.sys.diskfree
14+
tc.assertTrue(isnumeric(diskfree('~')))
15+
tc.assertTrue(diskfree('~') > 0, 'diskfree')
16+
end
17+
18+
function test_memory(tc)
19+
import stdlib.sys.memfree
20+
tc.assumeNotEmpty(pyversion)
21+
tc.assertTrue(isnumeric(memfree))
22+
end
23+
24+
end
25+
end

+stdlib/+fileio/absolute_path.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
function abspath = absolute_path(p)
2+
% path need not exist, but absolute path is returned
3+
%
4+
% NOTE: some network file systems are not resolvable by Matlab Java
5+
% subsystem, but are sometimes still valid--so return
6+
% unmodified path if this occurs.
7+
%
8+
% Copyright (c) 2020 Michael Hirsch (MIT License)
9+
arguments
10+
p (1,:) string
11+
end
12+
13+
import stdlib.fileio.expanduser
14+
import stdlib.fileio.is_absolute_path
15+
16+
% have to expand ~ first
17+
p = expanduser(p);
18+
19+
if ~is_absolute_path(p)
20+
% otherwise the default is Documents/Matlab, which is probably not wanted.
21+
p = fullfile(pwd, p);
22+
end
23+
24+
abspath = p;
25+
26+
for i = 1:length(abspath)
27+
abspath(i) = string(java.io.File(abspath(i)).getCanonicalPath());
28+
end
29+
30+
end % function

+stdlib/+fileio/copyfile.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function copyfile(in, out)
2+
%% copyfile(path) overloads copyfile with tilde expansion
3+
% "out" may be a directory or directory/name.ext
4+
arguments
5+
in (1,1) string
6+
out (1,1) string
7+
end
8+
9+
import stdlib.fileio.expanduser
10+
11+
in = expanduser(in);
12+
out = expanduser(out);
13+
14+
try
15+
copyfile(in, out);
16+
catch e
17+
if e.identifier ~= "MATLAB:COPYFILE:SourceAndDestinationSame"
18+
rethrow(e)
19+
end
20+
end
21+
22+
end % function

+stdlib/+fileio/example.ini

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; example.ini -- INI file example for the class INiConfig
2+
; UPD: 21.03.2010
3+
4+
; Supported whitespaces in section names
5+
[Section 1] ; allowed the comment to section
6+
key11=10 ; numeric scalar
7+
key12=1.45, 19.5, 0.6, -1.4 ; numeric vector
8+
key13=1.5+3i, -2-2i ; complex numeric vector
9+
key14=Hello, Matlab! ; string
10+
11+
[Section 2]
12+
; Description of key 1
13+
key21=3, 5, -1, 2+3i ; real and complex numeric vector
14+
15+
; Description of key 2
16+
key23= ; empty value
17+
18+
[Section 3] ; this empty section
19+
20+
; Sect
21+
[DATA]
22+
; Supported whitespaces in key names
23+
key num = 113 ; supported whitespaces before and after "=" delimiter
24+
key str = INI file example
25+
26+
[12345]
27+
01=1, 2, 3, 4, 5
28+
02=1 2 3 4 5 ; supported whitespace delimiter without comma
29+
key.01=2.45e-005 ; %e format
File renamed without changes.

+stdlib/+fileio/extract_zstd.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function extract_zstd(archive, out_dir)
2+
% extract a zstd file "archive" to "out_dir"
3+
% out_dir need not exist yet, but its parent must
4+
% we do this in two steps to be reliable with old tar versions
5+
% https://www.scivision.dev/tar-extract-zstd
6+
7+
arguments
8+
archive (1,1) string
9+
out_dir (1,1) string
10+
end
11+
12+
archive = stdlib.fileio.expanduser(archive);
13+
14+
assert(isfile(archive), "%s is not a file", archive)
15+
16+
[ret, ~] = system("zstd -h");
17+
if ret ~= 0
18+
if ismac
19+
msg = "brew install zstd";
20+
else
21+
msg = "cmake -P cmake/build_zstd.cmake";
22+
end
23+
error("need to have Zstd installed: \n install zstd by: \n %s", msg)
24+
end
25+
26+
tar_arc = tempname;
27+
28+
ret = system("zstd -d " + archive + " -o " + tar_arc);
29+
assert(ret == 0, "problem extracting %s", archive)
30+
31+
untar(tar_arc, out_dir)
32+
delete(tar_arc)
33+
34+
end

0 commit comments

Comments
 (0)