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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "OSstream.H"
28 #include "token.H"
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
33 {
34  if (t.type() == token::VERBATIMSTRING)
35  {
36  write(char(token::HASH));
38  writeQuoted(t.stringToken(), false);
39  write(char(token::HASH));
40  write(char(token::END_BLOCK));
41  }
42  else if (t.type() == token::VARIABLE)
43  {
44  writeQuoted( t.stringToken(), false);
45  }
46  return *this;
47 }
48 
49 
51 {
52  os_ << c;
53  if (c == token::NL)
54  {
55  lineNumber_++;
56  }
57  setState(os_.rdstate());
58  return *this;
59 }
60 
61 
63 {
64  lineNumber_ += string(str).count(token::NL);
65  os_ << str;
66  setState(os_.rdstate());
67  return *this;
68 }
69 
70 
72 {
73  os_ << str;
74  setState(os_.rdstate());
75  return *this;
76 }
77 
78 
80 {
81  os_ << token::BEGIN_STRING;
82 
83  int backslash = 0;
84  for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
85  {
86  char c = *iter;
87 
88  if (c == '\\')
89  {
90  backslash++;
91  // suppress output until we know if other characters follow
92  continue;
93  }
94  else if (c == token::NL)
95  {
96  lineNumber_++;
97  backslash++; // backslash escape for newline
98  }
99  else if (c == token::END_STRING)
100  {
101  backslash++; // backslash escape for quote
102  }
103 
104  // output pending backslashes
105  while (backslash)
106  {
107  os_ << '\\';
108  backslash--;
109  }
110 
111  os_ << c;
112  }
113 
114  // silently drop any trailing backslashes
115  // they would otherwise appear like an escaped end-quote
116 
117  os_ << token::END_STRING;
118 
119  setState(os_.rdstate());
120  return *this;
121 }
122 
123 
125 (
126  const std::string& str,
127  const bool quoted
128 )
129 {
130  if (quoted)
131  {
132  os_ << token::BEGIN_STRING;
133 
134  int backslash = 0;
135  for
136  (
137  string::const_iterator iter = str.begin();
138  iter != str.end();
139  ++iter
140  )
141  {
142  char c = *iter;
143 
144  if (c == '\\')
145  {
146  backslash++;
147  // suppress output until we know if other characters follow
148  continue;
149  }
150  else if (c == token::NL)
151  {
152  lineNumber_++;
153  backslash++; // backslash escape for newline
154  }
155  else if (c == token::END_STRING)
156  {
157  backslash++; // backslash escape for quote
158  }
159 
160  // output pending backslashes
161  while (backslash)
162  {
163  os_ << '\\';
164  backslash--;
165  }
166 
167  os_ << c;
168  }
169 
170  // silently drop any trailing backslashes
171  // they would otherwise appear like an escaped end-quote
172  os_ << token::END_STRING;
173  }
174  else
175  {
176  // output unquoted string, only advance line number on newline
177  lineNumber_ += string(str).count(token::NL);
178  os_ << str;
179  }
180 
181  setState(os_.rdstate());
182  return *this;
183 }
184 
185 
187 {
188  os_ << val;
189  setState(os_.rdstate());
190  return *this;
191 }
192 
193 
195 {
196  os_ << val;
197  setState(os_.rdstate());
198  return *this;
199 }
200 
201 
203 {
204  os_ << val;
205  setState(os_.rdstate());
206  return *this;
207 }
208 
209 
211 {
212  os_ << val;
213  setState(os_.rdstate());
214  return *this;
215 }
216 
217 
218 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
219 {
220  if (format() != BINARY)
221  {
223  << "stream format not binary"
224  << abort(FatalIOError);
225  }
226 
227  os_ << token::BEGIN_LIST;
228  os_.write(buf, count);
229  os_ << token::END_LIST;
230 
231  setState(os_.rdstate());
232 
233  return *this;
234 }
235 
236 
238 {
239  for (unsigned short i = 0; i < indentLevel_*indentSize_; i++)
240  {
241  os_ << ' ';
242  }
243 }
244 
245 
247 {
248  os_.flush();
249 }
250 
251 
252 // Add carriage return and flush stream
254 {
255  write('\n');
256  os_.flush();
257 }
258 
259 
260 // Get flags of output stream
261 std::ios_base::fmtflags Foam::OSstream::flags() const
262 {
263  return os_.flags();
264 }
265 
266 
267 // Set flags of output stream
268 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
269 {
270  return os_.flags(f);
271 }
272 
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 
277 // Get width of output field
279 {
280  return os_.width();
281 }
282 
283 // Set width of output field (and return old width)
284 int Foam::OSstream::width(const int w)
285 {
286  return os_.width(w);
287 }
288 
289 // Get precision of output field
291 {
292  return os_.precision();
293 }
294 
295 // Set precision of output field (and return old precision)
297 {
298  return os_.precision(p);
299 }
300 
301 
302 // ************************************************************************* //
token.H
format
word format(conversionProperties.lookup("format"))
Foam::doubleScalar
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:49
w
volScalarField w(IOobject("w", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE), mesh, dimensionedScalar("w", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0))
p
p
Definition: pEqn.H:62
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::floatScalar
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:49
Foam::OSstream::width
virtual int width() const
Get width of output field.
Definition: OSstream.C:278
Foam::OSstream::write
virtual Ostream & write(const token &)
Write next token to stream.
Definition: OSstream.C:32
Foam::FatalIOError
IOerror FatalIOError
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:74
Foam::token::NL
@ NL
Definition: token.H:97
Foam::token::stringToken
const string & stringToken() const
Definition: tokenI.H:249
Foam::token::type
tokenType type() const
Definition: tokenI.H:178
error.H
Foam::token::VARIABLE
@ VARIABLE
Definition: token.H:79
Foam::OSstream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:261
Foam::token::BEGIN_STRING
@ BEGIN_STRING
Definition: token.H:110
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::OSstream::precision
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:290
Foam::OSstream::indent
virtual void indent()
Add indentation characters.
Definition: OSstream.C:237
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::OSstream::endl
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:253
f
labelList f(nPoints)
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::OSstream::writeQuoted
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:125
Foam::token::HASH
@ HASH
Definition: token.H:108
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::string::count
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:40
Foam::token::VERBATIMSTRING
@ VERBATIMSTRING
Definition: token.H:81
write
Tcoeff write()
Foam::OSstream::flush
virtual void flush()
Flush stream.
Definition: OSstream.C:246
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
OSstream.H
Foam::token::END_STRING
@ END_STRING
Definition: token.H:111