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 ();
142 int stdlen = std.length ();
143 int slen = s.length ();
145 return (slen <= stdlen
146 && slen >= min_match_len
148 ? (strncmp (std.c_str (), s.c_str (), slen) == 0)
156 const std::string& s,
157 int min_toks_to_match,
int max_toks)
161 int toks_matched = 0;
163 if (s.empty () || max_toks < 1)
166 char *kw =
strsave (s.c_str ());
184 const char **to_match =
new const char * [max_toks + 1];
185 const char *
const *s1 = std;
186 const char **s2 = to_match;
193 while ((end = strchr (beg,
' ')) != 0)
205 if (tok_count >= max_toks)
216 if (!
almost_match (*s1, *s2, min_len[toks_matched], 0))
226 status = (toks_matched >= min_toks_to_match);
248 return (nr == 0 || nc == 0);
291 DEFUN (file_in_loadpath, args, ,
293 @deftypefn {Built-in Function} {} file_in_loadpath (@var{file})\n\
294 @deftypefnx {Built-in Function} {} file_in_loadpath (@var{file}, \"all\")\n\
296 Return the absolute name of @var{file} if it can be found in\n\
297 the list of directories specified by @code{path}.\n\
299 If no file is found, return an empty character string.\n\
301 If the first argument is a cell array of strings, search each directory of\n\
302 the loadpath for element of the cell array and return the first that\n\
305 If the second optional argument @qcode{\"all\"} is supplied, return a cell\n\
306 array containing the list of all files that have the same name in the path.\n\
307 If no files are found, return an empty cell array.\n\
308 @seealso{file_in_path, dir_in_loadpath, path}\n\
313 int nargin = args.
length ();
315 if (nargin == 1 || nargin == 2)
324 else if (nargin == 2)
326 std::string opt = args(1).string_value ();
332 error (
"file_in_loadpath: invalid option");
336 error (
"file_in_loadpath: FILE argument must be a string");
366 @deftypefn {Built-in Function} {} file_in_path (@var{path}, @var{file})\n\
367 @deftypefnx {Built-in Function} {} file_in_path (@var{path}, @var{file}, \"all\")\n\
368 Return the absolute name of @var{file} if it can be found in @var{path}.\n\
370 The value of @var{path} should be a colon-separated list of directories in\n\
371 the format described for @code{path}. If no file is found, return an empty\n\
372 character string. For example:\n\
376 file_in_path (EXEC_PATH, \"sh\")\n\
377 @result{} \"/bin/sh\"\n\
381 If the second argument is a cell array of strings, search each directory of\n\
382 the path for element of the cell array and return the first that matches.\n\
384 If the third optional argument @qcode{\"all\"} is supplied, return a cell\n\
385 array containing the list of all files that have the same name in the path.\n\
386 If no files are found, return an empty cell array.\n\
387 @seealso{file_in_loadpath, dir_in_loadpath, path}\n\
392 int nargin = args.
length ();
394 if (nargin == 2 || nargin == 3)
396 if (args(0).is_string ())
398 std::string path = args(0).string_value ();
406 else if (nargin == 3)
408 std::string opt = args(2).string_value ();
414 error (
"file_in_path: invalid option");
418 error (
"file_in_path: all arguments must be strings");
421 error (
"file_in_path: PATH must be a string");
454 std::string nm = name;
456 if (! suffix.empty ())
464 const std::string& file,
465 bool require_regular_file)
467 std::string fname = file;
479 = fs.
exists () && (fs.
is_reg () || ! require_regular_file);
508 int len = name.length ();
519 else if (len > 2 && name[len - 2] ==
'.' && name[len - 1] ==
'm')
523 std::string fname = name;
525 if (pos != std::string::npos)
526 fname = name.substr (0, pos);
543 if (dir.length () > 0)
546 std::string (
"Contents.m"));
565 int len = name.length ();
576 else if (len > 4 && name[len - 4] ==
'.' && name[len - 3] ==
'o'
577 && name[len - 2] ==
'c' && name[len - 1] ==
't')
594 int len = name.length ();
605 else if (len > 4 && name[len - 4] ==
'.' && name[len - 3] ==
'm'
606 && name[len - 2] ==
'e' && name[len - 1] ==
'x')
624 size_t len = s.length ();
630 if (s[j] ==
'\\' && j+1 < len)
679 warning (
"unrecognized escape sequence '\\%c' --\
680 converting to '%c'", s[j], s[j]);
701 @deftypefn {Built-in Function} {} do_string_escapes (@var{string})\n\
702 Convert escape sequences in @var{string} to the characters they represent.\n\
704 Escape sequences begin with a leading backslash\n\
705 (@qcode{'@xbackslashchar{}'}) followed by 1--3 characters\n\
706 (.e.g., @qcode{\"@xbackslashchar{}n\"} => newline).\n\
707 @seealso{undo_string_escapes}\n\
712 int nargin = args.
length ();
716 if (args(0).is_string ())
719 error (
"do_string_escapes: STRING argument must be of type string");
793 static char retval[2];
806 for (
size_t i = 0; i < s.length (); i++)
814 @deftypefn {Built-in Function} {} undo_string_escapes (@var{s})\n\
815 Convert special characters in strings back to their escaped forms.\n\
817 For example, the expression\n\
824 assigns the value of the alert character (control-g, ASCII code 7) to the\n\
825 string variable @code{bell}. If this string is printed, the system will\n\
826 ring the terminal bell (if it is possible). This is normally the desired\n\
827 outcome. However, sometimes it is useful to be able to print the original\n\
828 representation of the string, with the special characters replaced by their\n\
829 escape sequences. For example,\n\
833 octave:13> undo_string_escapes (bell)\n\
839 replaces the unprintable alert character with its printable representation.\n\
840 @seealso{do_string_escapes}\n\
845 int nargin = args.
length ();
849 if (args(0).is_string ())
852 error (
"undo_string_escapes: S argument must be a string");
882 DEFUN (is_absolute_filename, args, ,
884 @deftypefn {Built-in Function} {} is_absolute_filename (@var{file})\n\
885 Return true if @var{file} is an absolute filename.\n\
886 @seealso{is_rooted_relative_filename, make_absolute_filename, isdir}\n\
891 if (args.length () == 1)
892 retval = (args(0).is_string ()
907 DEFUN (is_rooted_relative_filename, args, ,
909 @deftypefn {Built-in Function} {} is_rooted_relative_filename (@var{file})\n\
910 Return true if @var{file} is a rooted-relative filename.\n\
911 @seealso{is_absolute_filename, make_absolute_filename, isdir}\n\
916 if (args.length () == 1)
917 retval = (args(0).is_string ()
932 DEFUN (make_absolute_filename, args, ,
934 @deftypefn {Built-in Function} {} make_absolute_filename (@var{file})\n\
935 Return the full name of @var{file} beginning from the root of the file\n\
938 No check is done for the existence of @var{file}.\n\
939 @seealso{canonicalize_file_name, is_absolute_filename, is_rooted_relative_filename, isdir}\n\
944 if (args.length () == 1)
946 std::string nm = args(0).string_value ();
951 error (
"make_absolute_filename: FILE argument must be a file name");
966 DEFUN (dir_in_loadpath, args, ,
968 @deftypefn {Built-in Function} {} dir_in_loadpath (@var{dir})\n\
969 @deftypefnx {Built-in Function} {} dir_in_loadpath (@var{dir}, \"all\")\n\
970 Return the full name of the path element matching @var{dir}.\n\
972 The match is performed at the end of each path element. For example, if\n\
973 @var{dir} is @qcode{\"foo/bar\"}, it matches the path element\n\
974 @nospell{@qcode{\"/some/dir/foo/bar\"}}, but not\n\
975 @nospell{@qcode{\"/some/dir/foo/bar/baz\"}}\n\
976 @nospell{@qcode{\"/some/dir/allfoo/bar\"}}.\n\
978 If the optional second argument is supplied, return a cell array containing\n\
979 all name matches rather than just the first.\n\
980 @seealso{file_in_path, file_in_loadpath, path}\n\
985 int nargin = args.length ();
989 if (nargin == 1 || nargin == 2)
991 dir = args(0).string_value ();
997 else if (nargin == 2)
1001 error (
"dir_in_loadpath: DIR must be a directory name");
1029 @deftypefn {Built-in Function} {@var{err} =} errno ()\n\
1030 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{val})\n\
1031 @deftypefnx {Built-in Function} {@var{err} =} errno (@var{name})\n\
1032 Return the current value of the system-dependent variable errno,\n\
1033 set its value to @var{val} and return the previous value, or return\n\
1034 the named error code given @var{name} as a character string, or -1\n\
1035 if @var{name} is not found.\n\
1036 @seealso{errno_list}\n\
1041 int nargin = args.
length ();
1045 if (args(0).is_string ())
1047 std::string nm = args(0).string_value ();
1053 int val = args(0).int_value ();
1058 error (
"errno: argument must be string or integer");
1061 else if (nargin == 0)
1084 DEFUN (errno_list, args, ,
1086 @deftypefn {Built-in Function} {} errno_list ()\n\
1087 Return a structure containing the system-dependent errno values.\n\
1093 if (args.length () == 0)
1110 if (nr < 0 || nc < 0)
1113 "%s: converting negative dimension to zero", warnfor);
1115 nr = (nr < 0) ? 0 : nr;
1116 nc = (nc < 0) ? 0 : nc;
1125 for (
int i = 0; i < dim.
length (); i++)
1136 "%s: converting negative dimension to zero", warnfor);
1155 if (nr == 1 || nc == 1)
1165 dim(i) =
static_cast<int> (
fix (v(i)));
1168 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1189 if ((nr == 1 && nc == 2) || (nr == 2 && nc == 1))
1200 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1215 error (
"%s: expecting two scalar arguments", warn_for);
1228 retval = dims.
numel ();
1239 retval *= idxi.
numel ();
1245 retval *= jdx.
length (dv(i));
1258 if (nr > 0 && nc > 0)
1274 if (nr > 0 && nc > 0)
1291 va_start (args, fmt);
1317 int status = gnulib::vasprintf (&result, fmt, args);
1334 va_start (args, fmt);
1351 =
static_cast<unsigned int> (modf (seconds, &t) * 1000000);
1356 :
static_cast<unsigned int> (t));
1367 DEFUN (isindex, args, ,
1369 @deftypefn {Built-in Function} {} isindex (@var{ind})\n\
1370 @deftypefnx {Built-in Function} {} isindex (@var{ind}, @var{n})\n\
1371 Return true if @var{ind} is a valid index.\n\
1373 Valid indices are either positive integers (although possibly of real data\n\
1374 type), or logical arrays.\n\
1376 If present, @var{n} specifies the maximum extent of the dimension to be\n\
1377 indexed. When possible the internal result is cached so that subsequent\n\
1378 indexing using @var{ind} will not perform the check again.\n\
1380 Implementation Note: Strings are first converted to double values before the\n\
1381 checks for valid indices are made. Unless a string contains the NULL\n\
1382 character @nospell{\"\\0\"}, it will always be a valid index.\n\
1386 int nargin = args.
length ();
1390 n = args(1).idx_type_value ();
1391 else if (nargin != 1)
1405 idx_vector idx = args(0).index_vector (
true);
1410 retval = idx.
extent (n) <= n;
1417 catch (octave_execution_exception)
1442 int nargin = args.
length ();
1447 const Cell *ccells = cells;
1452 for (
int i = 0; i < nargin; i++)
1463 new_args(i) = ccells[i](0);
1465 else if (numel == 1)
1468 dims = ccells[i].
dims ();
1470 else if (dims != ccells[i].dims ())
1472 error (
"%s: cell arguments must have matching sizes", fun_name);
1480 for (
int i = 0; i < nargout; i++)
1481 rcells[i].
clear (dims);
1485 for (
int i = 0; i < nargin; i++)
1487 new_args(i) = ccells[i](j);
1493 if (tmp.
length () < nargout)
1495 error (
"%s: do_simple_cellfun: internal error", fun_name);
1500 for (
int i = 0; i < nargout; i++)
1501 rcells[i](j) = tmp(i);
1508 retval.resize (nargout);
1509 for (
int i = 0; i < nargout; i++)
1510 retval(i) = rcells[i];
1536 DEFUN (isstudent, args, ,
1538 @deftypefn {Built-in Function} {} isstudent ()\n\
1539 Return true if running in the student edition of @sc{matlab}.\n\
1541 @code{isstudent} always returns false in Octave.\n\
1545 if (args.length () != 0)
void warning_with_id(const char *id, const char *fmt,...)
size_t octave_vformat(std::ostream &os, const char *fmt, va_list args)
void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
static void clear(octave_shlib &oct_file)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
std::string find_first_of(const string_vector &names)
octave_idx_type length(octave_idx_type n=0) const
static string_vector find_matching_dirs(const std::string &dir)
std::string octave_asprintf(const char *fmt,...)
bool same_file(const std::string &f, const std::string &g)
octave_idx_type rows(void) const
std::string mex_file_in_path(const std::string &name)
std::streamsize oprecision
OCTINTERP_API void print_usage(void)
octave_idx_type numel(void) const
Number of elements in the array.
octave_idx_type length(void) const
bool is_scalar_type(void) const
bool is_numeric_type(void) const
static char * strsave(const char *s)
static std::string find_file(const std::string &file)
int int_value(bool req_int=false, bool frc_str_conv=false) const
static string_vector make_absolute(const string_vector &sv)
void resize(int n, int fill_value=0)
static std::string find_first_of(const string_vector &files)
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
void octave_sleep(double seconds)
static string_vector names(const map_type &lst)
~octave_preserve_stream_state(void)
int empty_arg(const char *, octave_idx_type nr, octave_idx_type nc)
static std::string find_mex_file(const std::string &fcn, const std::string &pack_name=std::string())
std::string file_in_path(const std::string &name, const std::string &suffix)
std::string fcn_file_in_path(const std::string &name)
bool is_keyword(const std::string &s)
OCTAVE_API int octave_strncasecmp(const char *s1, const char *s2, size_t n)
static void check_dimensions(octave_idx_type &nr, octave_idx_type &nc, const char *warnfor)
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
size_t octave_format(std::ostream &os, const char *fmt,...)
std::string contents_file_in_path(const std::string &dir)
static std::string find_oct_file(const std::string &fcn, const std::string &pack_name=std::string())
Cell cell_value(void) const
idx_vector index_vector(bool require_integers=false) const
void gripe_data_file_in_path(const std::string &fcn, const std::string &file)
std::string octave_vasprintf(const char *fmt, va_list args)
int almost_match(const std::string &std, const std::string &s, int min_match_len, int case_sens)
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
static std::string concat(const std::string &, const std::string &)
octave_idx_type columns(void) const
F77_RET_T const double const double * f
bool valid_identifier(const char *s)
octave_idx_type numel(const octave_value_list &idx)
bool same_file_internal(const std::string &file1, const std::string &file2)
static std::string make_absolute(const std::string &s, const std::string &dot_path=get_current_directory())
const char * undo_string_escape(char c)
#define DEFUNX(name, fname, args_name, nargout_name, doc)
bool is_magic_colon(void) const
dim_vector redim(int n) const
octave_idx_type length(void) const
static bool absolute_pathname(const std::string &s)
int keyword_almost_match(const char *const *std, int *min_len, const std::string &s, int min_toks_to_match, int max_toks)
static bool rooted_relative_pathname(const std::string &s)
std::string oct_file_in_path(const std::string &name)
std::ios::fmtflags oflags
std::string search_path_for_file(const std::string &path, const string_vector &names)
static std::string find_fcn_file(const std::string &fcn, const std::string &pack_name=std::string())
void warning(const char *fmt,...)
Handles the reference counting for all the derived classes.
charNDArray max(char d, const charNDArray &m)
octave_idx_type length(void) const
Number of elements in the array.
string_vector find_all_first_of(const string_vector &names)
bool is_empty(void) const
octave_idx_type extent(octave_idx_type n) const
std::string undo_string_escapes(const std::string &s)
octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx)
static int lookup(const std::string &name)
Array< double > vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
FloatMatrix float_identity_matrix(octave_idx_type nr, octave_idx_type nc)
std::string do_string_escapes(const std::string &s)
octave_value_list do_simple_cellfun(octave_value_list(*fun)(const octave_value_list &, int), const char *fun_name, const octave_value_list &args, int nargout)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
static std::string find_dir(const std::string &dir)
static octave_scalar_map list(void)
string_vector search_path_for_all_files(const std::string &path, const string_vector &names)
void octave_usleep(unsigned int useconds)
bool discard_error_messages
OCTAVE_EXPORT octave_value_list Ferrno(const octave_value_list &args, int)
static string_vector find_all_first_of(const string_vector &files)
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))
charNDArray min(char d, const charNDArray &m)