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-2025 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 {
69  writeQuoted(kt, kt.isPattern());
70  setState(os_.rdstate());
71  return *this;
72 }
73 
74 
76 {
78  writeQuoted(vs, false);
79  os_ << token::HASH << token::END_BLOCK;
80  setState(os_.rdstate());
81  return *this;
82 }
83 
84 
86 (
87  const std::string& str,
88  const bool quoted
89 )
90 {
91  if (quoted)
92  {
93  os_ << token::BEGIN_STRING;
94 
95  int backslash = 0;
96  for
97  (
98  string::const_iterator iter = str.begin();
99  iter != str.end();
100  ++iter
101  )
102  {
103  char c = *iter;
104 
105  if (c == '\\')
106  {
107  backslash++;
108  // suppress output until we know if other characters follow
109  continue;
110  }
111  else if (c == token::NL)
112  {
113  lineNumber_++;
114  backslash++; // backslash escape for newline
115  }
116  else if (c == token::END_STRING)
117  {
118  backslash++; // backslash escape for quote
119  }
120 
121  // output pending backslashes
122  while (backslash)
123  {
124  os_ << '\\';
125  backslash--;
126  }
127 
128  os_ << c;
129  }
130 
131  // silently drop any trailing backslashes
132  // they would otherwise appear like an escaped end-quote
133  os_ << token::END_STRING;
134  }
135  else
136  {
137  // output unquoted string, only advance line number on newline
138  lineNumber_ += string(str).count(token::NL);
139  os_ << str;
140  }
141 
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 
196 {
197  os_ << val;
198  setState(os_.rdstate());
199  return *this;
200 }
201 
202 
203 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
204 {
205  if (format() != BINARY)
206  {
208  << "stream format not binary"
209  << abort(FatalIOError);
210  }
211 
212  os_ << token::BEGIN_LIST;
213  os_.write(buf, count);
214  os_ << token::END_LIST;
215 
216  setState(os_.rdstate());
217 
218  return *this;
219 }
220 
221 
223 {
224  for (unsigned short i = 0; i < indentLevel_*indentSize_; i++)
225  {
226  os_ << ' ';
227  }
228 }
229 
230 
232 {
233  os_.flush();
234 }
235 
236 
238 {
239  write('\n');
240  os_.flush();
241 }
242 
243 
244 std::ios_base::fmtflags Foam::OSstream::flags() const
245 {
246  return os_.flags();
247 }
248 
249 
250 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
251 {
252  return os_.flags(f);
253 }
254 
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
259 {
260  return os_.width();
261 }
262 
263 
264 int Foam::OSstream::width(const int w)
265 {
266  return os_.width(w);
267 }
268 
269 
271 {
272  return os_.precision();
273 }
274 
275 
277 {
278  return os_.precision(p);
279 }
280 
281 
282 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:229
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:249
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:237
virtual void indent()
Add indentation characters.
Definition: OSstream.C:222
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string with optional double quotes.
Definition: OSstream.C:86
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:270
virtual Ostream & write(const char)
Write character.
Definition: OSstream.C:32
virtual int width() const
Get width of output field.
Definition: OSstream.C:258
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:244
virtual void flush()
Flush stream.
Definition: OSstream.C:231
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A class for handling keywords in dictionaries.
Definition: keyType.H:69
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:97
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:120
@ BEGIN_BLOCK
Definition: token.H:114
@ END_BLOCK
Definition: token.H:115
@ END_STRING
Definition: token.H:121
@ BEGIN_LIST
Definition: token.H:110
@ END_LIST
Definition: token.H:111
A class for handling verbatimStrings, derived from string.
A class for handling words, derived from string.
Definition: word.H:63
#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