Loading...
Searching...
No Matches
OSstream.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-2016,2024 OpenFOAM Foundation
9 Copyright (C) 2017-2023 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 "OSstream.H"
32#include <algorithm>
33
34// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35
36bool Foam::OSstream::write(const token& tok)
37{
38 // Direct token handling only for some types
39
40 switch (tok.type())
41 {
43 {
44 // silently consume the flag
45 return true;
46 }
47
49 {
50 // Token stored with leading '#' sigil - output directly
51 write(tok.wordToken());
52 return true;
53 }
54
56 {
57 // Token stored with surrounding '${{ .. }}' - output directly
58 writeQuoted(tok.stringToken(), false);
59 return true;
60 }
61
63 {
64 // Token stored with leading '$' sigil - output directly
65 writeQuoted(tok.stringToken(), false);
66 return true;
67 }
68
70 {
71 // Token stored without surrounding '#{ .. #}'. Add on output
72 write(char(token::HASH));
74 writeQuoted(tok.stringToken(), false);
75 write(char(token::HASH));
77
78 return true;
79 }
80
82 {
83 // Character content - written without quotes
84 writeQuoted(tok.stringToken(), false);
85 return true;
86 }
87
88 default:
89 break;
90 }
91
92 return false;
93}
94
95
97{
98 os_ << c;
99 syncState();
101 // Advance line number on newline
102 if (c == token::NL) ++lineNumber_;
103 return *this;
104}
105
106
108(
109 const char* str,
110 std::streamsize len,
111 const bool quoted
112)
113{
114 if (!str || len <= 0) return *this;
115
116 const char* last = (str + len);
117
118 if (!quoted)
119 {
120 os_ << std::string_view(str, len);
121 syncState();
122
123 // Unquoted, only advance line number on newline
124 lineNumber_ += std::count(str, last, '\n');
125 return *this;
126 }
127
128
129 // Output with surrounding quotes and backslash escaping
130 // - functionality like std::quoted (from <iomanip>), while also
131 // counting the newlines.
132
133 os_ << token::DQUOTE;
134
135 unsigned backslash = 0;
136 for (auto iter = str; iter != last; ++iter)
137 {
138 const char c = *iter;
139
140 if (c == '\\')
141 {
142 ++backslash;
143 continue; // Delay output until escaped character is known
144 }
145 else if (c == token::NL)
146 {
147 ++backslash; // Add backslash escape for newline
148 ++lineNumber_; // Advance line number on newline
149 }
150 else if (c == token::DQUOTE)
151 {
152 ++backslash; // Add backslash escape for quote
153 }
154
155 // output all pending backslashes
156 while (backslash)
157 {
158 os_ << '\\';
159 --backslash;
160 }
161
162 os_ << c;
163 }
164
165 // silently drop any trailing backslashes
166 // they would otherwise appear like an escaped end-quote
168
169 syncState();
170 return *this;
171}
172
173
174Foam::Ostream& Foam::OSstream::write(const char* str)
175{
176 if (!str) return *this;
177
178 const char* last = (str + strlen(str));
179
180 os_ << str;
181 syncState();
183 // Advance line number on newline
184 lineNumber_ += std::count(str, last, '\n');
185 return *this;
186}
187
188
190{
191 // Unquoted, and no newlines expected.
192 os_ << str;
193 syncState();
194
195 return *this;
196}
197
199Foam::Ostream& Foam::OSstream::write(const std::string& str)
200{
201 return writeQuoted(str.data(), str.size(), true);
202}
203
204
205Foam::Ostream& Foam::OSstream::write(const int32_t val)
207 os_ << val;
208 syncState();
209 return *this;
210}
211
212
213Foam::Ostream& Foam::OSstream::write(const int64_t val)
215 os_ << val;
216 syncState();
217 return *this;
218}
219
220
221Foam::Ostream& Foam::OSstream::write(const uint32_t val)
223 os_ << val;
224 syncState();
225 return *this;
226}
227
228
229Foam::Ostream& Foam::OSstream::write(const uint64_t val)
231 os_ << val;
232 syncState();
233 return *this;
234}
235
236
237Foam::Ostream& Foam::OSstream::write(const float val)
239 os_ << val;
240 syncState();
241 return *this;
242}
243
244
245Foam::Ostream& Foam::OSstream::write(const double val)
247 os_ << val;
248 syncState();
249 return *this;
250}
251
252
253Foam::Ostream& Foam::OSstream::write(const char* data, std::streamsize count)
254{
255 beginRawWrite(count);
256 writeRaw(data, count);
257 endRawWrite();
258
259 return *this;
260}
261
262
263bool Foam::OSstream::beginRawWrite(std::streamsize count)
264{
266 {
268 << "stream format not binary"
270 }
272 os_ << token::BEGIN_LIST;
273 syncState();
274 return os_.good();
275}
276
277
280 os_ << token::END_LIST;
281 syncState();
282 return os_.good();
283}
284
285
287(
288 const char* data,
289 std::streamsize count
290)
291{
292 // No check for IOstreamOption::BINARY since this is either done in the
293 // beginRawWrite() method, or the caller knows what they are doing.
295 os_.write(data, count);
296 syncState();
297 return *this;
298}
299
300
302{
303 for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
305 os_ << ' ';
306 }
307 syncState();
308}
309
312{
313 os_.flush();
314}
315
316
318{
319 write('\n');
320 os_.flush();
321}
322
323
324// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
326char Foam::OSstream::fill() const
327{
328 return os_.fill();
329}
330
332char Foam::OSstream::fill(const char fillch)
333{
334 return os_.fill(fillch);
335}
336
338int Foam::OSstream::width() const
339{
340 return os_.width();
341}
342
344int Foam::OSstream::width(const int w)
345{
346 return os_.width(w);
347}
348
351{
352 return os_.precision();
353}
354
355
356int Foam::OSstream::precision(const int p)
357{
358 return os_.precision(p);
359}
360
361
362// ************************************************************************* //
label lineNumber_
The file line.
Definition IOstream.H:140
virtual void flush() override
Flush stream.
Definition OSstream.C:304
virtual Ostream & writeQuoted(const char *str, std::streamsize len, const bool quoted=true) override
Write character/string content, with/without surrounding quotes.
Definition OSstream.C:101
virtual void indent() override
Add indentation characters.
Definition OSstream.C:294
virtual char fill() const override
Get the current padding character.
Definition OSstream.C:319
virtual int precision() const override
Get precision of output field.
Definition OSstream.C:343
virtual Ostream & writeRaw(const char *data, std::streamsize count) override
Low-level raw binary output.
Definition OSstream.C:280
virtual bool beginRawWrite(std::streamsize count) override
Begin marker for low-level raw binary output.
Definition OSstream.C:256
void syncState()
Set stream state to match that of the std::ostream.
Definition OSstream.H:182
virtual bool endRawWrite() override
End marker for low-level raw binary output.
Definition OSstream.C:271
virtual void endl() override
Add newline and flush stream.
Definition OSstream.C:310
virtual bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition OSstream.C:29
virtual int width() const override
Get width of output field.
Definition OSstream.C:331
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
unsigned short indentSize_
Number of spaces per indent level.
Definition Ostream.H:72
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
unsigned short indentLevel_
Current indent level.
Definition Ostream.H:77
A token holds an item read from Istream.
Definition token.H:70
@ FLAG
stream flag (1-byte bitmask)
Definition token.H:86
@ EXPRESSION
Definition token.H:104
@ CHAR_DATA
String-variant: plain character content.
Definition token.H:110
@ DIRECTIVE
Definition token.H:101
@ BEGIN_BLOCK
Begin block [isseparator].
Definition token.H:178
@ END_BLOCK
End block [isseparator].
Definition token.H:179
@ HASH
Hash - directive or start verbatim string.
Definition token.H:149
@ BEGIN_LIST
Begin list [isseparator].
Definition token.H:174
@ DQUOTE
Double quote.
Definition token.H:154
@ END_LIST
End list [isseparator].
Definition token.H:175
@ NL
Newline [isspace].
Definition token.H:143
const string & stringToken() const
Return const reference to the string contents.
Definition tokenI.H:1076
tokenType type() const noexcept
Return the token type.
Definition tokenI.H:482
const word & wordToken() const
Return const reference to the word contents.
Definition tokenI.H:1022
A class for handling words, derived from Foam::string.
Definition word.H:66
volScalarField & p
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
const dimensionedScalar c
Speed of light in a vacuum.
errorManip< error > abort(error &err)
Definition errorManip.H:139
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
runTime write()
word format(conversionProperties.get< word >("format"))