1######################################################################## 2## 3## Copyright (C) 2018-2021 The Octave Project Developers 4## 5## See the file COPYRIGHT.md in the top-level directory of this 6## distribution or <https://octave.org/copyright/>. 7## 8## This file is part of Octave. 9## 10## Octave is free software: you can redistribute it and/or modify it 11## under the terms of the GNU General Public License as published by 12## the Free Software Foundation, either version 3 of the License, or 13## (at your option) any later version. 14## 15## Octave is distributed in the hope that it will be useful, but 16## WITHOUT ANY WARRANTY; without even the implied warranty of 17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18## GNU General Public License for more details. 19## 20## You should have received a copy of the GNU General Public License 21## along with Octave; see the file COPYING. If not, see 22## <https://www.gnu.org/licenses/>. 23## 24######################################################################## 25 26## -*- texinfo -*- 27## @deftypefn {} {@var{tf} =} isfile (@var{f}) 28## Return true if @var{f} is a regular file and false otherwise. 29## 30## If @var{f} is a cell array of strings, @var{tf} is a logical array of the 31## same size. 32## @seealso{isfolder, exist, stat, is_absolute_filename, is_rooted_relative_filename} 33## @end deftypefn 34 35function retval = isfile (f) 36 37 if (nargin != 1) 38 print_usage (); 39 endif 40 41 if (! (ischar (f) || iscellstr (f))) 42 error ("isfile: F must be a string or cell array of strings"); 43 endif 44 45 f = cellstr (f); 46 retval = false (size (f)); 47 for i = 1:numel (f) 48 [info, err] = stat (f{i}); 49 retval(i) = (! err && S_ISREG (info.mode)); 50 endfor 51 52endfunction 53 54 55%!shared mfile 56%! mfile = which ("isfile"); 57 58%!assert (isfile (mfile)) 59%!assert (! isfile (tempdir ())) 60%!assert (isfile ({mfile, pwd()}), [true, false]) 61%!assert (isfile ({mfile; pwd()}), [true; false]) 62 63%!test 64%! unwind_protect 65%! tmp = tempname (); 66%! [d, n] = fileparts (tmp); 67%! assert (! isfile (tmp)); 68%! save ("-text", tmp, "tmp"); # cheap way to create a file 69%! assert (isfile (tmp)); 70%! addpath (d); 71%! assert (! isfile (n)); 72%! unwind_protect_cleanup 73%! try, unlink (tmp); end_try_catch 74%! try, rmpath (d); end_try_catch 75%! end_unwind_protect 76 77## Test input validation 78%!error isfile () 79%!error isfile ("a", "b") 80%!error <F must be a string> isfile (1.0) 81