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  writeFormat wF
33 )
34 :
35  iOstream(wF),
36  name_(streamName),
37  os_(os)
38 {
39  if (os_.good())
40  {
41  setOpened();
42  setGood();
43  os_.precision(precision_);
44  }
45  else
46  {
47  setState(os_.rdstate());
48  }
49 }
50 
51 
52 
53 bool pFlow::Ostream::write(const token& tok)
54 {
55  // Direct token handling only for some types
56 
57  switch (tok.type())
58  {
59  case token::tokenType::FLAG :
60  {
61  // silently consume the flag
62  return true;
63  }
64 
65  case token::tokenType::VARIABLE :
66  {
67  writeQuoted(tok.wordToken(), false);
68 
69  return true;
70  }
71 
72  default:
73  break;
74  }
75 
76  return false;
77 }
78 
79 
81 {
82  os_ << c;
83  if (c == token::NL)
84  {
85  ++lineNumber_;
86  }
87  setState(os_.rdstate());
88  return *this;
89 }
90 
91 
93 {
94  lineNumber_ += countChar(str, token::NL);
95  os_ << str;
96  setState(os_.rdstate());
97  return *this;
98 }
99 
100 
102 {
103  os_ << str;
104  setState(os_.rdstate());
105  return *this;
106 }
107 
108 
110 (
111  const word& str,
112  const bool quoted
113 )
114 {
115  if (!quoted)
116  {
117  // Output unquoted, only advance line number on newline
118  lineNumber_ += countChar(str, token::NL);
119  os_ << str;
120 
121  setState(os_.rdstate());
122  return *this;
123  }
124 
125 
126  // Output with surrounding quotes and backslash escaping
127  os_ << token::BEGIN_STRING;
128 
129  unsigned backslash = 0;
130  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
131  {
132  const char c = *iter;
133 
134  if (c == '\\')
135  {
136  ++backslash;
137  continue; // only output after escaped character is known
138  }
139  else if (c == token::NL)
140  {
141  ++lineNumber_;
142  ++backslash; // backslash escape for newline
143  }
144  else if (c == token::END_STRING)
145  {
146  ++backslash; // backslash escape for quote
147  }
148 
149  // output all pending backslashes
150  while (backslash)
151  {
152  os_ << '\\';
153  --backslash;
154  }
155 
156  os_ << c;
157  }
158 
159  // silently drop any trailing backslashes
160  // they would otherwise appear like an escaped end-quote
161  os_ << token::END_STRING;
162 
163  setState(os_.rdstate());
164  return *this;
165 }
166 
167 
168 
170 {
171  os_ << val;
172  setState(os_.rdstate());
173  return *this;
174 }
175 
176 
178 {
179  os_ << val;
180  setState(os_.rdstate());
181  return *this;
182 }
183 
184 /*pFlow::iOstream& pFlow::Ostream::write(const int16 val)
185 {
186  os_ << val;
187  setState(os_.rdstate());
188  return *this;
189 }
190 
191 pFlow::iOstream& pFlow::Ostream::write(const int8 val)
192 {
193  os_ << val;
194  setState(os_.rdstate());
195  return *this;
196 }*/
197 
199 {
200  os_ << val;
201  setState(os_.rdstate());
202  return *this;
203 }
204 
206 {
207  os_ << val;
208  setState(os_.rdstate());
209  return *this;
210 }
211 
213 {
214  os_ << val;
215  setState(os_.rdstate());
216  return *this;
217 }
218 
220 {
221  os_ << val;
222  setState(os_.rdstate());
223  return *this;
224 }
225 
226 
228 {
229  os_ << val;
230  setState(os_.rdstate());
231  return *this;
232 }
233 
235 (
236  const char* binaryData,
237  std::streamsize count
238 )
239 {
240  if ( !isBinary() )
241  {
242  fatalErrorInFunction<<"stream format is not binray. Stream name is "<<
243  name()<<'\n';
244  fatalExit;
245  }
246 
247  os_ << token::BEGIN_LIST;
248  os_.write(binaryData, count);
249  os_ << token::END_LIST;
250 
251  setState(os_.rdstate());
252 
253  return *this;
254 }
255 
256 
257 
258 
260 {
261  for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
262  {
263  os_ << ' ';
264  }
265 }
266 
267 
269 {
270  os_.flush();
271 }
272 
273 
275 {
276  write('\n');
277  os_.flush();
278 }
279 
280 
281 std::ios_base::fmtflags pFlow::Ostream::flags() const
282 {
283  return os_.flags();
284 }
285 
286 
287 std::ios_base::fmtflags pFlow::Ostream::flags(const ios_base::fmtflags f)
288 {
289  return os_.flags(f);
290 }
291 
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
296 {
297  return os_.fill();
298 }
299 
300 
301 char pFlow::Ostream::fill(const char fillch)
302 {
303  return os_.fill(fillch);
304 }
305 
306 
308 {
309  return os_.width();
310 }
311 
312 
313 int pFlow::Ostream::width(const int w)
314 {
315  return os_.width(w);
316 }
317 
318 
320 {
321  return os_.precision();
322 }
323 
324 
326 {
327  return os_.precision(p);
328 }
329 
330 
331 // ************************************************************************* //
pFlow::Ostream::writeQuoted
iOstream & writeQuoted(const word &str, const bool quoted=true) override
Write std::string surrounded by quotes.
Definition: Ostream.cpp:110
Ostream.hpp
pFlow::Ostream::flags
virtual ios_base::fmtflags flags() const
Return flags of stream.
Definition: Ostream.cpp:281
pFlow::Ostream::flush
void flush() override
Flush stream.
Definition: Ostream.cpp:268
pFlow::token::type
tokenType type() const
Definition: tokenI.hpp:284
fatalExit
#define fatalExit
Definition: error.hpp:57
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::Ostream::indent
void indent() override
Add indentation characters.
Definition: Ostream.cpp:259
pFlow::token::NL
@ NL
Newline [isspace].
Definition: token.hpp:86
pFlow::Ostream::width
int width() const override
Get width of output field.
Definition: Ostream.cpp:307
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::Ostream::fill
char fill() const override
Get padding character.
Definition: Ostream.cpp:295
pFlow::Ostream::precision
int precision() const override
Get precision of output field.
Definition: Ostream.cpp:319
pFlow::Ostream::endl
void endl() override
Add newline and flush stream.
Definition: Ostream.cpp:274
pFlow::uint16
unsigned short int uint16
Definition: builtinTypes.hpp:57
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Begin string with double quote.
Definition: token.hpp:104
pFlow::token::END_LIST
@ END_LIST
End list [isseparator].
Definition: token.hpp:90
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
pFlow::Ostream::Ostream
Ostream(std::ostream &os, const word &streamName, writeFormat wf=ASCII)
Definition: Ostream.cpp:29
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.hpp:89
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:57
pFlow::token::END_STRING
@ END_STRING
End string with double quote.
Definition: token.hpp:105
pFlow::Ostream::write
bool write(const token &tok) override
Write Functions.
Definition: Ostream.cpp:53
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