Loading...
Searching...
No Matches
tokenIO.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2015 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "token.H"
31#include "IOstreams.H"
32
33// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
34
35namespace Foam
36{
37template<class OS>
38static OS& printTokenInfo(OS& os, const token& tok)
39{
40 os << "on line " << tok.lineNumber() << ": ";
41
42 switch (tok.type())
43 {
45 os << "undefined token";
46 break;
47
49 os << "bool '" << (tok.boolToken() ? "true" : "false") << '\'';
50 break;
51
53 os << "flag '" << int(tok.flagToken()) << '\'';
54 break;
55
57 os << "punctuation '" << tok.pToken() << '\'';
58 break;
59
61 os << "int32 " << tok.int32Token();
62 break;
63
65 os << "int64 " << tok.int64Token();
66 break;
67
69 os << "uint32 " << tok.uint32Token();
70 break;
71
73 os << "uint64 " << tok.uint64Token();
74 break;
75
77 os << "float " << tok.floatToken();
78 break;
79
81 os << "double " << tok.doubleToken();
82 break;
83
85 os << "word '" << tok.wordToken() << '\'';
86 break;
87
89 os << "directive '" << tok.wordToken() << '\'';
90 break;
91
93 os << "string " << tok.stringToken();
94 break;
95
97 os << "expression " << tok.stringToken();
98 break;
99
101 os << "variable " << tok.stringToken();
102 break;
103
105 os << "verbatim " << tok.stringToken();
106 break;
107
109 os << "char_data " << tok.stringToken();
110 break;
111
113 {
114 if (tok.compoundToken().pending())
115 {
116 os << "pending ";
117 }
118 if (tok.compoundToken().moved())
119 {
120 os << "moved ";
121 }
122 os << "compound of type "
123 << tok.compoundToken().type();
124 }
125 break;
126
128 os << "error";
129 break;
130
131 default:
132 os << "unknown token type '" << int(tok.type()) << '\'';
133 break;
134 }
135
136 return os;
137}
138} // End namespace Foam
139
140
141// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
142
143Foam::token::token(Istream& is)
144:
145 token()
147 is.read(*this);
148}
149
150
151// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
152
154{
155 reset();
156 if (is.good())
157 {
158 is.read(*this);
160 return good();
161}
162
163
164// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
165
167{
168 switch (tokType)
169 {
170 case token::tokenType::UNDEFINED: return "undefined";
171 case token::tokenType::BOOL: return "bool";
172 case token::tokenType::FLAG: return "flag";
173 case token::tokenType::PUNCTUATION: return "punctuation";
174
175 case token::tokenType::INTEGER_32: return "int32";
176 case token::tokenType::INTEGER_64: return "int64";
177 case token::tokenType::UNSIGNED_INTEGER_32: return "uint32";
178 case token::tokenType::UNSIGNED_INTEGER_64: return "uint64";
179 case token::tokenType::FLOAT: return "float";
180 case token::tokenType::DOUBLE: return "double";
181 case token::tokenType::WORD: return "word";
182 case token::tokenType::DIRECTIVE: return "directive";
183 case token::tokenType::STRING: return "string";
184 case token::tokenType::EXPRESSION: return "expression";
185 case token::tokenType::VARIABLE: return "variable";
186 case token::tokenType::VERBATIM: return "verbatim";
187 case token::tokenType::CHAR_DATA: return "char_data";
188 case token::tokenType::COMPOUND: return "compound";
189 case token::tokenType::ERROR: return "error";
190
191 default:
192 break;
193 }
195 return "unknown(" + std::to_string(int(tokType)) + ")";
196}
197
198
199// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
200
202{
203 tok.reset();
204 return is.read(tok);
205}
206
207
208Foam::Ostream& Foam::operator<<(Ostream& os, const token& tok)
209{
210 switch (tok.type_)
211 {
213 os << "UNDEFINED";
215 << "Undefined token" << endl;
216 break;
217
219 // Swallow the flag
220 break;
221
223 os << tok.data_.punctuationVal;
224 break;
225
227 os << tok.data_.flagVal; // An int value
228 break;
229
231 os << tok.data_.int32Val;
232 break;
233
235 os << tok.data_.int64Val;
236 break;
237
239 os << tok.data_.uint32Val;
240 break;
241
243 os << tok.data_.uint64Val;
244 break;
245
247 os << tok.data_.floatVal;
248 break;
249
251 os << tok.data_.doubleVal;
252 break;
253
254 // Possibly different behaviour for serial/parallel streams:
255 // preserve types
261 os.write(tok);
262 break;
263
265 os << *tok.data_.wordPtr;
266 break;
267
269 os << *tok.data_.stringPtr;
270 break;
271
273 os << *tok.data_.compoundPtr;
274 break;
275
277 os << "ERROR";
279 << "Error token" << endl;
280 break;
281
282 default:
283 os << "UNKNOWN";
285 << "Unknown token" << endl;
287
288 os.check(FUNCTION_NAME);
289 return os;
290}
291
294{
295 return os << char(pt);
296}
297
300{
301 return os << char(pt);
302}
303
304
305Foam::Ostream& Foam::operator<<(Ostream& os, const token::compound& ct)
306{
307 os << ct.type();
308 if (!ct.pending())
309 {
310 // Do not write compound content if tagged as 'pending-read'
311 os << token::SPACE;
312 ct.write(os);
313 }
315 return os;
316}
317
318
319// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320
321ostream& Foam::operator<<
322(
323 ostream& os,
324 const InfoProxy<token>& iproxy
326{
327 return printTokenInfo(os, *iproxy);
328}
329
330
331template<>
332Foam::Ostream& Foam::operator<<
333(
334 Ostream& os,
335 const InfoProxy<token>& iproxy
336)
337{
338 return printTokenInfo(os, *iproxy);
339}
340
341
342// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
A helper class for outputting values to Ostream.
Definition InfoProxy.H:49
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
Abstract base class for complex tokens.
Definition token.H:192
bool moved() const noexcept
Get compound transferred status.
Definition token.H:278
bool pending() const noexcept
Get compound pending-read status.
Definition token.H:291
virtual void write(Ostream &os) const =0
Write the underlying content.
A token holds an item read from Istream.
Definition token.H:70
tokenType
Enumeration defining the types of token.
Definition token.H:81
@ ERROR
Token error encountered.
Definition token.H:83
@ DOUBLE
double (double-precision) type
Definition token.H:94
@ FLAG
stream flag (1-byte bitmask)
Definition token.H:86
@ WORD
Foam::word.
Definition token.H:97
@ UNSIGNED_INTEGER_32
uint32 type
Definition token.H:91
@ EXPRESSION
Definition token.H:104
@ UNDEFINED
An undefined token-type.
Definition token.H:82
@ COMPOUND
Compound type such as List<label> etc.
Definition token.H:99
@ CHAR_DATA
String-variant: plain character content.
Definition token.H:110
@ INTEGER_64
int64 type
Definition token.H:90
@ FLOAT
float (single-precision) type
Definition token.H:93
@ DIRECTIVE
Definition token.H:101
@ BOOL
boolean type
Definition token.H:88
@ UNSIGNED_INTEGER_64
uint64 type
Definition token.H:92
@ INTEGER_32
int32 type
Definition token.H:89
@ STRING
Foam::string (usually double-quoted).
Definition token.H:98
@ PUNCTUATION
single character punctuation
Definition token.H:87
label lineNumber() const noexcept
The line number for the token.
Definition tokenI.H:570
word name() const
Return the name of the current token type.
Definition tokenI.H:476
punctuationToken
Standard punctuation tokens (a character).
Definition token.H:140
@ SPACE
Space [isspace].
Definition token.H:144
float floatToken() const
Return float value.
Definition tokenI.H:923
const string & stringToken() const
Return const reference to the string contents.
Definition tokenI.H:1076
punctuationToken pToken() const
Return punctuation character.
Definition tokenI.H:676
bool good() const noexcept
True if token is not UNDEFINED or ERROR.
Definition tokenI.H:584
constexpr token() noexcept
Default construct, initialized to an UNDEFINED token.
Definition tokenI.H:160
bool boolToken() const
Return boolean token value.
Definition tokenI.H:608
double doubleToken() const
Return double value.
Definition tokenI.H:950
const compound & compoundToken() const
Const reference to compound token. Fatal if the wrong type.
Definition tokenI.H:1124
uint32_t uint32Token() const
Return int32 value, convert from other integer type or Error.
Definition tokenI.H:798
int flagToken() const
Return flag bitmask value.
Definition tokenI.H:638
tokenType type() const noexcept
Return the token type.
Definition tokenI.H:482
int64_t int64Token() const
Return int64 value, convert from other integer type or Error.
Definition tokenI.H:767
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition tokenI.H:412
int32_t int32Token() const
Return int32 value, convert from other integer type or Error.
Definition tokenI.H:739
const word & wordToken() const
Return const reference to the word contents.
Definition tokenI.H:1022
bool read(Istream &is)
Read a token from Istream, calls reset() first.
Definition tokenIO.C:146
uint64_t uint64Token() const
Return int64 value, convert from other integer type or Error.
Definition tokenI.H:829
A class for handling words, derived from Foam::string.
Definition word.H:66
limits reset(1/(limits.max()+VSMALL), 1/(limits.min()+VSMALL))
OBJstream os(runTime.globalPath()/outputName)
#define WarningInFunction
Report a warning using Foam::Warning.
#define FUNCTION_NAME
#define SeriousErrorInFunction
Report an error message using Foam::SeriousError.
Namespace for OpenFOAM.
static OS & printTokenInfo(OS &os, const token &tok)
Definition tokenIO.C:31
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Istream & operator>>(Istream &, directionInfo &)