Skip to content

Commit 5b32ef1

Browse files
committed
is_{readable,writable}: array
1 parent 6178075 commit 5b32ef1

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

+stdlib/+native/is_readable.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
function y = is_readable(file)
2+
arguments
3+
file string
4+
end
25

3-
y = false;
4-
5-
if ~stdlib.exists(file), return, end
6+
y = stdlib.exists(file);
7+
if ~any(y)
8+
return
9+
end
610

711
props = "Readable";
812
if isunix
913
props = [props, "GroupRead", "OtherRead"];
1014
end
1115

12-
t = getPermissions(filePermissions(file), props);
13-
y = any(t{1, :});
16+
t = getPermissions(filePermissions(file(y)), props);
17+
y(y) = any(t{:,:}, 2);
1418

1519
end

+stdlib/+native/is_writable.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
function y = is_writable(p)
2-
3-
y = false;
1+
function y = is_writable(file)
2+
arguments
3+
file string
4+
end
45

5-
if ~stdlib.exists(p), return, end
6+
y = stdlib.exists(file);
7+
if ~any(y)
8+
return
9+
end
610

711
props = "Writable";
812
if isunix
913
props = [props, "GroupWrite", "OtherWrite"];
1014
end
1115

12-
t = getPermissions(filePermissions(p), props);
13-
y = any(t{1, :});
16+
t = getPermissions(filePermissions(file(y)), props);
17+
y(y) = any(t{:,:}, 2);
1418

1519
end

+stdlib/is_readable.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
function y = is_readable(file, backend)
99
arguments
10-
file {mustBeTextScalar}
10+
file string
1111
backend (1,:) string = ["java", "native", "legacy"]
1212
end
1313

14-
fun = hbackend(backend, "is_readable", 'R2025a');
14+
[fun, b] = hbackend(backend, "is_readable", 'R2025a');
1515

16-
y = fun(file);
16+
if isscalar(file) || b == "native"
17+
y = fun(file);
18+
else
19+
y = arrayfun(fun, file);
20+
end
1721

1822
end

+stdlib/is_writable.m

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@
77

88
function y = is_writable(file, backend)
99
arguments
10-
file {mustBeTextScalar}
10+
file string
1111
backend (1,:) string = ["java", "native", "legacy"]
1212
end
1313

14-
fun = hbackend(backend, "is_writable", 'R2025a');
14+
[fun, b] = hbackend(backend, "is_writable", 'R2025a');
1515

16-
y = fun(file);
16+
if isscalar(file) || b == "native"
17+
y = fun(file);
18+
else
19+
y = arrayfun(fun, file);
20+
end
1721

1822
end

test/TestExists.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ function test_is_rw(tc, Ps, backend, fname)
4343
end
4444

4545

46+
function test_is_rw_array(tc, backend, fname)
47+
h = str2func("stdlib." + fname);
48+
try
49+
r = h([".", tempname(), mfilename() + ".m"], backend);
50+
tc.verifyEqual(r, [true, false, true])
51+
catch e
52+
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
53+
end
54+
end
55+
56+
4657
function test_is_char_device(tc, icm)
4758
% /dev/stdin may not be available on CI systems
4859
if ispc

0 commit comments

Comments
 (0)