Ostream.cpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 // based on OpenFOAM stream, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 #include "error.hpp"
24 #include "token.hpp"
25 #include "Ostream.hpp"
26 
27 
29 (
30  std::ostream& os,
31  const word& streamName
32 )
33 :
34  iOstream(),
35  name_(streamName),
36  os_(os)
37 {
38  if (os_.good())
39  {
40  setOpened();
41  setGood();
42  os_.precision(precision_);
43  }
44  else
45  {
46  setState(os_.rdstate());
47  }
48 }
49 
50 
51 
52 bool pFlow::Ostream::write(const token& tok)
53 {
54  // Direct token handling only for some types
55 
56  switch (tok.type())
57  {
58  case token::tokenType::FLAG :
59  {
60  // silently consume the flag
61  return true;
62  }
63 
64  case token::tokenType::VARIABLE :
65  {
66  writeQuoted(tok.wordToken(), false);
67 
68  return true;
69  }
70 
71  default:
72  break;
73  }
74 
75  return false;
76 }
77 
78 
80 {
81  os_ << c;
82  if (c == token::NL)
83  {
84  ++lineNumber_;
85  }
86  setState(os_.rdstate());
87  return *this;
88 }
89 
90 
92 {
93  lineNumber_ += countChar(str, token::NL);
94  os_ << str;
95  setState(os_.rdstate());
96  return *this;
97 }
98 
99 
101 {
102  os_ << str;
103  setState(os_.rdstate());
104  return *this;
105 }
106 
107 
109 (
110  const word& str,
111  const bool quoted
112 )
113 {
114  if (!quoted)
115  {
116  // Output unquoted, only advance line number on newline
117  lineNumber_ += countChar(str, token::NL);
118  os_ << str;
119 
120  setState(os_.rdstate());
121  return *this;
122  }
123 
124 
125  // Output with surrounding quotes and backslash escaping
126  os_ << token::BEGIN_STRING;
127 
128  unsigned backslash = 0;
129  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
130  {
131  const char c = *iter;
132 
133  if (c == '\\')
134  {
135  ++backslash;
136  continue; // only output after escaped character is known
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 all 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  setState(os_.rdstate());
163  return *this;
164 }
165 
166 
167 
169 {
170  os_ << val;
171  setState(os_.rdstate());
172  return *this;
173 }
174 
175 
177 {
178  os_ << val;
179  setState(os_.rdstate());
180  return *this;
181 }
182 
183 /*pFlow::iOstream& pFlow::Ostream::write(const int16 val)
184 {
185  os_ << val;
186  setState(os_.rdstate());
187  return *this;
188 }
189 
190 pFlow::iOstream& pFlow::Ostream::write(const int8 val)
191 {
192  os_ << val;
193  setState(os_.rdstate());
194  return *this;
195 }*/
196 
198 {
199  os_ << val;
200  setState(os_.rdstate());
201  return *this;
202 }
203 
205 {
206  os_ << val;
207  setState(os_.rdstate());
208  return *this;
209 }
210 
212 {
213  os_ << val;
214  setState(os_.rdstate());
215  return *this;
216 }
217 
219 {
220  os_ << val;
221  setState(os_.rdstate());
222  return *this;
223 }
224 
225 
227 {
228  os_ << val;
229  setState(os_.rdstate());
230  return *this;
231 }
232 
233 
234 
236 {
237  for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
238  {
239  os_ << ' ';
240  }
241 }
242 
243 
245 {
246  os_.flush();
247 }
248 
249 
251 {
252  write('\n');
253  os_.flush();
254 }
255 
256 
257 std::ios_base::fmtflags pFlow::Ostream::flags() const
258 {
259  return os_.flags();
260 }
261 
262 
263 std::ios_base::fmtflags pFlow::Ostream::flags(const ios_base::fmtflags f)
264 {
265  return os_.flags(f);
266 }
267 
268 
269 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
270 
272 {
273  return os_.fill();
274 }
275 
276 
277 char pFlow::Ostream::fill(const char fillch)
278 {
279  return os_.fill(fillch);
280 }
281 
282 
284 {
285  return os_.width();
286 }
287 
288 
289 int pFlow::Ostream::width(const int w)
290 {
291  return os_.width(w);
292 }
293 
294 
296 {
297  return os_.precision();
298 }
299 
300 
302 {
303  return os_.precision(p);
304 }
305 
306 
307 // ************************************************************************* //
pFlow::Ostream::writeQuoted
virtual iOstream & writeQuoted(const word &str, const bool quoted=true) override
Definition: Ostream.cpp:109
pFlow::Ostream::flush
virtual void flush()
Definition: Ostream.cpp:244
Ostream.hpp
pFlow::Ostream::flags
virtual ios_base::fmtflags flags() const
Definition: Ostream.cpp:257
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
pFlow::Ostream::endl
virtual void endl()
Definition: Ostream.cpp:250
pFlow::token
Definition: token.hpp:42
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::token::NL
@ NL
Newline [isspace].
Definition: token.hpp:86
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::Ostream::precision
virtual int precision() const
Definition: Ostream.cpp:295
pFlow::uint16
unsigned short int uint16
Definition: builtinTypes.hpp:57
pFlow::Ostream::Ostream
Ostream(std::ostream &os, const word &streamName)
Definition: Ostream.cpp:29
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Begin string with double quote.
Definition: token.hpp:104
pFlow::Ostream::width
virtual int width() const
Definition: Ostream.cpp:283
pFlow::Ostream::fill
virtual char fill() const
Definition: Ostream.cpp:271
pFlow::Ostream::indent
virtual void indent()
Definition: Ostream.cpp:235
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::token::END_STRING
@ END_STRING
End string with double quote.
Definition: token.hpp:105
pFlow::Ostream::write
virtual bool write(const token &tok) override
Definition: Ostream.cpp:52
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::countChar
int32 countChar(const word &s, const char c)
Definition: bTypesFunctions.cpp:28
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
error.hpp