GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1993-2026 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
40class octave_value;
42class string_vector;
43
45
46extern OCTINTERP_API bool valid_identifier (const char *s);
47extern 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
54class OCTINTERP_API make_valid_name_options
55{
56public:
57
58 //! Default options for `make_valid_name` function calls.
59 //!
60 //! Calling the constructor without arguments is equivalent to:
61 //!
62 //! @code{.cc}
63 //! make_valid_name_options (ovl ("ReplacementStyle", "underscore",
64 //! "Prefix", "x"));
65 //! @endcode
66
68
69 OCTAVE_DEFAULT_COPY_MOVE_DELETE (make_valid_name_options)
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
90private:
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
103extern OCTINTERP_API bool
104make_valid_name (std::string& str, const make_valid_name_options& options);
105
106extern OCTINTERP_API int almost_match (const std::string& std,
107 const std::string& s,
108 int min_match_len = 1,
109 int case_sens = 1);
110
111extern OCTINTERP_API int
112keyword_almost_match (const char *const *std, int *min_len,
113 const std::string& s, int min_toks_to_match,
114 int max_toks);
115
116extern OCTINTERP_API std::string
117search_path_for_file (const std::string&, const string_vector&);
118
119extern OCTINTERP_API string_vector
120search_path_for_all_files (const std::string&, const string_vector&);
121
122extern OCTINTERP_API std::string
123file_in_path (const std::string&, const std::string&);
124
125extern OCTINTERP_API std::string
126find_data_file_in_load_path (const std::string& fcn,
127 const std::string& file,
128 bool require_regular_file = false);
129
130extern OCTINTERP_API std::string contents_file_in_path (const std::string&);
131
132extern OCTINTERP_API std::string fcn_file_in_path (const std::string&);
133
134extern OCTINTERP_API void
135display_file_lines (std::ostream& os, const std::string& file_name, int start, int end, int target_line, const std::string& marker, const std::string& who);
136
137extern OCTINTERP_API std::string do_string_escapes (const std::string& s);
138
139extern OCTINTERP_API const char * undo_string_escape (char c);
140
141extern OCTINTERP_API std::string undo_string_escapes (const std::string& s);
142
143extern OCTINTERP_API void
144check_dimensions (dim_vector& dim, const char *warnfor);
145
146extern OCTINTERP_API void
147get_dimensions (const octave_value& a, const char *warn_for,
148 dim_vector& dim);
149
150extern OCTINTERP_API void
151get_dimensions (const octave_value& a, const octave_value& b,
152 const char *warn_for, octave_idx_type& nr,
153 octave_idx_type& nc);
154
155extern OCTINTERP_API void
156get_dimensions (const octave_value& a, const char *warn_for,
158
159extern OCTINTERP_API octave_idx_type
160dims_to_numel (const dim_vector& dims, const octave_value_list& idx);
161
162extern OCTINTERP_API Matrix
164
165extern OCTINTERP_API FloatMatrix
167
168extern OCTINTERP_API std::size_t
169format (std::ostream& os, const char *fmt, ...);
170
171extern OCTINTERP_API std::size_t
172vformat (std::ostream& os, const char *fmt, va_list args);
173
174extern OCTINTERP_API std::string
175vasprintf (const char *fmt, va_list args);
176
177extern OCTINTERP_API std::string asprintf (const char *fmt, ...);
178
179extern OCTINTERP_API void sleep (double seconds,
180 bool do_graphics_events = false);
181
182extern OCTINTERP_API
185 const char *fcn_name, const octave_value_list& args,
186 int nargout);
187
188extern OCTINTERP_API
191 const char *fcn_name, const octave_value_list& args);
192
193OCTAVE_END_NAMESPACE(octave)
194
195#endif
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
Helper class for make_valid_name function calls.
Definition utils.h:55
const std::string & get_replacement_style() const
Definition utils.h:84
make_valid_name_options()=default
Default options for make_valid_name function calls.
const std::string & get_prefix() const
Definition utils.h:88
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::size_t vformat(std::ostream &os, const char *fmt, va_list args)
Definition utils.cc:1529
std::string find_data_file_in_load_path(const std::string &fcn, const std::string &file, bool require_regular_file=false)
Definition utils.cc:711
std::string asprintf(const char *fmt,...)
Definition utils.cc:1557
std::string do_string_escapes(const std::string &s)
Definition utils.cc:838
octave_idx_type dims_to_numel(const dim_vector &dims, const octave_value_list &idx)
Definition utils.cc:1442
std::string undo_string_escapes(const std::string &s)
Definition utils.cc:1065
int almost_match(const std::string &std, const std::string &s, int min_match_len=1, int case_sens=1)
Definition utils.cc:412
std::string file_in_path(const std::string &, const std::string &)
Definition utils.cc:698
string_vector search_path_for_all_files(const std::string &, const string_vector &)
Definition utils.cc:528
std::string contents_file_in_path(const std::string &)
Definition utils.cc:788
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:428
std::string search_path_for_file(const std::string &, const string_vector &)
Definition utils.cc:517
Matrix identity_matrix(octave_idx_type nr, octave_idx_type nc)
Definition utils.cc:1482
std::string vasprintf(const char *fmt, va_list args)
Definition utils.cc:1539
void get_dimensions(const octave_value &a, const char *warn_for, dim_vector &dim)
Definition utils.cc:1377
bool valid_identifier(const char *s)
Definition utils.cc:79
FloatMatrix float_identity_matrix(octave_idx_type nr, octave_idx_type nc)
Definition utils.cc:1498
std::size_t format(std::ostream &os, const char *fmt,...)
Definition utils.cc:1514
const char * undo_string_escape(char c)
Definition utils.cc:1020
void display_file_lines(std::ostream &os, const std::string &file_name, int start, int end, int target_line, const std::string &marker, const std::string &who)
Definition utils.cc:807
bool make_valid_name(std::string &str, const make_valid_name_options &options)
Modify str to be a valid variable name.
Definition utils.cc:137
std::string fcn_file_in_path(const std::string &)
Definition utils.cc:749
void sleep(double seconds, bool do_graphics_events=false)
Definition utils.cc:1577
void check_dimensions(dim_vector &dim, const char *warnfor)
Definition utils.cc:1358
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:1708