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 -------------------------------------------------------------------------------
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  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 
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 
226 {
227  if (format() != BINARY)
228  {
230  << "stream format not binary"
231  << abort(FatalIOError);
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 
288 std::ios_base::fmtflags Foam::OSstream::flags() const
289 {
290  return os_.flags();
291 }
292 
293 
294 std::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 
308 char Foam::OSstream::fill(const char fillch)
309 {
310  return os_.fill(fillch);
311 }
312 
313 
315 {
316  return os_.width();
317 }
318 
319 
320 int 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 // ************************************************************************* //
token.H
Foam::doubleScalar
double doubleScalar
A typedef for double.
Definition: scalarFwd.H:48
p
volScalarField & p
Definition: createFieldRefs.H:8
Foam::OSstream::write
virtual bool write(const token &tok)
Write token to stream or otherwise handle it.
Definition: OSstream.C:36
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:689
Foam::floatScalar
float floatScalar
A typedef for float.
Definition: scalarFwd.H:45
Foam::OSstream::beginRawWrite
virtual bool beginRawWrite(std::streamsize count)
Begin marker for low-level raw binary output.
Definition: OSstream.C:225
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:314
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:119
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
writeQuoted
os writeQuoted(("# "+outputName+"\n"), false)
Foam::OSstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:251
format
word format(conversionProperties.get< word >("format"))
Foam::token::DQUOTE
Double quote.
Definition: token.H:135
error.H
Foam::OSstream::fill
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:302
Foam::OSstream::flags
virtual ios_base::fmtflags flags() const
Get stream flags.
Definition: OSstream.C:288
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:160
Foam::OSstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:241
Foam::token::NL
Newline [isspace].
Definition: token.H:124
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::token::HASH
Hash - directive or start verbatim string.
Definition: token.H:130
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:631
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:326
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:266
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:311
Foam::OSstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:281
f
labelList f(nPoints)
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:36
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::stringOps::count
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
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155
stringOps.H
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::OSstream::flush
virtual void flush()
Flush stream.
Definition: OSstream.C:275
OSstream.H