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