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-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
36 bool Foam::OSstream::write(const token& tok)
37 {
38  // Direct token handling only for some types
39 
40  switch (tok.type())
41  {
42  case token::tokenType::FLAG :
43  {
44  // silently consume the flag
45  return true;
46  }
47 
48  case token::tokenType::DIRECTIVE :
49  {
50  // Token stored with leading '#' sigil - output directly
51  write(tok.wordToken());
52  return true;
53  }
54 
55  case token::tokenType::EXPRESSION :
56  {
57  // Token stored with surrounding '${{ .. }}' - output directly
58  writeQuoted(tok.stringToken(), false);
59  return true;
60  }
61 
62  case token::tokenType::VARIABLE :
63  {
64  // Token stored with leading '$' sigil - output directly
65  writeQuoted(tok.stringToken(), false);
66  return true;
67  }
68 
69  case token::tokenType::VERBATIM :
70  {
71  // Token stored without surrounding '#{ .. #}'. Add on output
72  write(char(token::HASH));
74  writeQuoted(tok.stringToken(), false);
75  write(char(token::HASH));
76  write(char(token::END_BLOCK));
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  syncState();
97  return *this;
98 }
99 
100 
101 Foam::Ostream& Foam::OSstream::write(const char* str)
102 {
103  lineNumber_ += stringOps::count(str, token::NL);
104  os_ << str;
105  syncState();
106  return *this;
107 }
108 
109 
111 {
112  os_ << str;
113  syncState();
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  syncState();
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
171 
172  syncState();
173  return *this;
174 }
175 
177 Foam::Ostream& Foam::OSstream::write(const string& str)
178 {
179  return writeQuoted(str, true);
180 }
181 
182 
183 Foam::Ostream& Foam::OSstream::write(const int32_t val)
184 {
185  os_ << val;
186  syncState();
187  return *this;
188 }
189 
190 
191 Foam::Ostream& Foam::OSstream::write(const int64_t val)
192 {
193  os_ << val;
194  syncState();
195  return *this;
196 }
197 
198 
199 Foam::Ostream& Foam::OSstream::write(const float val)
200 {
201  os_ << val;
202  syncState();
203  return *this;
204 }
205 
206 
207 Foam::Ostream& Foam::OSstream::write(const double val)
208 {
209  os_ << val;
210  syncState();
211  return *this;
212 }
213 
214 
215 Foam::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 
225 bool Foam::OSstream::beginRawWrite(std::streamsize count)
226 {
227  if (format() != BINARY)
228  {
230  << "stream format not binary"
231  << abort(FatalIOError);
232  }
234  os_ << token::BEGIN_LIST;
235  syncState();
236  return os_.good();
237 }
238 
239 
241 {
243  syncState();
244  return os_.good();
245 }
246 
247 
249 (
250  const char* data,
251  std::streamsize count
252 )
253 {
254  // No check for format() == BINARY since this is either done in the
255  // beginRawWrite() method, or the caller knows what they are doing.
257  os_.write(data, count);
258  syncState();
259  return *this;
260 }
261 
262 
264 {
265  for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
266  {
267  os_ << ' ';
268  }
269  syncState();
270 }
271 
274 {
275  os_.flush();
276 }
277 
278 
280 {
281  write('\n');
282  os_.flush();
283 }
284 
285 
286 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 char Foam::OSstream::fill() const
289 {
290  return os_.fill();
291 }
292 
294 char Foam::OSstream::fill(const char fillch)
295 {
296  return os_.fill(fillch);
297 }
298 
300 int Foam::OSstream::width() const
301 {
302  return os_.width();
303 }
304 
306 int Foam::OSstream::width(const int w)
307 {
308  return os_.width(w);
309 }
310 
312 int Foam::OSstream::precision() const
313 {
314  return os_.precision();
315 }
316 
317 
318 int Foam::OSstream::precision(const int p)
319 {
320  return os_.precision(p);
321 }
322 
323 
324 // ************************************************************************* //
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:233
Begin block [isseparator].
Definition: token.H:162
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:281
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:305
Double quote.
Definition: token.H:138
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:624
A token holds an item read from Istream.
Definition: token.H:64
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:272
Newline [isspace].
Definition: token.H:127
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:304
virtual void indent()
Add indentation characters.
Definition: OSstream.C:256
Begin list [isseparator].
Definition: token.H:158
os writeQuoted(("# "+outputName+"\), false)
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
A class for handling words, derived from Foam::string.
Definition: word.H:63
virtual void flush()
Flush stream.
Definition: OSstream.C:266
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
End list [isseparator].
Definition: token.H:159
errorManip< error > abort(error &err)
Definition: errorManip.H:139
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OSstream.C:29
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:112
Database for solution data, solver performance and other reduced data.
Definition: data.H:51
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:682
word format(conversionProperties.get< word >("format"))
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OSstream.C:218
virtual int width() const
Get width of output field.
Definition: OSstream.C:293
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
const dimensionedScalar c
Speed of light in a vacuum.
std::string::size_type count(const std::string &s, const char c)
Count the number of occurrences of the specified character.
Definition: stringOps.C:688
volScalarField & p
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:242
End block [isseparator].
Definition: token.H:163
Hash - directive or start verbatim string.
Definition: token.H:133
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...