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-2020 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  // The '#' sigil is already part of the wordToken
51  write(tok.wordToken());
52 
53  return true;
54  }
55 
56  case token::tokenType::VERBATIM :
57  {
58  // Surrounding '#{ .. #}' to be recognized as verbatim
59  write(char(token::HASH));
61  writeQuoted(tok.stringToken(), false);
62  write(char(token::HASH));
63  write(char(token::END_BLOCK));
64 
65  return true;
66  }
67 
68  case token::tokenType::VARIABLE :
69  {
70  writeQuoted(tok.stringToken(), false);
71 
72  return true;
73  }
74 
75  default:
76  break;
77  }
78 
79  return false;
80 }
81 
82 
84 {
85  os_ << c;
86  if (c == token::NL)
87  {
88  ++lineNumber_;
89  }
90  setState(os_.rdstate());
91  return *this;
92 }
93 
94 
96 {
97  lineNumber_ += stringOps::count(str, token::NL);
98  os_ << str;
99  setState(os_.rdstate());
100  return *this;
101 }
102 
103 
105 {
106  os_ << str;
107  setState(os_.rdstate());
108  return *this;
109 }
110 
111 
113 (
114  const std::string& str,
115  const bool quoted
116 )
117 {
118  if (!quoted)
119  {
120  // Output unquoted, only advance line number on newline
121  lineNumber_ += stringOps::count(str, token::NL);
122  os_ << str;
123 
124  setState(os_.rdstate());
125  return *this;
126  }
127 
128 
129  // Output with surrounding quotes and backslash escaping
130  os_ << token::BEGIN_STRING;
131 
132  unsigned backslash = 0;
133  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
134  {
135  const char c = *iter;
136 
137  if (c == '\\')
138  {
139  ++backslash;
140  continue; // only output after escaped character is known
141  }
142  else if (c == token::NL)
143  {
144  ++lineNumber_;
145  ++backslash; // backslash escape for newline
146  }
147  else if (c == token::END_STRING)
148  {
149  ++backslash; // backslash escape for quote
150  }
151 
152  // output all pending backslashes
153  while (backslash)
154  {
155  os_ << '\\';
156  --backslash;
157  }
158 
159  os_ << c;
160  }
161 
162  // silently drop any trailing backslashes
163  // they would otherwise appear like an escaped end-quote
164  os_ << token::END_STRING;
165 
166  setState(os_.rdstate());
167  return *this;
168 }
169 
170 
172 {
173  return writeQuoted(str, true);
174 }
175 
176 
178 {
179  os_ << val;
180  setState(os_.rdstate());
181  return *this;
182 }
183 
184 
186 {
187  os_ << val;
188  setState(os_.rdstate());
189  return *this;
190 }
191 
192 
194 {
195  os_ << val;
196  setState(os_.rdstate());
197  return *this;
198 }
199 
200 
202 {
203  os_ << val;
204  setState(os_.rdstate());
205  return *this;
206 }
207 
208 
209 Foam::Ostream& Foam::OSstream::write(const char* data, std::streamsize count)
210 {
211  beginRawWrite(count);
212  writeRaw(data, count);
213  endRawWrite();
214 
215  return *this;
216 }
217 
218 
220 {
221  if (format() != BINARY)
222  {
224  << "stream format not binary"
225  << abort(FatalIOError);
226  }
227 
228  os_ << token::BEGIN_LIST;
229  setState(os_.rdstate());
230 
231  return os_.good();
232 }
233 
234 
236 {
237  os_ << token::END_LIST;
238  setState(os_.rdstate());
239 
240  return os_.good();
241 }
242 
243 
245 (
246  const char* data,
247  std::streamsize count
248 )
249 {
250  // No check for format() == BINARY since this is either done in the
251  // beginRawWrite() method, or the caller knows what they are doing.
252 
253  os_.write(data, count);
254  setState(os_.rdstate());
255 
256  return *this;
257 }
258 
259 
261 {
262  for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
263  {
264  os_ << ' ';
265  }
266 }
267 
268 
270 {
271  os_.flush();
272 }
273 
274 
276 {
277  write('\n');
278  os_.flush();
279 }
280 
281 
282 std::ios_base::fmtflags Foam::OSstream::flags() const
283 {
284  return os_.flags();
285 }
286 
287 
288 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
289 {
290  return os_.flags(f);
291 }
292 
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 
297 {
298  return os_.fill();
299 }
300 
301 
302 char Foam::OSstream::fill(const char fillch)
303 {
304  return os_.fill(fillch);
305 }
306 
307 
309 {
310  return os_.width();
311 }
312 
313 
314 int Foam::OSstream::width(const int w)
315 {
316  return os_.width(w);
317 }
318 
319 
321 {
322  return os_.precision();
323 }
324 
325 
327 {
328  return os_.precision(p);
329 }
330 
331 
332 // ************************************************************************* //
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:62
Foam::token::stringToken
const string & stringToken() const
Return const reference to the string contents.
Definition: tokenI.H:644
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:219
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:308
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &str, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:113
Foam::token::END_STRING
End string with double quote.
Definition: token.H:143
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::OSstream::writeRaw
virtual Ostream & writeRaw(const char *data, std::streamsize count)
Low-level raw binary output.
Definition: OSstream.C:245
format
word format(conversionProperties.get< word >("format"))
error.H
Foam::OSstream::fill
virtual char fill() const
Get the current padding character.
Definition: OSstream.C:296
Foam::OSstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:282
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:127
Foam::OSstream::endRawWrite
virtual bool endRawWrite()
End marker for low-level raw binary output.
Definition: OSstream.C:235
Foam::token::NL
Newline [isspace].
Definition: token.H:119
Foam::Ostream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Foam::token::HASH
Hash - directive or 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:126
Foam::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.H:599
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:320
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:260
Foam::token::type
tokenType type() const noexcept
Return the token type.
Definition: tokenI.H:309
Foam::OSstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:275
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:35
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
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:401
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:699
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:122
stringOps.H
Foam::data
Database for solution data, solver performance and other reduced data.
Definition: data.H:55
Foam::token::BEGIN_STRING
Begin string with double quote.
Definition: token.H:142
Foam::OSstream::flush
virtual void flush()
Flush stream.
Definition: OSstream.C:269
OSstream.H