OSstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 {
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
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 
219 {
220  os_ << val;
221  setState(os_.rdstate());
222  return *this;
223 }
224 
225 
226 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
227 {
228  if (format() != BINARY)
229  {
231  << "stream format not binary"
232  << abort(FatalIOError);
233  }
234 
235  os_ << token::BEGIN_LIST;
236  os_.write(buf, count);
237  os_ << token::END_LIST;
238 
239  setState(os_.rdstate());
240 
241  return *this;
242 }
243 
244 
246 {
247  for (unsigned short i = 0; i < indentLevel_*indentSize_; i++)
248  {
249  os_ << ' ';
250  }
251 }
252 
253 
255 {
256  os_.flush();
257 }
258 
259 
261 {
262  write('\n');
263  os_.flush();
264 }
265 
266 
267 std::ios_base::fmtflags Foam::OSstream::flags() const
268 {
269  return os_.flags();
270 }
271 
272 
273 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
274 {
275  return os_.flags(f);
276 }
277 
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
282 {
283  return os_.width();
284 }
285 
286 
287 int Foam::OSstream::width(const int w)
288 {
289  return os_.width(w);
290 }
291 
292 
294 {
295  return os_.precision();
296 }
297 
298 
300 {
301  return os_.precision(p);
302 }
303 
304 
305 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:231
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:69
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:39
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:267
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:293
A token holds items read from Istream.
Definition: token.H:69
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:260
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string surrounded by quotes.
Definition: OSstream.C:125
virtual void indent()
Add indentation characters.
Definition: OSstream.C:245
A class for handling words, derived from string.
Definition: word.H:59
virtual void flush()
Flush stream.
Definition: OSstream.C:254
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
labelList f(nPoints)
const string & stringToken() const
Definition: tokenI.H:258
virtual Ostream & write(const token &)
Write next token to stream.
Definition: OSstream.C:32
long double longDoubleScalar
Lang double precision floating point scalar type.
virtual int width() const
Get width of output field.
Definition: OSstream.C:281
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
tokenType type() const
Definition: tokenI.H:187
const dimensionedScalar c
Speed of light in a vacuum.
static const unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:63
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:251
volScalarField & p
A class for handling character strings derived from std::string.
Definition: string.H:74
IOerror FatalIOError