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-2024 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  os_ << c;
35  if (c == token::NL)
36  {
37  lineNumber_++;
38  }
39  setState(os_.rdstate());
40  return *this;
41 }
42 
43 
45 {
46  lineNumber_ += string(str).count(token::NL);
47  os_ << str;
48  setState(os_.rdstate());
49  return *this;
50 }
51 
52 
54 {
55  os_ << str;
56  setState(os_.rdstate());
57  return *this;
58 }
59 
60 
62 {
63  return writeQuoted(str);
64 }
65 
66 
68 {
70  writeQuoted(vs, false);
71  os_ << token::HASH << token::END_BLOCK;
72  setState(os_.rdstate());
73  return *this;
74 }
75 
76 
78 (
79  const std::string& str,
80  const bool quoted
81 )
82 {
83  if (quoted)
84  {
85  os_ << token::BEGIN_STRING;
86 
87  int backslash = 0;
88  for
89  (
90  string::const_iterator iter = str.begin();
91  iter != str.end();
92  ++iter
93  )
94  {
95  char c = *iter;
96 
97  if (c == '\\')
98  {
99  backslash++;
100  // suppress output until we know if other characters follow
101  continue;
102  }
103  else if (c == token::NL)
104  {
105  lineNumber_++;
106  backslash++; // backslash escape for newline
107  }
108  else if (c == token::END_STRING)
109  {
110  backslash++; // backslash escape for quote
111  }
112 
113  // output pending backslashes
114  while (backslash)
115  {
116  os_ << '\\';
117  backslash--;
118  }
119 
120  os_ << c;
121  }
122 
123  // silently drop any trailing backslashes
124  // they would otherwise appear like an escaped end-quote
125  os_ << token::END_STRING;
126  }
127  else
128  {
129  // output unquoted string, only advance line number on newline
130  lineNumber_ += string(str).count(token::NL);
131  os_ << str;
132  }
133 
134  setState(os_.rdstate());
135  return *this;
136 }
137 
138 
140 {
141  os_ << val;
142  setState(os_.rdstate());
143  return *this;
144 }
145 
146 
148 {
149  os_ << val;
150  setState(os_.rdstate());
151  return *this;
152 }
153 
154 
156 {
157  os_ << val;
158  setState(os_.rdstate());
159  return *this;
160 }
161 
162 
164 {
165  os_ << val;
166  setState(os_.rdstate());
167  return *this;
168 }
169 
170 
172 {
173  os_ << val;
174  setState(os_.rdstate());
175  return *this;
176 }
177 
178 
180 {
181  os_ << val;
182  setState(os_.rdstate());
183  return *this;
184 }
185 
186 
188 {
189  os_ << val;
190  setState(os_.rdstate());
191  return *this;
192 }
193 
194 
195 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
196 {
197  if (format() != BINARY)
198  {
200  << "stream format not binary"
201  << abort(FatalIOError);
202  }
203 
204  os_ << token::BEGIN_LIST;
205  os_.write(buf, count);
206  os_ << token::END_LIST;
207 
208  setState(os_.rdstate());
209 
210  return *this;
211 }
212 
213 
215 {
216  for (unsigned short i = 0; i < indentLevel_*indentSize_; i++)
217  {
218  os_ << ' ';
219  }
220 }
221 
222 
224 {
225  os_.flush();
226 }
227 
228 
230 {
231  write('\n');
232  os_.flush();
233 }
234 
235 
236 std::ios_base::fmtflags Foam::OSstream::flags() const
237 {
238  return os_.flags();
239 }
240 
241 
242 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
243 {
244  return os_.flags(f);
245 }
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 
251 {
252  return os_.width();
253 }
254 
255 
256 int Foam::OSstream::width(const int w)
257 {
258  return os_.width(w);
259 }
260 
261 
263 {
264  return os_.precision();
265 }
266 
267 
269 {
270  return os_.precision(p);
271 }
272 
273 
274 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:228
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:229
virtual void indent()
Add indentation characters.
Definition: OSstream.C:214
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string with optional double quotes.
Definition: OSstream.C:78
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:262
virtual Ostream & write(const char)
Write character.
Definition: OSstream.C:32
virtual int width() const
Get width of output field.
Definition: OSstream.C:250
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:236
virtual void flush()
Flush stream.
Definition: OSstream.C:223
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A class for handling character strings derived from std::string.
Definition: string.H:79
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:47
@ BEGIN_STRING
Definition: token.H:119
@ BEGIN_BLOCK
Definition: token.H:113
@ END_BLOCK
Definition: token.H:114
@ END_STRING
Definition: token.H:120
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
const dimensionedScalar c
Speed of light in a vacuum.
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
double doubleScalar
Double precision floating point scalar type.
Definition: doubleScalar.H:52
errorManip< error > abort(error &err)
Definition: errorManip.H:131
float floatScalar
Float precision floating point scalar type.
Definition: floatScalar.H:52
IOerror FatalIOError
label count(const ListType &l, typename ListType::const_reference x)
Count the number of occurrences of a value in a list.
long double longDoubleScalar
Lang double precision floating point scalar type.
word format(conversionProperties.lookup("format"))
labelList f(nPoints)
volScalarField & p