26 #if defined (HAVE_CONFIG_H)
80 if (! s || ! (isalpha (*s) || *s ==
'_'))
84 if (! (isalnum (*s) || *s ==
'_'))
95 DEFUN (isvarname, args, ,
105 if (args.length () != 1)
110 if (args(0).is_string ())
143 bool previous =
false;
144 bool any_non_space =
false;
147 c = ((any_non_space && previous && std::isalpha (c)) ? std::toupper (c)
149 previous = std::isspace (c);
150 any_non_space |= (! previous);
154 str.erase (std::remove_if (str.begin(), str.end(),
156 { return std::isspace(x); }),
165 str[0] = std::toupper (str[0]);
170 if (! std::isalpha (str[0]) && str[0] !=
'_')
176 c = (std::isalnum (c) ? c :
'_');
178 str.erase (std::remove_if (str.begin(), str.end(),
180 { return ! std::isalnum (x) && x !=
'_'; }),
184 const std::string permitted_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
185 "abcdefghijklmnopqrstuvwxyz"
188 std::size_t pos = str.find_first_not_of (permitted_chars);
192 while (pos != std::string::npos)
195 std::snprintf (hex_str,
sizeof (hex_str),
"0x%02X", str[pos]);
196 str.replace (pos, 1, hex_str);
199 pos = str.find_first_not_of (permitted_chars,
200 pos +
sizeof (hex_str) - 1);
210 auto nargs = args.
length ();
216 error (
"makeValidName: property/value options must occur in pairs");
218 auto str_to_lower = [] (std::string& s)
222 { return std::tolower(c); });
225 for (
auto i = 0; i < nargs; i = i + 2)
227 std::string parameter = args(i).xstring_value (
"makeValidName: "
228 "option argument must be a string");
229 str_to_lower (parameter);
230 if (parameter ==
"replacementstyle")
233 "'ReplacementStyle' value must be a string");
238 error (
"makeValidName: invalid 'ReplacementStyle' value '%s'",
241 else if (parameter ==
"prefix")
243 m_prefix = args(i + 1).xstring_value (
"makeValidName: "
244 "'Prefix' value must be a string");
247 error (
"makeValidName: invalid 'Prefix' value '%s'",
251 error (
"makeValidName: unknown property '%s'", parameter.c_str ());
255 DEFUN (__make_valid_name__, args, ,
268 auto nargin = args.length ();
274 if (args(0).is_string ())
276 std::string varname = args(0).string_value ();
278 return ovl (varname, is_modified);
280 else if (args(0).iscellstr ())
284 for (
auto i = 0; i < varnames.
numel (); i++)
286 return ovl (varnames, is_modified);
289 error (
"makeValidName: STR must be a string or cellstr");
299 DEFUN (is_same_file, args, ,
324 if (args.length () != 2)
330 bool s1_cellstr = args(0).iscellstr ();
331 bool s2_string = args(1).is_string ();
332 bool s2_cellstr = args(1).iscellstr ();
334 if (s1_string && s2_string)
336 std::string file1 = args(0).string_value ();
337 std::string file2 = args(1).string_value ();
341 else if ((s1_string && s2_cellstr) || (s1_cellstr && s2_string))
348 cellstr_arg = args(1);
353 cellstr_arg = args(0);
362 output(idx) =
same_file (str, cellstr(idx));
366 else if (s1_cellstr && s2_cellstr)
375 error (
"is_same_file: cellstr arrays FILEPATH1 and FILEPATH2 must be the same size");
380 output(idx) =
same_file (cellstr1(idx), cellstr2(idx));
385 error (
"is_same_file: FILEPATH1 and FILEPATH2 must be strings or cell arrays of strings");
409 int min_match_len,
int case_sens)
411 int stdlen = std.length ();
412 int slen = s.length ();
414 return (slen <= stdlen
415 && slen >= min_match_len
417 ? (
strncmp (std.c_str (), s.c_str (), slen) == 0)
424 const std::string& s,
425 int min_toks_to_match,
int max_toks)
429 int toks_matched = 0;
431 if (s.empty () || max_toks < 1)
434 char *kw =
strsave (s.c_str ());
451 const char **to_match =
new const char *[max_toks + 1];
452 const char *
const *s1 = std;
453 const char **s2 = to_match;
460 while ((end = strchr (beg,
' ')) !=
nullptr)
472 if (tok_count >= max_toks)
477 s2[tok_count+1] =
nullptr;
483 if (!
almost_match (*s1, *s2, min_len[toks_matched], 0))
493 status = (toks_matched >= min_toks_to_match);
548 DEFMETHOD (file_in_loadpath, interp, args, ,
571 int nargin = args.length ();
573 if (nargin < 1 || nargin > 2)
577 0).xstring_vector_value (
"file_in_loadpath: FILE argument must be a string");
580 error (
"file_in_loadpath: FILE argument must not be empty");
588 std::string opt = args(
589 1).xstring_value (
"file_in_loadpath: optional second argument must be a string");
592 error (R
"(file_in_loadpath: "all" is only valid second argument)");
644 int nargin = args.length ();
646 if (nargin < 2 || nargin > 3)
649 std::string path = args(0).xstring_value (
"file_in_path: PATH must be a string");
651 string_vector names = args(1).xstring_vector_value (
"file_in_path: FILE argument must be a string");
654 error (
"file_in_path: FILE argument must not be empty");
660 std::string opt = args(2).xstring_value (
"file_in_path: optional third argument must be a string");
663 error (R
"(file_in_path: "all" is only valid third argument)");
691 std::string
file_in_path (
const std::string& name,
const std::string& suffix)
693 std::string nm = name;
695 if (! suffix.empty ())
704 const std::string& file,
705 bool require_regular_file)
707 std::string fname = file;
710 || sys::env::rooted_relative_pathname (fname)))
715 sys::file_stat fs (fname);
718 = fs.exists () && (fs.is_reg () || ! require_regular_file);
746 int len = name.length ();
752 sys::file_stat fs (name);
754 if (fs.exists () && ! fs.is_dir ())
757 else if (
len > 2 && name[
len - 2] ==
'.' && name[
len - 1] ==
'm')
765 std::string fname = name;
766 std::size_t pos = name.find_first_of (
'>');
767 if (pos != std::string::npos)
768 fname = name.substr (0, pos);
790 std::string tcontents
793 sys::file_stat fs (tcontents);
810 std::size_t
len = s.length ();
816 if (s[j] ==
'\\' && j+1 <
len)
870 int tmpi = s[j] -
'0';
873 int digit = s[k] -
'0';
874 if (digit < 0 || digit > 7)
890 if (! isxdigit (s[k]))
896 tmpi += digit -
'a' + 10;
897 else if (digit >=
'A')
898 tmpi += digit -
'A' + 10;
904 warning (R
"(malformed hex escape sequence '\x' -- converting to '\0')");
912 warning (R
"(unrecognized escape sequence '\%c' -- converting to '%c')", s[j], s[j]);
940 if (args.length () != 1)
943 std::string str = args(
944 0).xstring_value (
"do_string_escapes: STRING argument must be of type string");
1025 static char retval[2] {
'\0',
'\0'};
1037 for (std::size_t i = 0; i < s.length (); i++)
1074 if (args.length () != 1)
1077 std::string str = args(0).xstring_value (
"undo_string_escapes: STRING argument must be a string");
1104 DEFUN (is_absolute_filename, args, ,
1111 if (args.length () != 1)
1114 return ovl (args(0).is_string ()
1125 DEFUN (is_rooted_relative_filename, args, ,
1132 if (args.length () != 1)
1135 return ovl (args(0).is_string ()
1136 && sys::env::rooted_relative_pathname (args(0).string_value ()));
1146 DEFUN (make_absolute_filename, args, ,
1157 if (args.length () != 1)
1160 std::string nm = args(0).xstring_value (
"make_absolute_filename: FILE argument must be a filename");
1172 DEFMETHOD (dir_in_loadpath, interp, args, ,
1195 int nargin = args.length ();
1197 if (nargin < 1 || nargin > 2)
1202 dir = args(0).xstring_value (
"dir_in_loadpath: DIR must be a directory name");
1204 load_path& lp = interp.get_load_path ();
1249 int nargin = args.length ();
1258 if (args(0).is_string ())
1266 int val = args(0).xint_value (
"errno: argument must be string or integer");
1292 DEFUN (errno_list, args, ,
1299 if (args.length () != 0)
1312 const char *warnfor)
1314 if (nr < 0 || nc < 0)
1317 "%s: converting negative dimension to zero", warnfor);
1319 nr = (nr < 0) ? 0 : nr;
1320 nc = (nc < 0) ? 0 : nc;
1328 for (
int i = 0; i < dim.
ndims (); i++)
1339 "%s: converting negative dimension to zero", warnfor);
1348 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1383 if ((nr != 1 || nc != 2) && (nr != 2 || nc != 1))
1384 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1412 retval = dims.
numel ();
1423 retval *= idxi.
numel ();
1430 retval *= jdx.
length (dv(i));
1434 error (
"dims_to_numel: invalid index %s", ie.what ());
1447 if (nr > 0 && nc > 0)
1462 if (nr > 0 && nc > 0)
1473 std::size_t
format (std::ostream& os,
const char *fmt, ...)
1478 va_start (args, fmt);
1480 retval =
vformat (os, fmt, args);
1487 std::size_t
vformat (std::ostream& os,
const char *fmt, va_list args)
1518 va_start (args, fmt);
1532 void sleep (
double seconds,
bool do_graphics_events)
1542 if (do_graphics_events)
1551 struct timespec one_tenth = { 0, 100000000 };
1559 if (do_graphics_events)
1560 gh_mgr.process_events ();
1568 double end_time = now.double_value () + seconds;
1569 double remaining_time = seconds;
1573 struct timespec nano_laps = { 0, 100000000 };
1575 while (remaining_time > 0.1)
1579 if (do_graphics_events)
1581 gh_mgr.process_events ();
1584 remaining_time = end_time - now.double_value ();
1586 if (remaining_time < 0.1)
1593 remaining_time = end_time - now.double_value ();
1596 if (remaining_time > 0.0)
1598 nano_laps = { 0,
static_cast<int> (remaining_time * 1e9) };
1622 int nargin = args.length ();
1624 if (nargin < 1 || nargin > 2)
1629 n = args(1).idx_type_value ();
1635 idx_vector idx = args(0).index_vector (
true);
1644 interp.recover_from_exception ();
1669 int nargin = args.
length ();
1674 const Cell *ccells = cells;
1679 for (
int i = 0; i < nargin; i++)
1682 iscell[i] = arg.
iscell ();
1690 new_args(i) = ccells[i](0);
1692 else if (
numel == 1)
1695 dims = ccells[i].
dims ();
1697 else if (dims != ccells[i].dims ())
1698 error (
"%s: cell arguments must have matching sizes", fcn_name);
1702 for (
int i = 0; i < nargout; i++)
1703 rcells[i].clear (dims);
1707 for (
int i = 0; i < nargin; i++)
1709 new_args(i) = ccells[i](j);
1715 if (tmp.
length () < nargout)
1716 error (
"%s: do_simple_cellfun: internal error", fcn_name);
1718 for (
int i = 0; i < nargout; i++)
1719 rcells[i](j) = tmp(i);
1724 for (
int i = 0; i < nargout; i++)
1725 retval(i) = rcells[i];
1744 DEFUN (isstudent, args, ,
1753 if (args.length () != 0)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
charNDArray min(char d, const charNDArray &m)
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API 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 std::list< std::string > &names)
std::list< std::string > find_all_first_of(const std::list< std::string > &names)
octave_idx_type length(octave_idx_type n=0) const
octave_idx_type extent(octave_idx_type n) const
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.
std::string m_replacement_style
make_valid_name_options()=default
Default options for make_valid_name function calls.
const std::string & get_replacement_style() const
const std::string & get_prefix() 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
std::string string_value(bool force=false) const
Array< std::string > cellstr_value(void) const
bool is_magic_colon(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
dim_vector dims(void) const
octave_idx_type numel(void) const
std::list< std::string > std_list(void) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
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)
load_path & __get_load_path__(void)
gh_manager & __get_gh_manager__(void)
bool iskeyword(const std::string &s)
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
static char * strsave(const char *s)
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_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.
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
static bool absolute_pathname(const std::string &s)
OCTAVE_API int octave_strncasecmp(const char *s1, const char *s2, size_t n)
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)
bool valid_identifier(const char *s)
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)
bool make_valid_name(std::string &str, const make_valid_name_options &options)
Modify str to be a valid variable name.
const char * undo_string_escape(char c)
std::string search_path_for_file(const std::string &path, const string_vector &names)
octave_value_list do_simple_cellfun(octave_value_list(*fcn)(const octave_value_list &, int), const char *fcn_name, const octave_value_list &args, int nargout)
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)