26#if defined (HAVE_CONFIG_H)
79 if (! s || ! (isalpha (*s) || *s ==
'_'))
83 if (! (isalnum (*s) || *s ==
'_'))
94DEFUN (isvarname, args, ,
104 if (args.length () != 1)
109 if (args(0).is_string ())
142 bool previous =
false;
143 bool any_non_space =
false;
146 c = ((any_non_space && previous && std::isalpha (c)) ? std::toupper (c)
148 previous = std::isspace (c);
149 any_non_space |= (! previous);
153 str.erase (std::remove_if (str.begin(), str.end(),
155 { return std::isspace(x); }),
164 str[0] = std::toupper (str[0]);
169 if (! std::isalpha (str[0]) && str[0] !=
'_')
175 c = (std::isalnum (c) ? c :
'_');
177 str.erase (std::remove_if (str.begin(), str.end(),
179 { return ! std::isalnum (x) && x !=
'_'; }),
183 const std::string permitted_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
184 "abcdefghijklmnopqrstuvwxyz"
187 std::size_t pos = str.find_first_not_of (permitted_chars);
191 while (pos != std::string::npos)
194 std::snprintf (hex_str,
sizeof (hex_str),
"0x%02X", str[pos]);
195 str.replace (pos, 1, hex_str);
198 pos = str.find_first_not_of (permitted_chars,
199 pos +
sizeof (hex_str) - 1);
209 auto nargs = args.
length ();
215 error (
"makeValidName: property/value options must occur in pairs");
217 auto str_to_lower = [] (std::string& s)
221 { return std::tolower(c); });
224 for (
auto i = 0; i < nargs; i = i + 2)
226 std::string parameter = args(i).xstring_value (
"makeValidName: "
227 "option argument must be a string");
228 str_to_lower (parameter);
229 if (parameter ==
"replacementstyle")
232 "'ReplacementStyle' value must be a string");
237 error (
"makeValidName: invalid 'ReplacementStyle' value '%s'",
240 else if (parameter ==
"prefix")
242 m_prefix = args(i + 1).xstring_value (
"makeValidName: "
243 "'Prefix' value must be a string");
246 error (
"makeValidName: invalid 'Prefix' value '%s'",
250 error (
"makeValidName: unknown property '%s'", parameter.c_str ());
254DEFUN (__make_valid_name__, args, ,
267 auto nargin = args.length ();
273 if (args(0).is_string ())
275 std::string varname = args(0).string_value ();
277 return ovl (varname, is_modified);
279 else if (args(0).iscellstr ())
283 for (
auto i = 0; i < varnames.
numel (); i++)
285 return ovl (varnames, is_modified);
288 error (
"makeValidName: STR must be a string or cellstr");
298DEFUN (is_same_file, args, ,
323 if (args.length () != 2)
329 bool s1_cellstr = args(0).iscellstr ();
330 bool s2_string = args(1).is_string ();
331 bool s2_cellstr = args(1).iscellstr ();
333 if (s1_string && s2_string)
335 std::string file1 = args(0).string_value ();
336 std::string file2 = args(1).string_value ();
340 else if ((s1_string && s2_cellstr) || (s1_cellstr && s2_string))
347 cellstr_arg = args(1);
352 cellstr_arg = args(0);
361 output(idx) =
same_file (str, cellstr(idx));
365 else if (s1_cellstr && s2_cellstr)
374 error (
"is_same_file: cellstr arrays FILEPATH1 and FILEPATH2 must be the same size");
379 output(idx) =
same_file (cellstr1(idx), cellstr2(idx));
384 error (
"is_same_file: FILEPATH1 and FILEPATH2 must be strings or cell arrays of strings");
408 int min_match_len,
int case_sens)
410 int stdlen =
std.length ();
411 int slen = s.length ();
413 return (slen <= stdlen
414 && slen >= min_match_len
416 ? (
strncmp (
std.c_str (), s.c_str (), slen) == 0)
423 const std::string& s,
424 int min_toks_to_match,
int max_toks)
428 int toks_matched = 0;
430 if (s.empty () || max_toks < 1)
433 char *kw =
strsave (s.c_str ());
450 const char **to_match =
new const char * [max_toks + 1];
451 const char *
const *s1 =
std;
452 const char **s2 = to_match;
459 while ((end = strchr (beg,
' ')) !=
nullptr)
471 if (tok_count >= max_toks)
476 s2[tok_count+1] =
nullptr;
482 if (!
almost_match (*s1, *s2, min_len[toks_matched], 0))
492 status = (toks_matched >= min_toks_to_match);
513 directory_path p (
path);
523 directory_path p (
path);
547DEFMETHOD (file_in_loadpath, interp, args, ,
570 int nargin = args.length ();
572 if (nargin < 1 || nargin > 2)
575 string_vector names = args(0).xstring_vector_value (
"file_in_loadpath: FILE argument must be a string");
578 error (
"file_in_loadpath: FILE argument must not be empty");
586 std::string opt = args(1).xstring_value (
"file_in_loadpath: optional second argument must be a string");
589 error (R
"(file_in_loadpath: "all" is only valid second argument)");
641 int nargin = args.length ();
643 if (nargin < 2 || nargin > 3)
646 std::string
path = args(0).xstring_value (
"file_in_path: PATH must be a string");
648 string_vector names = args(1).xstring_vector_value (
"file_in_path: FILE argument must be a string");
651 error (
"file_in_path: FILE argument must not be empty");
657 std::string opt = args(2).xstring_value (
"file_in_path: optional third argument must be a string");
660 error (R
"(file_in_path: "all" is only valid third argument)");
690 std::string nm =
name;
692 if (! suffix.empty ())
701 const std::string& file,
702 bool require_regular_file)
704 std::string fname = file;
707 || sys::env::rooted_relative_pathname (fname)))
712 sys::file_stat fs (fname);
715 = fs.exists () && (fs.is_reg () || ! require_regular_file);
749 sys::file_stat fs (
name);
751 if (fs.exists () && ! fs.is_dir ())
762 std::string fname =
name;
763 std::size_t pos =
name.find_first_of (
'>');
764 if (pos != std::string::npos)
765 fname =
name.substr (0, pos);
787 std::string tcontents
790 sys::file_stat fs (tcontents);
807 std::size_t
len = s.length ();
813 if (s[j] ==
'\\' && j+1 <
len)
867 int tmpi = s[j] -
'0';
870 int digit = s[k] -
'0';
871 if (digit < 0 || digit > 7)
887 if (! isxdigit (s[k]))
893 tmpi += digit -
'a' + 10;
894 else if (digit >=
'A')
895 tmpi += digit -
'A' + 10;
901 warning (R
"(malformed hex escape sequence '\x' -- converting to '\0')");
909 warning (R
"(unrecognized escape sequence '\%c' -- converting to '%c')", s[j], s[j]);
937 if (args.length () != 1)
940 std::string str = args(0).xstring_value (
"do_string_escapes: STRING argument must be of type string");
1021 static char retval[2] {
'\0',
'\0'};
1033 for (std::size_t i = 0; i < s.length (); i++)
1070 if (args.length () != 1)
1073 std::string str = args(0).xstring_value (
"undo_string_escapes: S argument must be a string");
1100DEFUN (is_absolute_filename, args, ,
1107 if (args.length () != 1)
1110 return ovl (args(0).is_string ()
1121DEFUN (is_rooted_relative_filename, args, ,
1128 if (args.length () != 1)
1131 return ovl (args(0).is_string ()
1132 && sys::env::rooted_relative_pathname (args(0).string_value ()));
1142DEFUN (make_absolute_filename, args, ,
1154 if (args.length () != 1)
1157 std::string nm = args(0).xstring_value (
"make_absolute_filename: FILE argument must be a filename");
1169DEFMETHOD (dir_in_loadpath, interp, args, ,
1192 int nargin = args.length ();
1194 if (nargin < 1 || nargin > 2)
1199 dir = args(0).xstring_value (
"dir_in_loadpath: DIR must be a directory name");
1201 load_path& lp = interp.get_load_path ();
1246 int nargin = args.length ();
1255 if (args(0).is_string ())
1263 int val = args(0).xint_value (
"errno: argument must be string or integer");
1289DEFUN (errno_list, args, ,
1296 if (args.length () != 0)
1309 const char *warnfor)
1311 if (nr < 0 || nc < 0)
1314 "%s: converting negative dimension to zero", warnfor);
1316 nr = (nr < 0) ? 0 : nr;
1317 nc = (nc < 0) ? 0 : nc;
1325 for (
int i = 0; i < dim.
ndims (); i++)
1336 "%s: converting negative dimension to zero", warnfor);
1346 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1381 if ((nr != 1 || nc != 2) && (nr != 2 || nc != 1))
1382 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1410 retval = dims.
numel ();
1421 retval *= idxi.
numel ();
1428 retval *= jdx.
length (dv(i));
1430 catch (
const index_exception& ie)
1432 error (
"dims_to_numel: invalid index %s", ie.what ());
1445 if (nr > 0 && nc > 0)
1460 if (nr > 0 && nc > 0)
1471 std::size_t
format (std::ostream& os,
const char *fmt, ...)
1476 va_start (args, fmt);
1478 retval =
vformat (os, fmt, args);
1485 std::size_t
vformat (std::ostream& os,
const char *fmt, va_list args)
1494 std::size_t
format (std::ostream& os,
const std::string& enc,
const char *fmt, ...)
1499 va_start (args, fmt);
1501 retval =
vformat (os, enc, fmt, args);
1508 std::size_t
vformat (std::ostream& os,
const std::string& enc,
const char *fmt,
1513 if (enc.compare (
"utf-8"))
1543 va_start (args, fmt);
1557 void sleep (
double seconds,
bool do_graphics_events)
1567 if (do_graphics_events)
1576 struct timespec one_tenth = { 0, 100000000 };
1584 if (do_graphics_events)
1585 gh_mgr.process_events ();
1593 double end_time = now.double_value () + seconds;
1594 double remaining_time = seconds;
1598 struct timespec nano_laps = { 0, 100000000 };
1600 while (remaining_time > 0.1)
1604 if (do_graphics_events)
1606 gh_mgr.process_events ();
1609 remaining_time = end_time - now.double_value ();
1611 if (remaining_time < 0.1)
1618 remaining_time = end_time - now.double_value ();
1621 if (remaining_time > 0.0)
1623 nano_laps = { 0,
static_cast<int> (remaining_time * 1e9) };
1647 int nargin = args.length ();
1649 if (nargin < 1 || nargin > 2)
1654 n = args(1).idx_type_value ();
1660 idx_vector idx = args(0).index_vector (
true);
1663 retval = idx.
extent (n) <= n;
1669 interp.recover_from_exception ();
1694 int nargin = args.
length ();
1699 const Cell *ccells = cells;
1704 for (
int i = 0; i < nargin; i++)
1707 iscell[i] = arg.
iscell ();
1715 new_args(i) = ccells[i](0);
1717 else if (
numel == 1)
1720 dims = ccells[i].
dims ();
1722 else if (dims != ccells[i].dims ())
1723 error (
"%s: cell arguments must have matching sizes", fun_name);
1727 for (
int i = 0; i < nargout; i++)
1728 rcells[i].clear (dims);
1732 for (
int i = 0; i < nargin; i++)
1734 new_args(i) = ccells[i](j);
1740 if (tmp.
length () < nargout)
1741 error (
"%s: do_simple_cellfun: internal error", fun_name);
1743 for (
int i = 0; i < nargout; i++)
1744 rcells[i](j) = tmp(i);
1749 for (
int i = 0; i < nargout; i++)
1750 retval(i) = rcells[i];
1769DEFUN (isstudent, args, ,
1778 if (args.length () != 0)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
charNDArray min(char d, const charNDArray &m)
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Vector representing the dimensions (size) of an Array.
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
void resize(int n, int fill_value=0)
bool isvector(void) const
octave_idx_type ndims(void) const
Number of dimensions.
OCTAVE_API dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
std::string find_first_of(const string_vector &files) const
string_vector find_matching_dirs(const std::string &dir) const
string_vector find_all_first_of(const string_vector &files) const
std::string find_file(const std::string &file) const
std::string find_dir(const std::string &dir) const
std::string find_fcn_file(const std::string &fcn, const std::string &pack_name="")
Helper class for make_valid_name function calls.
const std::string & get_replacement_style() const
std::string m_replacement_style
make_valid_name_options()=default
Default options for make_valid_name function calls.
const std::string & get_prefix() const
octave_idx_type extent(octave_idx_type n) const
octave_idx_type length(octave_idx_type n=0) const
static int lookup(const std::string &name)
static octave_scalar_map list(void)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length(void) const
octave::idx_vector index_vector(bool require_integers=false) const
octave_idx_type rows(void) const
bool isnumeric(void) const
octave_idx_type numel(void) const
octave_idx_type idx_type_value(bool req_int=false, bool frc_str_conv=false) const
bool is_scalar_type(void) const
bool is_string(void) const
Cell cell_value(void) const
octave_idx_type columns(void) const
OCTINTERP_API Array< octave_idx_type > octave_idx_type_vector_value(bool req_int=false, bool frc_str_conv=false, bool frc_vec_conv=false) const
std::string string_value(bool force=false) const
Array< std::string > cellstr_value(void) const
bool is_magic_colon(void) const
dim_vector dims(void) const
octave_idx_type numel(void) const
std::list< std::string > std_list(void) const
OCTINTERP_API void print_usage(void)
#define DEFMETHOD(name, interp_name, args_name, nargout_name, doc)
Macro to define a builtin method.
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Macro to define a builtin function with certain internal name.
void warning(const char *fmt,...)
void warning_with_id(const char *id, const char *fmt,...)
void error(const char *fmt,...)
void warn_data_file_in_path(const std::string &fcn, const std::string &file)
ColumnVector transform(const Matrix &m, double x, double y, double z)
OCTAVE_NAMESPACE_BEGIN bool iskeyword(const std::string &s)
OCTAVE_API int octave_strncasecmp(const char *s1, const char *s2, size_t n)
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
static char * strsave(const char *s)
OCTAVE_API std::string u8_to_encoding(const std::string &who, const std::string &u8_string, const std::string &encoding)
OCTAVE_API bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
gh_manager & __get_gh_manager__(const std::string &who)
load_path & __get_load_path__(const std::string &who)
int octave_nanosleep_wrapper(const struct timespec *requested, struct timespec *remaining)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
T::size_type numel(const T &str)
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static bool absolute_pathname(const std::string &s)
bool same_file_internal(const std::string &file1, const std::string &file2)
std::size_t vformat(std::ostream &os, const char *fmt, va_list args)
static string_vector make_absolute(const string_vector &sv)
std::string asprintf(const char *fmt,...)
static void check_dimensions(octave_idx_type &nr, octave_idx_type &nc, const char *warnfor)
std::string do_string_escapes(const std::string &s)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file)
octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx_arg)
std::string contents_file_in_path(const std::string &dir)
std::string undo_string_escapes(const std::string &s)
int almost_match(const std::string &std, const std::string &s, int min_match_len, int case_sens)
int keyword_almost_match(const char *const *std, int *min_len, const std::string &s, int min_toks_to_match, int max_toks)
Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
std::string file_in_path(const std::string &name, const std::string &suffix)
std::string vasprintf(const char *fmt, va_list args)
void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
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)
FloatMatrix float_identity_matrix(octave_idx_type nr, octave_idx_type nc)
std::size_t format(std::ostream &os, const char *fmt,...)
void sleep(double seconds, bool do_graphics_events)
OCTAVE_EXPORT octave_value_list Ferrno(const octave_value_list &args, int)
string_vector search_path_for_all_files(const std::string &path, const string_vector &names)
const char * undo_string_escape(char c)
bool make_valid_name(std::string &str, const make_valid_name_options &options)
Modify str to be a valid variable name.
OCTAVE_NAMESPACE_BEGIN bool valid_identifier(const char *s)
std::string search_path_for_file(const std::string &path, const string_vector &names)
std::string fcn_file_in_path(const std::string &name)
bool same_file(const std::string &f, const std::string &g)
int octave_vasprintf_wrapper(char **buf, const char *fmt, va_list args)