FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/error.H>
27 #include "OSstream.H"
28 #include <OpenFOAM/token.H>
29 
30 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 
33 {
34  return *this;
35 }
36 
37 
39 {
40  os_ << c;
41  if (c == token::NL)
42  {
43  lineNumber_++;
44  }
45  setState(os_.rdstate());
46  return *this;
47 }
48 
49 
51 {
52  lineNumber_ += string(str).count(token::NL);
53  os_ << str;
54  setState(os_.rdstate());
55  return *this;
56 }
57 
58 
60 {
61  os_ << str;
62  setState(os_.rdstate());
63  return *this;
64 }
65 
66 
68 {
69  os_ << token::BEGIN_STRING;
70 
71  register int backslash = 0;
72  for (string::const_iterator iter = str.begin(); iter != str.end(); ++iter)
73  {
74  register char c = *iter;
75 
76  if (c == '\\')
77  {
78  backslash++;
79  // suppress output until we know if other characters follow
80  continue;
81  }
82  else if (c == token::NL)
83  {
84  lineNumber_++;
85  backslash++; // backslash escape for newline
86  }
87  else if (c == token::END_STRING)
88  {
89  backslash++; // backslash escape for quote
90  }
91 
92  // output pending backslashes
93  while (backslash)
94  {
95  os_ << '\\';
96  backslash--;
97  }
98 
99  os_ << c;
100  }
101 
102  // silently drop any trailing backslashes
103  // they would otherwise appear like an escaped end-quote
104 
105  os_ << token::END_STRING;
106 
107  setState(os_.rdstate());
108  return *this;
109 }
110 
111 
113 (
114  const std::string& str,
115  const bool quoted
116 )
117 {
118  if (quoted)
119  {
120  os_ << token::BEGIN_STRING;
121 
122  register int backslash = 0;
123  for
124  (
125  string::const_iterator iter = str.begin();
126  iter != str.end();
127  ++iter
128  )
129  {
130  register char c = *iter;
131 
132  if (c == '\\')
133  {
134  backslash++;
135  // suppress output until we know if other characters follow
136  continue;
137  }
138  else if (c == token::NL)
139  {
140  lineNumber_++;
141  backslash++; // backslash escape for newline
142  }
143  else if (c == token::END_STRING)
144  {
145  backslash++; // backslash escape for quote
146  }
147 
148  // output pending backslashes
149  while (backslash)
150  {
151  os_ << '\\';
152  backslash--;
153  }
154 
155  os_ << c;
156  }
157 
158  // silently drop any trailing backslashes
159  // they would otherwise appear like an escaped end-quote
160  os_ << token::END_STRING;
161  }
162  else
163  {
164  // output unquoted string, only advance line number on newline
165  lineNumber_ += string(str).count(token::NL);
166  os_ << str;
167  }
168 
169  setState(os_.rdstate());
170  return *this;
171 }
172 
173 
175 {
176  os_ << val;
177  setState(os_.rdstate());
178  return *this;
179 }
180 
181 
183 {
184  os_ << val;
185  setState(os_.rdstate());
186  return *this;
187 }
188 
189 
191 {
192  os_ << val;
193  setState(os_.rdstate());
194  return *this;
195 }
196 
197 
198 Foam::Ostream& Foam::OSstream::write(const char* buf, std::streamsize count)
199 {
200  if (format() != BINARY)
201  {
202  FatalIOErrorIn("Ostream::write(const char*, std::streamsize)", *this)
203  << "stream format not binary"
204  << abort(FatalIOError);
205  }
206 
207  os_ << token::BEGIN_LIST;
208  os_.write(buf, count);
209  os_ << token::END_LIST;
210 
211  setState(os_.rdstate());
212 
213  return *this;
214 }
215 
216 
218 {
219  for (register unsigned short i = 0; i < indentLevel_*indentSize_; i++)
220  {
221  os_ << ' ';
222  }
223 }
224 
225 
227 {
228  os_.flush();
229 }
230 
231 
232 // Add carriage return and flush stream
234 {
235  write('\n');
236  os_.flush();
237 }
238 
239 
240 // Get flags of output stream
241 std::ios_base::fmtflags Foam::OSstream::flags() const
242 {
243  return os_.flags();
244 }
245 
246 
247 // Set flags of output stream
248 std::ios_base::fmtflags Foam::OSstream::flags(const ios_base::fmtflags f)
249 {
250  return os_.flags(f);
251 }
252 
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 
257 // Get width of output field
259 {
260  return os_.width();
261 }
262 
263 // Set width of output field (and return old width)
264 int Foam::OSstream::width(const int w)
265 {
266  return os_.width(w);
267 }
268 
269 // Get precision of output field
271 {
272  return os_.precision();
273 }
274 
275 // Set precision of output field (and return old precision)
277 {
278  return os_.precision(p);
279 }
280 
281 
282 // ************************ vim: set sw=4 sts=4 et: ************************ //