36 #include <sys/types.h>
39 #include "vasnprintf.h"
79 if (! s || ! (isalpha (*s) || *s ==
'_' || *s ==
'$'))
83 if (! (isalnum (*s) || *s ==
'_' || *s ==
'$'))
95 DEFUN (isvarname, args, ,
97 @deftypefn {Built-in Function} {} isvarname (@var{name})\n\
98 Return true if @var{name} is a valid variable name.\n\
99 @seealso{iskeyword, exist, who}\n\
104 int nargin = args.
length ();
108 else if (args(0).is_string ())
110 std::string varname = args(0).string_value ();
138 almost_match (
const std::string& std,
const std::string& s,
int min_match_len,
141 int stdlen = std.length ();
142 int slen = s.length ();
144 return (slen <= stdlen
145 && slen >= min_match_len
147 ? (strncmp (std.c_str (), s.c_str (), slen) == 0)
155 const std::string& s,
156 int min_toks_to_match,
int max_toks)
160 int toks_matched = 0;
162 if (s.empty () || max_toks < 1)
165 char *kw =
strsave (s.c_str ());
183 const char **to_match =
new const char * [max_toks + 1];
184 const char *
const *s1 = std;
185 const char **s2 = to_match;
192 while ((end = strchr (beg,
' ')) != 0)
204 if (tok_count >= max_toks)
215 if (!
almost_match (*s1, *s2, min_len[toks_matched], 0))
225 status = (toks_matched >= min_toks_to_match);
247 return (nr == 0 || nc == 0);
290 DEFUN (file_in_loadpath, args, ,
292 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\
293 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\
295 Return the absolute name of @var{file} if it can be found in\n\
296 the list of directories specified by @code{path}.\n\
297 If no file is found, return an empty character string.\n\
299 If the first argument is a cell array of strings, search each\n\
300 directory of the loadpath for element of the cell array and return\n\
301 the first that matches.\n\
303 If the second optional argument @qcode{\"all\"} is supplied, return\n\
304 a cell array containing the list of all files that have the same\n\
305 name in the path. If no files are found, return an empty cell array.\n\
306 @seealso{file_in_path, find_dir_in_path, path}\n\
311 int nargin = args.
length ();
313 if (nargin == 1 || nargin == 2)
322 else if (nargin == 2)
324 std::string opt = args(1).string_value ();
330 error (
"file_in_loadpath: invalid option");
334 error (
"file_in_loadpath: FILE argument must be a string");
362 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\
363 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\
364 Return the absolute name of @var{file} if it can be found in\n\
365 @var{path}. The value of @var{path} should be a colon-separated list of\n\
366 directories in the format described for @code{path}. If no file\n\
367 is found, return an empty character string. For example:\n\
371 file_in_path (EXEC_PATH, \"sh\")\n\
372 @result{} \"/bin/sh\"\n\
376 If the second argument is a cell array of strings, search each\n\
377 directory of the path for element of the cell array and return\n\
378 the first that matches.\n\
380 If the third optional argument @qcode{\"all\"} is supplied, return\n\
381 a cell array containing the list of all files that have the same\n\
382 name in the path. If no files are found, return an empty cell array.\n\
383 @seealso{file_in_loadpath, find_dir_in_path, path}\n\
388 int nargin = args.
length ();
390 if (nargin == 2 || nargin == 3)
392 std::string path = args(0).string_value ();
402 else if (nargin == 3)
404 std::string opt = args(2).string_value ();
410 error (
"file_in_path: invalid option");
414 error (
"file_in_path: all arguments must be strings");
417 error (
"file_in_path: PATH must be a string");
447 std::string nm = name;
449 if (! suffix.empty ())
463 int len = name.length ();
474 else if (len > 2 && name[len - 2] ==
'.' && name[len - 1] ==
'm')
478 std::string fname = name;
480 if (pos != std::string::npos)
481 fname = name.substr (0, pos);
498 if (dir.length () > 0)
501 std::string (
"Contents.m"));
520 int len = name.length ();
531 else if (len > 4 && name[len - 4] ==
'.' && name[len - 3] ==
'o'
532 && name[len - 2] ==
'c' && name[len - 1] ==
't')
549 int len = name.length ();
560 else if (len > 4 && name[len - 4] ==
'.' && name[len - 3] ==
'm'
561 && name[len - 2] ==
'e' && name[len - 1] ==
'x')
579 size_t len = s.length ();
585 if (s[j] ==
'\\' && j+1 < len)
634 warning (
"unrecognized escape sequence '\\%c' --\
635 converting to '%c'", s[j], s[j]);
656 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\
657 Convert special characters in @var{string} to their escaped forms.\n\
662 int nargin = args.
length ();
666 if (args(0).is_string ())
669 error (
"do_string_escapes: STRING argument must be of type string");
733 static char retval[2];
746 for (
size_t i = 0; i < s.length (); i++)
754 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\
755 Convert special characters in strings back to their escaped forms. For\n\
756 example, the expression\n\
763 assigns the value of the alert character (control-g, ASCII code 7) to\n\
764 the string variable @code{bell}. If this string is printed, the\n\
765 system will ring the terminal bell (if it is possible). This is\n\
766 normally the desired outcome. However, sometimes it is useful to be\n\
767 able to print the original representation of the string, with the\n\
768 special characters replaced by their escape sequences. For example,\n\
772 octave:13> undo_string_escapes (bell)\n\
778 replaces the unprintable alert character with its printable\n\
784 int nargin = args.
length ();
788 if (args(0).is_string ())
791 error (
"undo_string_escapes: S argument must be a string");
813 DEFUN (is_absolute_filename, args, ,
815 @deftypefn {Built-in Function} {} is_absolute_filename (@var{file})\n\
816 Return true if @var{file} is an absolute filename.\n\
817 @seealso{is_rooted_relative_filename, make_absolute_filename, isdir}\n\
822 if (args.length () == 1)
823 retval = (args(0).is_string ()
838 DEFUN (is_rooted_relative_filename, args, ,
840 @deftypefn {Built-in Function} {} is_rooted_relative_filename (@var{file})\n\
841 Return true if @var{file} is a rooted-relative filename.\n\
842 @seealso{is_absolute_filename, make_absolute_filename, isdir}\n\
847 if (args.length () == 1)
848 retval = (args(0).is_string ()
863 DEFUN (make_absolute_filename, args, ,
865 @deftypefn {Built-in Function} {} make_absolute_filename (@var{file})\n\
866 Return the full name of @var{file} beginning from the root of the file\n\
867 system. No check is done for the existence of @var{file}.\n\
868 @seealso{canonicalize_file_name, is_absolute_filename, is_rooted_relative_filename, isdir}\n\
873 if (args.length () == 1)
875 std::string nm = args(0).string_value ();
880 error (
"make_absolute_filename: FILE argument must be a file name");
895 DEFUN (find_dir_in_path, args, ,
897 @deftypefn {Built-in Function} {} find_dir_in_path (@var{dir})\n\
898 @deftypefnx {Built-in Function} {} find_dir_in_path (@var{dir}, \"all\")\n\
899 Return the full name of the path element matching @var{dir}. The\n\
900 match is performed at the end of each path element. For example, if\n\
901 @var{dir} is @qcode{\"foo/bar\"}, it matches the path element\n\
902 @nospell{@qcode{\"/some/dir/foo/bar\"}}, but not\n\
903 @nospell{@qcode{\"/some/dir/foo/bar/baz\"}}\n\
904 @nospell{@qcode{\"/some/dir/allfoo/bar\"}}.\n\
906 The second argument is optional. If it is supplied, return a cell array\n\
907 containing all name matches rather than just the first.\n\
908 @seealso{file_in_path, file_in_loadpath, path}\n\
913 int nargin = args.length ();
917 if (nargin == 1 || nargin == 2)
919 dir = args(0).string_value ();
925 else if (nargin == 2)
929 error (
"find_dir_in_path: DIR must be a directory name");
946 @deftypefn {Built-in Function} {@var{err} =} errno ()\n\
947 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})\n\
948 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})\n\
949 Return the current value of the system-dependent variable errno,\n\
950 set its value to @var{val} and return the previous value, or return\n\
951 the named error code given @var{name} as a character string, or -1\n\
952 if @var{name} is not found.\n\
957 int nargin = args.
length ();
961 if (args(0).is_string ())
963 std::string nm = args(0).string_value ();
968 error (
"errno: expecting character string argument");
972 int val = args(0).int_value ();
977 error (
"errno: expecting integer argument");
980 else if (nargin == 0)
1003 DEFUN (errno_list, args, ,
1005 @deftypefn {Built-in Function} {} errno_list ()\n\
1006 Return a structure containing the system-dependent errno values.\n\
1011 if (args.length () == 0)
1028 if (nr < 0 || nc < 0)
1031 "%s: converting negative dimension to zero", warnfor);
1033 nr = (nr < 0) ? 0 : nr;
1034 nc = (nc < 0) ? 0 : nc;
1043 for (
int i = 0; i < dim.
length (); i++)
1054 "%s: converting negative dimension to zero", warnfor);
1073 if (nr == 1 || nc == 1)
1083 dim(i) =
static_cast<int> (
fix (v(i)));
1086 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1107 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1))
1118 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1133 error (
"%s: expecting two scalar arguments", warn_for);
1146 retval = dims.
numel ();
1157 retval *= idxi.
numel ();
1163 retval *= jdx.
length (dv(i));
1176 if (nr > 0 && nc > 0)
1192 if (nr > 0 && nc > 0)
1209 va_start (args, fmt);
1235 int status = gnulib::vasprintf (&result, fmt, args);
1252 va_start (args, fmt);
1269 =
static_cast<unsigned int> (modf (seconds, &t) * 1000000);
1274 :
static_cast<unsigned int> (t));
1285 DEFUN (isindex, args, ,
1287 @deftypefn {Built-in Function} {} isindex (@var{ind})\n\
1288 @deftypefnx {Built-in Function} {} isindex (@var{ind}, @var{n})\n\
1289 Return true if @var{ind} is a valid index. Valid indices are\n\
1290 either positive integers (although possibly of real data type), or logical\n\
1291 arrays. If present, @var{n} specifies the maximum extent of the dimension\n\
1292 to be indexed. When possible the internal result is cached so that\n\
1293 subsequent indexing using @var{ind} will not perform the check again.\n\
1297 int nargin = args.
length ();
1301 n = args(1).idx_type_value ();
1302 else if (nargin != 1)
1323 retval = idx.
extent (n) <= n;
1330 catch (octave_execution_exception)
1353 int nargin = args.
length ();
1358 const Cell *ccells = cells;
1363 for (
int i = 0; i < nargin; i++)
1374 new_args(i) = ccells[i](0);
1376 else if (numel == 1)
1379 dims = ccells[i].
dims ();
1381 else if (dims != ccells[i].dims ())
1383 error (
"%s: cell arguments must have matching sizes", fun_name);
1391 for (
int i = 0; i < nargout; i++)
1392 rcells[i].
clear (dims);
1396 for (
int i = 0; i < nargin; i++)
1398 new_args(i) = ccells[i](j);
1404 if (tmp.
length () < nargout)
1406 error (
"%s: do_simple_cellfun: internal error", fun_name);
1411 for (
int i = 0; i < nargout; i++)
1412 rcells[i](j) = tmp(i);
1419 retval.resize (nargout);
1420 for (
int i = 0; i < nargout; i++)
1421 retval(i) = rcells[i];