GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
utils.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_utils_h)
27 #define octave_utils_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cstdarg>
32 
33 #include <iosfwd>
34 #include <string>
35 #include <list>
36 
37 #include "dMatrix.h"
38 #include "lo-utils.h"
39 
40 class octave_value;
41 class octave_value_list;
42 class string_vector;
43 
45 
46 extern OCTINTERP_API bool valid_identifier (const char *s);
47 extern OCTINTERP_API bool valid_identifier (const std::string& s);
48 
49 //! Helper class for `make_valid_name` function calls.
50 //!
51 //! Extracting options separately for multiple (e.g. 1000+) function calls
52 //! avoids expensive repetitive parsing of the very same options.
53 
54 class
55 OCTINTERP_API
57 {
58 public:
59 
60  //! Default options for `make_valid_name` function calls.
61  //!
62  //! Calling the constructor without arguments is equivalent to:
63  //!
64  //! @code{.cc}
65  //! make_valid_name_options (ovl ("ReplacementStyle", "underscore",
66  //! "Prefix", "x"));
67  //! @endcode
68 
70 
71  //! Extract attribute-value-pairs from an octave_value_list of strings.
72  //!
73  //! If attributes occur multiple times, the rightmost pair is chosen.
74  //!
75  //! @code{.cc}
76  //! make_valid_name_options (ovl ("ReplacementStyle", "hex", ...));
77  //! @endcode
78 
80 
81  //! @return ReplacementStyle, see `help matlab.lang.makeValidName`.
82 
83  const std::string&
84  get_replacement_style () const { return m_replacement_style; }
85 
86  //! @return Prefix, see `help matlab.lang.makeValidName`.
87 
88  const std::string& get_prefix () const { return m_prefix; }
89 
90 private:
91 
92  std::string m_replacement_style{"underscore"};
93  std::string m_prefix{"x"};
94 };
95 
96 //! Modify @p str to be a valid variable name.
97 //!
98 //! @param str input string
99 //! @param options see also `help matlab.lang.makeValidName`.
100 //!
101 //! @return true, if @p str was modified.
102 
103 extern OCTINTERP_API bool
104 make_valid_name (std::string& str, const make_valid_name_options& options);
105 
106 extern OCTINTERP_API bool
107 same_file (const std::string& f, const std::string& g);
108 
109 extern OCTINTERP_API int almost_match (const std::string& std,
110  const std::string& s,
111  int min_match_len = 1,
112  int case_sens = 1);
113 
114 extern OCTINTERP_API int
115 keyword_almost_match (const char *const *std, int *min_len,
116  const std::string& s, int min_toks_to_match,
117  int max_toks);
118 
119 extern OCTINTERP_API std::string
120 search_path_for_file (const std::string&, const string_vector&);
121 
122 extern OCTINTERP_API string_vector
123 search_path_for_all_files (const std::string&, const string_vector&);
124 
125 extern OCTINTERP_API std::string
126 file_in_path (const std::string&, const std::string&);
127 
128 extern OCTINTERP_API std::string
129 find_data_file_in_load_path (const std::string& fcn,
130  const std::string& file,
131  bool require_regular_file = false);
132 
133 extern OCTINTERP_API std::string contents_file_in_path (const std::string&);
134 
135 extern OCTINTERP_API std::string fcn_file_in_path (const std::string&);
136 
137 extern OCTINTERP_API std::string do_string_escapes (const std::string& s);
138 
139 extern OCTINTERP_API const char * undo_string_escape (char c);
140 
141 extern OCTINTERP_API std::string undo_string_escapes (const std::string& s);
142 
143 extern OCTINTERP_API void
144 check_dimensions (dim_vector& dim, const char *warnfor);
145 
146 extern OCTINTERP_API void
147 get_dimensions (const octave_value& a, const char *warn_for,
148  dim_vector& dim);
149 
150 extern OCTINTERP_API void
151 get_dimensions (const octave_value& a, const octave_value& b,
152  const char *warn_for, octave_idx_type& nr,
153  octave_idx_type& nc);
154 
155 extern OCTINTERP_API void
156 get_dimensions (const octave_value& a, const char *warn_for,
158 
159 extern OCTINTERP_API octave_idx_type
160 dims_to_numel (const dim_vector& dims, const octave_value_list& idx);
161 
162 extern OCTINTERP_API Matrix
164 
165 extern OCTINTERP_API FloatMatrix
167 
168 extern OCTINTERP_API std::size_t
169 format (std::ostream& os, const char *fmt, ...);
170 
171 extern OCTINTERP_API std::size_t
172 vformat (std::ostream& os, const char *fmt, va_list args);
173 
174 extern OCTINTERP_API std::string
175 vasprintf (const char *fmt, va_list args);
176 
177 extern OCTINTERP_API std::string asprintf (const char *fmt, ...);
178 
179 extern OCTINTERP_API void sleep (double seconds,
180  bool do_graphics_events = false);
181 
182 extern OCTINTERP_API
185  const char *fcn_name, const octave_value_list& args,
186  int nargout);
187 
188 extern OCTINTERP_API
191  const char *fcn_name, const octave_value_list& args);
192 
194 
195 #endif
OCTAVE_END_NAMESPACE(octave)
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
Helper class for make_valid_name function calls.
Definition: utils.h:57
make_valid_name_options()=default
Default options for make_valid_name function calls.
const std::string & get_replacement_style() const
Definition: utils.h:84
const std::string & get_prefix() const
Definition: utils.h:88
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f
OCTINTERP_API std::string file_in_path(const std::string &, const std::string &)
Definition: utils.cc:691
OCTINTERP_API std::string vasprintf(const char *fmt, va_list args)
Definition: utils.cc:1496
OCTINTERP_API int keyword_almost_match(const char *const *std, int *min_len, const std::string &s, int min_toks_to_match, int max_toks)
Definition: utils.cc:423
OCTINTERP_API string_vector search_path_for_all_files(const std::string &, const string_vector &)
Definition: utils.cc:521
OCTINTERP_API int almost_match(const std::string &std, const std::string &s, int min_match_len=1, int case_sens=1)
Definition: utils.cc:408
OCTINTERP_API std::size_t vformat(std::ostream &os, const char *fmt, va_list args)
Definition: utils.cc:1487
OCTINTERP_API void sleep(double seconds, bool do_graphics_events=false)
Definition: utils.cc:1532
OCTINTERP_API std::string undo_string_escapes(const std::string &s)
Definition: utils.cc:1033
OCTINTERP_API std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file=false)
Definition: utils.cc:703
OCTINTERP_API bool valid_identifier(const char *s)
Definition: utils.cc:78
OCTINTERP_API FloatMatrix float_identity_matrix(octave_idx_type nr, octave_idx_type nc)
Definition: utils.cc:1458
OCTINTERP_API bool same_file(const std::string &f, const std::string &g)
Definition: utils.cc:294
OCTINTERP_API Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
Definition: utils.cc:1443
OCTINTERP_API void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
Definition: utils.cc:1342
OCTINTERP_API std::string fcn_file_in_path(const std::string &)
Definition: utils.cc:742
OCTINTERP_API std::string search_path_for_file(const std::string &, const string_vector &)
Definition: utils.cc:511
OCTINTERP_API void check_dimensions(dim_vector &dim, const char *warnfor)
Definition: utils.cc:1324
OCTINTERP_API std::string contents_file_in_path(const std::string &)
Definition: utils.cc:782
OCTINTERP_API std::string asprintf(const char *fmt,...)
Definition: utils.cc:1513
OCTINTERP_API std::size_t format(std::ostream &os, const char *fmt,...)
Definition: utils.cc:1473
OCTINTERP_API bool make_valid_name(std::string &str, const make_valid_name_options &options)
Modify str to be a valid variable name.
Definition: utils.cc:135
OCTINTERP_API 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)
Definition: utils.cc:1663
OCTINTERP_API octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx)
Definition: utils.cc:1404
OCTINTERP_API const char * undo_string_escape(char c)
Definition: utils.cc:986
OCTINTERP_API std::string do_string_escapes(const std::string &s)
Definition: utils.cc:804