GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ls-oct-text.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2025 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_ls_oct_text_h)
27#define octave_ls_oct_text_h 1
28
29#include "octave-config.h"
30
31#include <limits>
32#include <sstream>
33#include <string>
34
35#include "str-vec.h"
36
37#include "ls-ascii-helper.h"
38
39class octave_value;
40
41// Flag for cell elements
42#define CELL_ELT_TAG "<cell-element>"
43
44// Used when converting Inf to something that gnuplot can read.
45
46#if ! defined (OCT_RBV)
47# define OCT_RBV (std::numeric_limits<double>::max () / 100.0)
48#endif
49
50extern OCTINTERP_API std::string
51extract_keyword (std::istream& is, const char *keyword,
52 const bool next_only = false);
53
54extern OCTINTERP_API std::string
55read_text_data (std::istream& is, const std::string& filename, bool& global,
57 const bool do_name_validation = true);
58
59extern OCTINTERP_API bool
60save_text_data (std::ostream& os, const octave_value& val_arg,
61 const std::string& name, bool mark_global, int precision);
62
63extern OCTINTERP_API bool
64save_text_data_for_plotting (std::ostream& os, const octave_value& t,
65 const std::string& name);
66
67extern OCTINTERP_API bool
68save_three_d (std::ostream& os, const octave_value& t,
69 bool parametric = false);
70
71// Match KEYWORD on stream IS, placing the associated value in VALUE,
72// returning TRUE if successful and FALSE otherwise.
73//
74// Input should look something like:
75//
76// [%#][ \t]*keyword[ \t]*int-value.*\n
77
78template <typename T>
79bool
80extract_keyword (std::istream& is, const char *keyword, T& value,
81 const bool next_only = false)
82{
83 bool status = false;
84 value = T ();
85
86 char c;
87 while (is.get (c))
88 {
89 if (c == '%' || c == '#')
90 {
91 std::ostringstream buf;
92
93 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
94 ; // Skip whitespace and comment characters.
95
96 if (isalpha (c))
97 buf << c;
98
99 while (is.get (c) && isalpha (c))
100 buf << c;
101
102 std::string tmp = buf.str ();
103 bool match = (tmp.substr (0, strlen (keyword)) == keyword);
104
105 if (match)
106 {
107 while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
108 ; // Skip whitespace and the colon.
109
110 is.putback (c);
111 if (c != '\n' && c != '\r')
112 is >> value;
113 if (is)
114 status = true;
115 octave::skip_until_newline (is, false);
116 break;
117 }
118 else if (next_only)
119 break;
120 }
121 }
122 return status;
123}
124
125template <typename T>
126bool
127extract_keyword (std::istream& is, const std::string& kw, T& value,
128 const bool next_only = false)
129{
130 return extract_keyword (is, kw.c_str (), value, next_only);
131}
132
133// Match one of the elements in KEYWORDS on stream IS, placing the
134// matched keyword in KW and the associated value in VALUE,
135// returning TRUE if successful and FALSE otherwise.
136//
137// Input should look something like:
138//
139// [%#][ \t]*keyword[ \t]*int-value.*\n
140
141template <typename T>
142bool
143extract_keyword (std::istream& is, const string_vector& keywords,
144 std::string& kw, T& value, const bool next_only = false)
145{
146 bool status = false;
147 kw = "";
148 value = 0;
149
150 char c;
151 while (is.get (c))
152 {
153 if (c == '%' || c == '#')
154 {
155 std::ostringstream buf;
156
157 while (is.get (c) && (c == ' ' || c == '\t' || c == '%' || c == '#'))
158 ; // Skip whitespace and comment characters.
159
160 if (isalpha (c))
161 buf << c;
162
163 while (is.get (c) && isalpha (c))
164 buf << c;
165
166 std::string tmp = buf.str ();
167
168 for (int i = 0; i < keywords.numel (); i++)
169 {
170 int match = (tmp == keywords[i]);
171
172 if (match)
173 {
174 kw = keywords[i];
175
176 while (is.get (c) && (c == ' ' || c == '\t' || c == ':'))
177 ; // Skip whitespace and the colon.
178
179 is.putback (c);
180 if (c != '\n' && c != '\r')
181 is >> value;
182 if (is)
183 status = true;
184 octave::skip_until_newline (is, false);
185 return status;
186 }
187 }
188
189 if (next_only)
190 break;
191 }
192 }
193 return status;
194}
195
196#endif
octave_idx_type numel() const
Definition str-vec.h:98
bool save_text_data_for_plotting(std::ostream &os, const octave_value &t, const std::string &name)
bool save_text_data(std::ostream &os, const octave_value &val_arg, const std::string &name, bool mark_global, int precision)
std::string read_text_data(std::istream &is, const std::string &filename, bool &global, octave_value &tc, octave_idx_type count, const bool do_name_validation=true)
std::string extract_keyword(std::istream &is, const char *keyword, const bool next_only=false)
bool save_three_d(std::ostream &os, const octave_value &t, bool parametric=false)
T::size_type strlen(const typename T::value_type *str)
Definition oct-string.cc:88