Skip to content

Commit 82fea97

Browse files
committed
is_char_device: add sys., choose_method
1 parent beb9a34 commit 82fea97

File tree

4 files changed

+34
-23
lines changed

4 files changed

+34
-23
lines changed

+stdlib/+sys/is_char_device.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function y = is_char_device(file)
2+
3+
cmd = sprintf('test -c %s', file);
4+
5+
[s, ~] = system(cmd);
6+
y = s == 0;
7+
8+
end

+stdlib/is_char_device.m

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,15 @@
33
% e.g. NUL, /dev/null
44
% false if file does not exist
55

6-
function ok = is_char_device(p)
6+
function ok = is_char_device(file, method)
77
arguments
8-
p {mustBeTextScalar}
8+
file {mustBeTextScalar}
9+
method (1,:) string = ["python", "sys"]
910
end
1011

11-
if stdlib.has_python()
12-
ok = stdlib.python.is_char_device(p);
13-
elseif stdlib.isoctave()
14-
[s, err] = stat(p);
15-
ok = err == 0 && S_ISCHR(s.mode);
16-
else
17-
ok = logical.empty;
18-
end
12+
fun = choose_method(method, "is_char_device");
13+
14+
ok = fun(file);
1915

2016
end
2117

22-
%!assert (!is_exe(''))
23-
%!test
24-
%! if ispc
25-
%! n = "NUL";
26-
%! else
27-
%! n = "/dev/null";
28-
%! end
29-
%! assert (is_char_device(n))

+stdlib/private/choose_method.m

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,20 @@
3535
if ~isunix(), continue, end
3636
end
3737

38-
case {"legacy", "sys"}, has = true;
38+
case "legacy", has = true;
3939
case "native"
4040
has = stdlib.strempty(minVersion) || ~isMATLABReleaseOlderThan(minVersion);
4141

4242
if endsWith(name, "create_symlink")
4343
if ~has || ispc(), continue, end
4444
end
45+
case "sys"
46+
has = true;
47+
48+
if endsWith(name, "is_char_device")
49+
if ispc(), continue, end
50+
end
51+
4552
otherwise
4653
has = str2func("stdlib.has_" + m);
4754
end

test/TestExists.m

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
% on CI matlabroot can be writable!
1313
fname = {'is_readable', 'is_writable'}
1414
method = {'java', 'native', 'legacy'}
15+
icm = {'python', 'sys'}
1516
end
1617

1718
methods(TestClassSetup)
@@ -42,17 +43,24 @@ function test_is_rw(tc, Ps, method, fname)
4243
end
4344

4445

45-
function test_is_char_device(tc)
46-
is_capable(tc, @stdlib.python.is_char_device)
47-
46+
function test_is_char_device(tc, icm)
4847
% /dev/stdin may not be available on CI systems
4948
if ispc
5049
n = "NUL";
5150
else
5251
n = "/dev/null";
5352
end
5453

55-
tc.verifyTrue(stdlib.is_char_device(n))
54+
tc.assertNotEmpty(which("stdlib." + icm + ".is_char_device"))
55+
56+
try
57+
y = stdlib.is_char_device(n, icm);
58+
catch e
59+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError', e.message)
60+
return
61+
end
62+
63+
tc.verifyTrue(y)
5664
end
5765

5866

0 commit comments

Comments
 (0)