www.cemf.ir
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 
21 #include "error.hpp"
22 #include "token.hpp"
23 #include "Ostream.hpp"
24 
25 
27 (
28  std::ostream& os,
29  const word& streamName,
30  writeFormat wF
31 )
32 :
33  iOstream(wF),
34  name_(streamName),
35  os_(os)
36 {
37  if (os_.good())
38  {
39  setOpened();
40  setGood();
41  os_.precision(precision_);
42  }
43  else
44  {
45  setState(os_.rdstate());
46  }
47 }
48 
49 
50 
51 bool pFlow::Ostream::write(const token& tok)
52 {
53  // Direct token handling only for some types
54 
55  switch (tok.type())
56  {
57  case token::tokenType::FLAG :
58  {
59  // silently consume the flag
60  return true;
61  }
62 
63  case token::tokenType::VARIABLE :
64  {
65  writeQuoted(tok.wordToken(), false);
66 
67  return true;
68  }
69 
70  default:
71  break;
72  }
73 
74  return false;
75 }
76 
77 
79 {
80  os_ << c;
81  if (c == token::NL)
82  {
83  ++lineNumber_;
84  }
85  setState(os_.rdstate());
86  return *this;
87 }
88 
89 
91 {
92  lineNumber_ += countChar(str, token::NL);
93  os_ << str;
94  setState(os_.rdstate());
95  return *this;
96 }
97 
98 
100 {
101  os_ << str;
102  setState(os_.rdstate());
103  return *this;
104 }
105 
106 
108 (
109  const word& str,
110  const bool quoted
111 )
112 {
113  if (!quoted)
114  {
115  // Output unquoted, only advance line number on newline
116  lineNumber_ += countChar(str, token::NL);
117  os_ << str;
118 
119  setState(os_.rdstate());
120  return *this;
121  }
122 
123 
124  // Output with surrounding quotes and backslash escaping
125  os_ << token::BEGIN_STRING;
126 
127  unsigned backslash = 0;
128  for (auto iter = str.cbegin(); iter != str.cend(); ++iter)
129  {
130  const char c = *iter;
131 
132  if (c == '\\')
133  {
134  ++backslash;
135  continue; // only output after escaped character is known
136  }
137  else if (c == token::NL)
138  {
139  ++lineNumber_;
140  ++backslash; // backslash escape for newline
141  }
142  else if (c == token::END_STRING)
143  {
144  ++backslash; // backslash escape for quote
145  }
146 
147  // output all pending backslashes
148  while (backslash)
149  {
150  os_ << '\\';
151  --backslash;
152  }
153 
154  os_ << c;
155  }
156 
157  // silently drop any trailing backslashes
158  // they would otherwise appear like an escaped end-quote
159  os_ << token::END_STRING;
160 
161  setState(os_.rdstate());
162  return *this;
163 }
164 
165 
166 
168 {
169  os_ << val;
170  setState(os_.rdstate());
171  return *this;
172 }
173 
174 
176 {
177  os_ << val;
178  setState(os_.rdstate());
179  return *this;
180 }
181 
183 {
184  os_ << val;
185  setState(os_.rdstate());
186  return *this;
187 }
188 
190 {
191  os_ << val;
192  setState(os_.rdstate());
193  return *this;
194 }
195 
197 {
198  os_ << val;
199  setState(os_.rdstate());
200  return *this;
201 }
202 
204 {
205  os_ << val;
206  setState(os_.rdstate());
207  return *this;
208 }
209 
211 {
212  os_ << val;
213  setState(os_.rdstate());
214  return *this;
215 }
216 
217 
219 {
220  os_ << val;
221  setState(os_.rdstate());
222  return *this;
223 }
224 
226 {
227  os_ << val;
228  setState(os_.rdstate());
229  return *this;
230 }
231 
233 (
234  const char* binaryData,
235  std::streamsize count
236 )
237 {
238  if ( !isBinary() )
239  {
240  fatalErrorInFunction<<"stream format is not binray. Stream name is "<<
241  name()<<'\n';
242  fatalExit;
243  }
244 
245  os_ << token::BEGIN_LIST;
246  os_.write(binaryData, count);
247  os_ << token::END_LIST;
248 
249  return *this;
250 }
251 
253 {
254  write( static_cast<char>(255));
255  write( static_cast<char>(255));
256  write( static_cast<char>(255));
257  write( static_cast<char>(0));
258  return *this;
259 }
260 
261 void pFlow::Ostream::seek(size_t pos)
262 {
263  os_.seekp(pos, std::ios_base::cur);
264 }
265 
267 {
268  for (unsigned short i = 0; i < indentLevel_*indentSize_; ++i)
269  {
270  os_ << ' ';
271  }
272 }
273 
275 {
276  this->endl();
277  this->flush();
278 }
279 
281 {
282  os_.seekp(0, std::ios_base::end);
283  this->endl();
284  this->flush();
285 }
286 
288 {
289  os_.flush();
290 }
291 
293 {
294  write('\n');
295  os_.flush();
296 }
297 
298 std::ios_base::fmtflags pFlow::Ostream::flags() const
299 {
300  return os_.flags();
301 }
302 
303 
304 std::ios_base::fmtflags pFlow::Ostream::flags(const ios_base::fmtflags f)
305 {
306  return os_.flags(f);
307 }
308 
309 
310 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
311 
313 {
314  return os_.fill();
315 }
316 
317 
318 char pFlow::Ostream::fill(const char fillch)
319 {
320  return os_.fill(fillch);
321 }
322 
323 
325 {
326  return os_.width();
327 }
328 
329 
330 int pFlow::Ostream::width(const int w)
331 {
332  return os_.width(w);
333 }
334 
335 
337 {
338  return os_.precision();
339 }
340 
341 
343 {
344  return os_.precision(p);
345 }
346 
347 
348 // ************************************************************************* //
pFlow::Ostream::writeQuoted
iOstream & writeQuoted(const word &str, const bool quoted=true) override
Write std::string surrounded by quotes.
Definition: Ostream.cpp:108
Ostream.hpp
pFlow::Ostream::flags
virtual ios_base::fmtflags flags() const
Return flags of output stream.
Definition: Ostream.cpp:298
pFlow::Ostream::flush
void flush() override
Flush stream.
Definition: Ostream.cpp:287
pFlow::token::type
tokenType type() const
Return the token type.
Definition: tokenI.hpp:302
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
token.hpp
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::Ostream::indent
void indent() override
Add indentation characters.
Definition: Ostream.cpp:266
pFlow::token::NL
@ NL
Tab [isspace].
Definition: token.hpp:88
pFlow::Ostream::width
int width() const override
Get width of output field.
Definition: Ostream.cpp:324
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::Ostream::endOfBinaryStreaming
void endOfBinaryStreaming() override
Reach end of file add a new line and flush stream.
Definition: Ostream.cpp:280
pFlow::Ostream::fill
char fill() const override
Get the current padding character.
Definition: Ostream.cpp:312
pFlow::flush
iOstream & flush(iOstream &os)
Flush stream.
Definition: iOstream.hpp:333
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::Ostream::precision
int precision() const override
Get precision of output field.
Definition: Ostream.cpp:336
pFlow::Ostream::endl
void endl() override
Add newline and flush stream.
Definition: Ostream.cpp:292
pFlow::Ostream::startOfBinaryStreaming
void startOfBinaryStreaming() override
Add a new line and flush stream.
Definition: Ostream.cpp:274
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::Ostream::writeBinaryBlockFlag
iOstream & writeBinaryBlockFlag() override
Write the flag to indicate the start of a binary block.
Definition: Ostream.cpp:252
pFlow::token::BEGIN_STRING
@ BEGIN_STRING
Divide [isseparator].
Definition: token.hpp:106
pFlow::token::END_LIST
@ END_LIST
Begin list [isseparator].
Definition: token.hpp:92
pFlow::Ostream::seek
void seek(size_t pos) override
Definition: Ostream.cpp:261
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)
Construct from components.
Definition: Ostream.cpp:27
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
End entry [isseparator].
Definition: token.hpp:91
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:59
pFlow::token::END_STRING
@ END_STRING
Begin string with double quote.
Definition: token.hpp:107
pFlow::Ostream::write
bool write(const token &tok) override
Write token to stream or otherwise handle it.
Definition: Ostream.cpp:51
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::countChar
int32 countChar(const word &s, const char c)
Count numer of chars c in a word.
Definition: bTypesFunctions.cpp:28
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
error.hpp