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 OpenFOAM Foundation
9 Copyright (C) 2017-2021 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 "stringOps.H"
33
34// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
35
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
81 default:
82 break;
83 }
84
85 return false;
86}
87
88
90{
91 os_ << c;
92 if (c == token::NL)
93 {
94 ++lineNumber_;
95 }
96 setState(os_.rdstate());
97 return *this;
98}
99
100
102{
103 lineNumber_ += stringOps::count(str, token::NL);
104 os_ << str;
105 setState(os_.rdstate());
106 return *this;
107}
108
109
111{
112 os_ << str;
113 setState(os_.rdstate());
114 return *this;
115}
116
117
119(
120 const std::string& str,
121 const bool quoted
122)
123{
124 if (!quoted)
125 {
126 // Output unquoted, only advance line number on newline
127 lineNumber_ += stringOps::count(str, token::NL);
128 os_ << str;
129
130 setState(os_.rdstate());
131 return *this;
132 }
133
134
135 // Output with surrounding quotes and backslash escaping
136 os_ << token::DQUOTE;
137
138 unsigned backslash = 0;
139 for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
140 {
141 const char c = *iter;
142
143 if (c == '\\')
144 {
145 ++backslash;
146 continue; // only output after escaped character is known
147 }
148 else if (c == token::NL)
149 {
150 ++lineNumber_;
151 ++backslash; // backslash escape for newline
152 }
153 else if (c == token::DQUOTE)
154 {
155 ++backslash; // backslash escape for quote
156 }
157
158 // output all pending backslashes
159 while (backslash)
160 {
161 os_ << '\\';
162 --backslash;
163 }
164
165 os_ << c;
166 }
167
168 // silently drop any trailing backslashes
169 // they would otherwise appear like an escaped end-quote
170 os_ << token::DQUOTE;
171
172 setState(os_.rdstate());
173 return *this;
174}
175
176
178{
179 return writeQuoted(str, true);
180}
181
182
184{
185 os_ << val;
186 setState(os_.rdstate());
187 return *this;
188}
189
190
192{
193 os_ << val;
194 setState(os_.rdstate());
195 return *this;
196}
197
198
200{
201 os_ << val;
202 setState(os_.rdstate());
203 return *this;
204}
205
206
208{
209 os_ << val;
210 setState(os_.rdstate());
211 return *this;
212}
213
214
215Foam::Ostream& Foam::OSstream::write(const char* data, std::streamsize count)
216{
217 beginRawWrite(count);
218 writeRaw(data, count);
219 endRawWrite();
220
221 return *this;
222}
223
224
225bool Foam::OSstream::beginRawWrite(std::streamsize count)
226{
227 if (format() != BINARY)
228 {
230 << "stream format not binary"
232 }
233
234 os_ << token::BEGIN_LIST;
235 setState(os_.rdstate());
236
237 return os_.good();
238}
239
240
242{
243 os_ << token::END_LIST;
244 setState(os_.rdstate());
245
246 return os_.good();
247}
248
249
251(
252 const char* data,
253 std::streamsize count
254)
255{
256 // No check for format() == BINARY since this is either done in the
257 // beginRawWrite() method, or the caller knows what they are doing.
258
259 os_.write(data, count);
260 setState(os_.rdstate());
261
262 return *this;
263}
264
265
267{
268 for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
269 {
270 os_ << ' ';
271 }
272}
273
274
276{
277 os_.flush();
278}
279
280
282{
283 write('\n');
284 os_.flush();
285}
286
287
288std::ios_base::fmtflags Foam::OSstream::flags() const
289{
290 return os_.flags();
291}
292
293
294std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
295{
296 return os_.flags(f);
297}
298
299
300// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
301
303{
304 return os_.fill();
305}
306
307
308char Foam::OSstream::fill(const char fillch)
309{
310 return os_.fill(fillch);
311}
312
313
315{
316 return os_.width();
317}
318
319
320int Foam::OSstream::width(const int w)
321{
322 return os_.width(w);
323}
324
325
327{
328 return os_.precision();
329}
330
331
333{
334 return os_.precision(p);
335}
336
337
338// ************************************************************************* //
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:281
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:251
virtual void indent()
Add indentation characters.
Definition: OSstream.C:266
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OSstream.C:225
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:302
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:241
virtual int width() const
Get width of output field.
Definition: OSstream.C:314
virtual ios_base::fmtflags flags() const
Get stream flags.
Definition: OSstream.C:288
virtual void flush()
Flush stream.
Definition: OSstream.C:275
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:119
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Database for solution data, solver performance and other reduced data.
Definition: data.H:58
virtual bool write()
Write the output fields.
A token holds an item read from Istream.
Definition: token.H:69
@ VARIABLE
Definition: token.H:98
@ FLAG
stream flag (1-byte bitmask)
Definition: token.H:82
@ EXPRESSION
Definition: token.H:96
@ VERBATIM
Definition: token.H:100
@ DIRECTIVE
Definition: token.H:94
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
@ END_BLOCK
End block [isseparator].
Definition: token.H:160
@ HASH
Hash - directive or start verbatim string.
Definition: token.H:130
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
@ DQUOTE
Double quote.
Definition: token.H:135
@ END_LIST
End list [isseparator].
Definition: token.H:156
@ NL
Newline [isspace].
Definition: token.H:124
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:311
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
A class for handling words, derived from Foam::string.
Definition: word.H:68
volScalarField & p
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
os writeQuoted(("# "+outputName+"\n"), false)
std::string::size_type count(const std::string &s, const char c)
Count the number of occurrences of the specified character.
Definition: stringOps.C:697
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
errorManip< error > abort(error &err)
Definition: errorManip.H:144
IOerror FatalIOError
runTime write()
word format(conversionProperties.get< word >("format"))
labelList f(nPoints)