Skip to content

Commit 46774a2

Browse files
committed
add handle2filename()
1 parent 6e00741 commit 46774a2

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

+stdlib/handle2filename.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function name = handle2filename(fileHandle)
2+
% HANDLE2FILENAME Convert a file handle to a filename
3+
arguments
4+
fileHandle (1,1) {mustBeInteger}
5+
end
6+
7+
if fileHandle >= 0
8+
name = stdlib.posix(fopen(fileHandle));
9+
else
10+
name = string.empty;
11+
end
12+
13+
end

+stdlib/posix.m

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
function p = posix(p)
22
%% posix(file)
3-
% convert a path to a Posix path separated with "/" even on Windows.
3+
% convert a path to a Posix string path separated with "/" even on Windows.
44
% If Windows path also have escaping "\" this breaks
55
arguments
66
p string
77
end
88

9+
p = string(p);
10+
911
if ispc
1012
p = strrep(p, "\", "/");
1113
end

+stdlib/stem.m

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
[~, p] = fileparts(path);
88

9-
p = stdlib.posix(p);
10-
119
end

test/TestFileImpure.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,13 @@ function test_get_permissions(tc)
336336
tc.verifyThat(p, StartsWithSubstring("r"))
337337
end
338338

339+
function test_handle2filename(tc)
340+
tc.verifyEqual(stdlib.handle2filename(0), '"' + "stdin" + '"')
341+
tc.verifyEqual(stdlib.handle2filename(1), '"' + "stdout" + '"')
342+
tc.verifyEqual(stdlib.handle2filename(2), '"' + "stderr" + '"')
343+
tc.verifyEmpty(stdlib.handle2filename(fopen(tempname)))
344+
end
345+
339346
function test_java_version(tc)
340347
v = stdlib.java_version();
341348
L = strlength(v);

0 commit comments

Comments
 (0)