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-2019 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 {
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
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 
179 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
180 {
181  if (format() != BINARY)
182  {
184  << "stream format not binary"
185  << abort(FatalIOError);
186  }
187 
188  os_ << token::BEGIN_LIST;
189  os_.write(buf, count);
190  os_ << token::END_LIST;
191 
192  setState(os_.rdstate());
193 
194  return *this;
195 }
196 
197 
199 {
200  for (unsigned short i = 0; i < indentLevel_*indentSize_; i++)
201  {
202  os_ << ' ';
203  }
204 }
205 
206 
208 {
209  os_.flush();
210 }
211 
212 
214 {
215  write('\n');
216  os_.flush();
217 }
218 
219 
220 std::ios_base::fmtflags Foam::OSstream::flags() const
221 {
222  return os_.flags();
223 }
224 
225 
226 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
227 {
228  return os_.flags(f);
229 }
230 
231 
232 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
233 
235 {
236  return os_.width();
237 }
238 
239 
240 int Foam::OSstream::width(const int w)
241 {
242  return os_.width(w);
243 }
244 
245 
247 {
248  return os_.precision();
249 }
250 
251 
253 {
254  return os_.precision(p);
255 }
256 
257 
258 // ************************************************************************* //
A class for handling verbatimStrings, derived from string.
label lineNumber_
Definition: IOstream.H:231
unsigned short indentLevel_
Current indent level.
Definition: Ostream.H:67
size_type count(const char) const
Count and return the number of a given character in the string.
Definition: string.C:47
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: OSstream.C:220
virtual int precision() const
Get precision of output field.
Definition: OSstream.C:246
virtual void endl()
Add newline and flush stream.
Definition: OSstream.C:213
virtual Ostream & writeQuoted(const std::string &, const bool quoted=true)
Write std::string with optional double quotes.
Definition: OSstream.C:78
virtual void indent()
Add indentation characters.
Definition: OSstream.C:198
const dimensionedScalar & c
Speed of light in a vacuum.
A class for handling words, derived from string.
Definition: word.H:59
virtual void flush()
Flush stream.
Definition: OSstream.C:207
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:54
labelList f(nPoints)
virtual Ostream & write(const char)
Write character.
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:234
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
static const unsigned short indentSize_
Number of spaces per indent level.
Definition: Ostream.H:64
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:76
IOerror FatalIOError