GNU Octave  9.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-2024 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  OCTAVE_DEFAULT_COPY_MOVE_DELETE (make_valid_name_options)
72 
73  //! Extract attribute-value-pairs from an octave_value_list of strings.
74  //!
75  //! If attributes occur multiple times, the rightmost pair is chosen.
76  //!
77  //! @code{.cc}
78  //! make_valid_name_options (ovl ("ReplacementStyle", "hex", ...));
79  //! @endcode
80 
82 
83  //! @return ReplacementStyle, see `help matlab.lang.makeValidName`.
84 
85  const std::string&
86  get_replacement_style () const { return m_replacement_style; }
87 
88  //! @return Prefix, see `help matlab.lang.makeValidName`.
89 
90  const std::string& get_prefix () const { return m_prefix; }
91 
92 private:
93 
94  std::string m_replacement_style{"underscore"};
95  std::string m_prefix{"x"};
96 };
97 
98 //! Modify @p str to be a valid variable name.
99 //!
100 //! @param str input string
101 //! @param options see also `help matlab.lang.makeValidName`.
102 //!
103 //! @return true, if @p str was modified.
104 
105 extern OCTINTERP_API bool
106 make_valid_name (std::string& str, const make_valid_name_options& options);
107 
108 OCTAVE_DEPRECATED (9, "octave::same_file is obsolete, use octave::sys::same_file")
109 extern OCTINTERP_API bool
110 same_file (const std::string& f, const std::string& g);
111 
112 extern OCTINTERP_API int almost_match (const std::string& std,
113  const std::string& s,
114  int min_match_len = 1,
115  int case_sens = 1);
116 
117 extern OCTINTERP_API int
118 keyword_almost_match (const char *const *std, int *min_len,
119  const std::string& s, int min_toks_to_match,
120  int max_toks);
121 
122 extern OCTINTERP_API std::string
123 search_path_for_file (const std::string&, const string_vector&);
124 
125 extern OCTINTERP_API string_vector
126 search_path_for_all_files (const std::string&, const string_vector&);
127 
128 extern OCTINTERP_API std::string
129 file_in_path (const std::string&, const std::string&);
130 
131 extern OCTINTERP_API std::string
132 find_data_file_in_load_path (const std::string& fcn,
133  const std::string& file,
134  bool require_regular_file = false);
135 
136 extern OCTINTERP_API std::string contents_file_in_path (const std::string&);
137 
138 extern OCTINTERP_API std::string fcn_file_in_path (const std::string&);
139 
140 extern OCTINTERP_API std::string do_string_escapes (const std::string& s);
141 
142 extern OCTINTERP_API const char * undo_string_escape (char c);
143 
144 extern OCTINTERP_API std::string undo_string_escapes (const std::string& s);
145 
146 extern OCTINTERP_API void
147 check_dimensions (dim_vector& dim, const char *warnfor);
148 
149 extern OCTINTERP_API void
150 get_dimensions (const octave_value& a, const char *warn_for,
151  dim_vector& dim);
152 
153 extern OCTINTERP_API void
155  const char *warn_for, octave_idx_type& nr,
156  octave_idx_type& nc);
157 
158 extern OCTINTERP_API void
159 get_dimensions (const octave_value& a, const char *warn_for,
161 
162 extern OCTINTERP_API octave_idx_type
163 dims_to_numel (const dim_vector& dims, const octave_value_list& idx);
164 
165 extern OCTINTERP_API Matrix
167 
168 extern OCTINTERP_API FloatMatrix
170 
171 extern OCTINTERP_API std::size_t
172 format (std::ostream& os, const char *fmt, ...);
173 
174 extern OCTINTERP_API std::size_t
175 vformat (std::ostream& os, const char *fmt, va_list args);
176 
177 extern OCTINTERP_API std::string
178 vasprintf (const char *fmt, va_list args);
179 
180 extern OCTINTERP_API std::string asprintf (const char *fmt, ...);
181 
182 extern OCTINTERP_API void sleep (double seconds,
183  bool do_graphics_events = false);
184 
185 extern OCTINTERP_API
188  const char *fcn_name, const octave_value_list& args,
189  int nargout);
190 
191 extern OCTINTERP_API
194  const char *fcn_name, const octave_value_list& args);
195 
196 OCTAVE_END_NAMESPACE(octave)
197 
198 #endif
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:86
const std::string & get_prefix() const
Definition: utils.h:90
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
F77_RET_T const F77_DBLE const F77_DBLE * f
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 &)
Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
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,...)
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)
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)
bool same_file(const std::string &f, const std::string &g)
Definition: lo-sysdep.cc:437