26 #if defined (HAVE_CONFIG_H)
81 if (! s || ! (isalpha (*s) || *s ==
'_'))
85 if (! (isalnum (*s) || *s ==
'_'))
97 DEFUN (isvarname, args, ,
107 if (args.length () != 1)
112 if (args(0).is_string ())
145 bool previous =
false;
146 bool any_non_space =
false;
149 c = ((any_non_space && previous && std::isalpha (c)) ? std::toupper (c)
151 previous = std::isspace (c);
152 any_non_space |= (! previous);
156 str.erase (std::remove_if (str.begin(), str.end(),
158 { return std::isspace(x); }),
167 str[0] = std::toupper (str[0]);
172 if (! std::isalpha (str[0]) && str[0] !=
'_')
178 c = (std::isalnum (c) ? c :
'_');
180 str.erase (std::remove_if (str.begin(), str.end(),
182 { return ! std::isalnum (x) && x !=
'_'; }),
186 const std::string permitted_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
187 "abcdefghijklmnopqrstuvwxyz"
190 std::size_t pos = str.find_first_not_of (permitted_chars);
194 while (pos != std::string::npos)
197 std::snprintf (hex_str,
sizeof (hex_str),
"0x%02X", str[pos]);
198 str.replace (pos, 1, hex_str);
201 pos = str.find_first_not_of (permitted_chars,
202 pos +
sizeof (hex_str) - 1);
212 auto nargs = args.
length ();
218 error (
"makeValidName: property/value options must occur in pairs");
220 auto str_to_lower = [] (std::string& s)
224 { return std::tolower(c); });
227 for (
auto i = 0; i < nargs; i = i + 2)
229 std::string parameter = args(i).xstring_value (
"makeValidName: "
230 "option argument must be a string");
231 str_to_lower (parameter);
232 if (parameter ==
"replacementstyle")
234 m_replacement_style = args(i + 1).xstring_value (
"makeValidName: "
235 "'ReplacementStyle' value must be a string");
236 str_to_lower (m_replacement_style);
237 if ((m_replacement_style !=
"underscore")
238 && (m_replacement_style !=
"delete")
239 && (m_replacement_style !=
"hex"))
240 error (
"makeValidName: invalid 'ReplacementStyle' value '%s'",
241 m_replacement_style.c_str ());
243 else if (parameter ==
"prefix")
245 m_prefix = args(i + 1).xstring_value (
"makeValidName: "
246 "'Prefix' value must be a string");
249 error (
"makeValidName: invalid 'Prefix' value '%s'",
253 error (
"makeValidName: unknown property '%s'", parameter.c_str ());
257 DEFUN (__make_valid_name__, args, ,
270 auto nargin = args.length ();
276 if (args(0).is_string ())
278 std::string varname = args(0).string_value ();
280 return ovl (varname, is_modified);
282 else if (args(0).iscellstr ())
286 for (
auto i = 0; i < varnames.
numel (); i++)
288 return ovl (varnames, is_modified);
291 error (
"makeValidName: STR must be a string or cellstr");
302 DEFUN (is_same_file, args, ,
327 if (args.length () != 2)
333 bool s1_cellstr = args(0).iscellstr ();
334 bool s2_string = args(1).is_string ();
335 bool s2_cellstr = args(1).iscellstr ();
337 if (s1_string && s2_string)
339 std::string file1 = args(0).string_value ();
340 std::string file2 = args(1).string_value ();
344 else if ((s1_string && s2_cellstr) || (s1_cellstr && s2_string))
351 cellstr_arg = args(1);
356 cellstr_arg = args(0);
369 else if (s1_cellstr && s2_cellstr)
378 error (
"is_same_file: cellstr arrays FILEPATH1 and FILEPATH2 must be the same size");
388 error (
"is_same_file: FILEPATH1 and FILEPATH2 must be strings or cell arrays of strings");
413 int min_match_len,
int case_sens)
415 int stdlen = std.length ();
416 int slen = s.length ();
418 return (slen <= stdlen
419 && slen >= min_match_len
421 ? (
strncmp (std.c_str (), s.c_str (), slen) == 0)
429 const std::string& s,
430 int min_toks_to_match,
int max_toks)
434 int toks_matched = 0;
436 if (s.empty () || max_toks < 1)
439 char *kw =
strsave (s.c_str ());
456 const char **to_match =
new const char *[max_toks + 1];
457 const char *
const *s1 = std;
458 const char **s2 = to_match;
465 while ((end = strchr (beg,
' ')) !=
nullptr)
477 if (tok_count >= max_toks)
482 s2[tok_count+1] =
nullptr;
488 if (!
almost_match (*s1, *s2, min_len[toks_matched], 0))
498 status = (toks_matched >= min_toks_to_match);
538 sv[i] = sys::env::make_absolute (sv[i]);
551 retval[i] = sys::env::make_absolute (sv[i]);
556 DEFMETHOD (file_in_loadpath, interp, args, ,
579 int nargin = args.length ();
581 if (nargin < 1 || nargin > 2)
584 string_vector names = args(0).xstring_vector_value (
"file_in_loadpath: FILE argument must be a string");
587 error (
"file_in_loadpath: FILE argument must not be empty");
595 std::string opt = args(1).xstring_value (
"file_in_loadpath: optional second argument must be a string");
598 error (R
"(file_in_loadpath: "all" is only valid second argument)");
650 int nargin = args.length ();
652 if (nargin < 2 || nargin > 3)
655 std::string path = args(0).xstring_value (
"file_in_path: PATH must be a string");
657 string_vector names = args(1).xstring_vector_value (
"file_in_path: FILE argument must be a string");
660 error (
"file_in_path: FILE argument must not be empty");
666 std::string opt = args(2).xstring_value (
"file_in_path: optional third argument must be a string");
669 error (R
"(file_in_path: "all" is only valid third argument)");
700 std::string nm = name;
702 if (! suffix.empty ())
707 return sys::env::make_absolute (lp.
find_file (nm));
712 const std::string& file,
713 bool require_regular_file)
715 std::string fname = file;
717 if (! (sys::env::absolute_pathname (fname)
718 || sys::env::rooted_relative_pathname (fname)))
731 std::string tmp = sys::env::make_absolute (lp.
find_file (fname));
753 int len = name.length ();
757 if (sys::env::absolute_pathname (name))
762 else if (
len > 2 && name[
len - 2] ==
'.' && name[
len - 1] ==
'm')
770 std::string fname = name;
771 std::size_t pos = name.find_first_of (
'>');
772 if (pos != std::string::npos)
773 fname = name.substr (0, pos);
796 std::string tcontents
800 retval = sys::env::make_absolute (tcontents);
815 std::size_t
len = s.length ();
821 if (s[j] ==
'\\' && j+1 <
len)
875 int tmpi = s[j] -
'0';
878 int digit = s[k] -
'0';
879 if (digit < 0 || digit > 7)
895 if (! isxdigit (s[k]))
901 tmpi += digit -
'a' + 10;
902 else if (digit >=
'A')
903 tmpi += digit -
'A' + 10;
909 warning (R
"(malformed hex escape sequence '\x' -- converting to '\0')");
917 warning (R
"(unrecognized escape sequence '\%c' -- converting to '%c')", s[j], s[j]);
945 if (args.length () != 1)
948 std::string str = args(0).xstring_value (
"do_string_escapes: STRING argument must be of type string");
1027 static char retval[2] {
'\0',
'\0'};
1040 for (std::size_t i = 0; i < s.length (); i++)
1077 if (args.length () != 1)
1080 std::string str = args(0).xstring_value (
"undo_string_escapes: STRING argument must be a string");
1107 DEFUN (is_absolute_filename, args, ,
1114 if (args.length () != 1)
1117 return ovl (args(0).is_string ()
1118 && sys::env::absolute_pathname (args(0).string_value ()));
1128 DEFUN (is_rooted_relative_filename, args, ,
1135 if (args.length () != 1)
1138 return ovl (args(0).is_string ()
1139 && sys::env::rooted_relative_pathname (args(0).string_value ()));
1149 DEFUN (make_absolute_filename, args, ,
1160 if (args.length () != 1)
1163 std::string nm = args(0).xstring_value (
"make_absolute_filename: FILE argument must be a filename");
1165 return ovl (sys::env::make_absolute (nm));
1175 DEFMETHOD (dir_in_loadpath, interp, args, ,
1198 int nargin = args.length ();
1200 if (nargin < 1 || nargin > 2)
1205 dir = args(0).xstring_value (
"dir_in_loadpath: DIR must be a directory name");
1207 load_path& lp = interp.get_load_path ();
1252 int nargin = args.length ();
1261 if (args(0).is_string ())
1269 int val = args(0).xint_value (
"errno: argument must be string or integer");
1295 DEFUN (errno_list, args, ,
1302 if (args.length () != 0)
1316 const char *warnfor)
1318 if (nr < 0 || nc < 0)
1321 "%s: converting negative dimension to zero", warnfor);
1323 nr = (nr < 0) ? 0 : nr;
1324 nc = (nc < 0) ? 0 : nc;
1333 for (
int i = 0; i < dim.
ndims (); i++)
1344 "%s: converting negative dimension to zero", warnfor);
1354 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1390 if ((nr != 1 || nc != 2) && (nr != 2 || nc != 1))
1391 error (
"%s (A): use %s (size (A)) instead", warn_for, warn_for);
1421 retval = dims.
numel ();
1432 retval *= idxi.
numel ();
1439 retval *= jdx.
length (dv(i));
1443 error (
"dims_to_numel: invalid index %s", ie.what ());
1457 if (nr > 0 && nc > 0)
1473 if (nr > 0 && nc > 0)
1485 format (std::ostream& os,
const char *fmt, ...)
1490 va_start (args, fmt);
1492 retval =
vformat (os, fmt, args);
1500 vformat (std::ostream& os,
const char *fmt, va_list args)
1533 va_start (args, fmt);
1548 sleep (
double seconds,
bool do_graphics_events)
1558 if (do_graphics_events)
1567 struct timespec one_tenth = { 0, 100000000 };
1575 if (do_graphics_events)
1584 double end_time = now.double_value () + seconds;
1585 double remaining_time = seconds;
1589 struct timespec nano_laps = { 0, 100000000 };
1591 while (remaining_time > 0.1)
1595 if (do_graphics_events)
1600 remaining_time = end_time - now.double_value ();
1602 if (remaining_time < 0.1)
1609 remaining_time = end_time - now.double_value ();
1612 if (remaining_time > 0.0)
1614 nano_laps = { 0,
static_cast<int> (remaining_time * 1e9) };
1638 int nargin = args.length ();
1640 if (nargin < 1 || nargin > 2)
1645 n = args(1).idx_type_value ();
1651 idx_vector idx = args(0).index_vector (
true);
1660 interp.recover_from_exception ();
1685 int nargin = args.
length ();
1690 const Cell *ccells = cells;
1695 for (
int i = 0; i < nargin; i++)
1698 iscell[i] = arg.
iscell ();
1706 new_args(i) = ccells[i](0);
1708 else if (
numel == 1)
1711 dims = ccells[i].
dims ();
1713 else if (dims != ccells[i].dims ())
1714 error (
"%s: cell arguments must have matching sizes", fcn_name);
1718 for (
int i = 0; i < nargout; i++)
1719 rcells[i].clear (dims);
1723 for (
int i = 0; i < nargin; i++)
1725 new_args(i) = ccells[i](j);
1731 if (tmp.
length () < nargout)
1732 error (
"%s: do_simple_cellfun: internal error", fcn_name);
1734 for (
int i = 0; i < nargout; i++)
1735 rcells[i](j) = tmp(i);
1740 for (
int i = 0; i < nargout; i++)
1741 retval(i) = rcells[i];
1760 DEFUN (isstudent, args, ,
1769 if (args.length () != 0)
1781 OCTAVE_END_NAMESPACE(
octave)
ComplexNDArray concat(NDArray &ra, ComplexNDArray &rb, const Array< octave_idx_type > &ra_idx)
octave_value_list Ferrno(const octave_value_list &=octave_value_list(), int=0)
charNDArray min(char d, const charNDArray &m)
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
octave_idx_type numel() const
Number of elements in the array.
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)
octave_idx_type ndims() const
Number of dimensions.
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)
int process_events(bool force=false)
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.
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 octave_scalar_map list()
static int lookup(const std::string &name)
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
octave_idx_type length() const
octave_idx_type rows() const
octave::idx_vector index_vector(bool require_integers=false) const
bool is_scalar_type() const
octave_idx_type idx_type_value(bool req_int=false, bool frc_str_conv=false) const
Array< octave_idx_type > octave_idx_type_vector_value(bool req_int=false, bool frc_str_conv=false, bool frc_vec_conv=false) const
bool is_magic_colon() const
octave_idx_type numel() const
std::string string_value(bool force=false) const
octave_idx_type columns() const
Array< std::string > cellstr_value() const
octave_idx_type numel() const
std::list< std::string > std_list() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#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)
gh_manager & __get_gh_manager__()
load_path & __get_load_path__()
bool iskeyword(const std::string &s)
F77_RET_T const F77_DBLE * x
F77_RET_T const F77_DBLE const F77_DBLE * f
bool file_exists(const std::string &filename, bool is_dir)
char * strsave(const char *s)
Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
bool valid_identifier(const std::string &s)
octave_value do_simple_cellfun(octave_value_list(*fcn)(const octave_value_list &, int), const char *fcn_name, const octave_value_list &args)
bool same_file(const std::string &f, const std::string &g)
void get_dimensions(const octave_value &a, const octave_value &b, const char *warn_for, octave_idx_type &nr, octave_idx_type &nc)
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)
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.
int octave_strncasecmp(const char *s1, const char *s2, size_t n)
int kbhit(bool wait=true)
std::size_t vformat(std::ostream &os, const char *fmt, va_list args)
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file=false)
std::string asprintf(const char *fmt,...)
std::string do_string_escapes(const std::string &s)
octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx)
std::string undo_string_escapes(const std::string &s)
int almost_match(const std::string &std, const std::string &s, int min_match_len=1, int case_sens=1)
std::string file_in_path(const std::string &, const std::string &)
string_vector search_path_for_all_files(const std::string &, const string_vector &)
std::string contents_file_in_path(const std::string &)
int keyword_almost_match(const char *const *std, int *min_len, const std::string &s, int min_toks_to_match, int max_toks)
std::string search_path_for_file(const std::string &, const string_vector &)
std::string vasprintf(const char *fmt, va_list args)
FloatMatrix float_identity_matrix(octave_idx_type nr, octave_idx_type nc)
std::size_t format(std::ostream &os, const char *fmt,...)
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 fcn_file_in_path(const std::string &)
void sleep(double seconds, bool do_graphics_events=false)
void check_dimensions(dim_vector &dim, const char *warnfor)
int octave_vasprintf_wrapper(char **buf, const char *fmt, va_list args)