www.cemf.ir
iIstream.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 #include "iIstream.hpp"
21 
22 
24 {
25  if (bad())
26  {
28  "stream is bad.\n";
29  fatalExit;
30  }
31  else if (putBack_)
32  {
34  "putBack is already full. \n";
35  fatalExit;
36  }
37  else
38  {
39  putBackToken_ = tok;
40  putBack_ = true;
41  }
42 }
43 
44 
46 {
47 
48  if (bad())
49  {
51  return false;
52  }
53  else if (putBack_)
54  {
55  tok = putBackToken_;
56  putBack_ = false;
57  return true;
58  }
59 
60  return false;
61 }
62 
63 
65 {
66  if (putBack_)
67  {
68  tok = putBackToken_;
69  }
70  else
71  {
72  tok.reset();
73  }
74 
75  return putBack_;
76 }
77 
79 {
81  return 0;
82 }
83 
85 {
86  rewind();
87  return findTokenResume(w);
88 }
89 
91 {
92  token next;
93  bool isFirstToken = true;
94 
95  while( !eof() && good() )
96  {
97  read(next);
98  if(next.error())
99  {
101  "error in reading stream " << name() <<
102  " at line number "<< lineNumber()<<endl;
103  return false;
104  }
105 
106  if( next.isWord() && isFirstToken)
107  {
108  if(next.wordToken() == w )
109  {
110  return true;
111  }
112  }
113 
114  if(next.isEndStatement()|| next.isEndBlock())
115  isFirstToken = true;
116  else
117  isFirstToken = false;
118  }
119 
120  return false;
121 }
122 
123 bool pFlow::iIstream::findTokenSilent( const word & w, int32 limitLine )
124 {
125  rewind();
126  return findTokenResumeSilent(w,limitLine);
127 }
128 
130 {
131  token next;
132  bool isFirstToken = true;
133 
134  while( !eof() && good() && lineNumber()<limitLine )
135  {
136  read(next);
137  if(next.error())
138  {
139  return false;
140  }
141 
142  if( next.isWord() && isFirstToken)
143  {
144  if(next.wordToken() == w) return true;
145  }
146 
147  if(next.isEndStatement()|| next.isEndBlock())
148  isFirstToken = true;
149  else
150  isFirstToken = false;
151  }
152 
153  return false;
154 }
155 
157 (
158  const word& w,
159  word& nextW,
160  bool checkEndStatement
161 )
162 {
163  if( findToken(w) )
164  {
165  // next token
166  token next(*this);
167 
168  if(next.error())
169  {
170  ioErrorInFile( name(), lineNumber());
171  return false;
172  }
173  if( next.isWord() )
174  {
175  nextW = next.wordToken();
176 
177  if( checkEndStatement )
178  {
179  read(next);
180 
181  if( !next.isEndStatement() )
182  {
183  ioErrorInFile(name(), lineNumber()) <<
184  " expected ; but found " << next;
185  return false;
186  }
187 
188  }
189 
190  return true;
191 
192  }else
193  {
194  ioErrorInFile( name(), lineNumber() ) <<
195  " token is not a word" << next <<endl;
196  return false;
197  }
198  }
199 
200  return false;
201 }
202 
203 bool pFlow::iIstream::findTokenAndNextSilent(const word& w, word& nextW, int32 limitLine)
204 {
205  if( findTokenSilent(w, limitLine) )
206  {
207  // next token
208  token next(*this);
209 
210  if(next.error())
211  {
212  return false;
213  }
214 
215  if( next.isWord() )
216  {
217  nextW = next.wordToken();
218 
219  read(next);
220 
221  if( !next.isEndStatement() )
222  {
223  return false;
224  }
225 
226  return true;
227 
228  }else
229  {
230  return false;
231  }
232  }
233 
234  return false;
235 }
236 
237 
238 bool pFlow::iIstream::readBegin(const char* funcName)
239 {
240 
241  const token delimiter(*this);
242 
243  if (delimiter != token::BEGIN_LIST)
244  {
245  setBad();
246 
247  ioErrorInFile( name(), lineNumber() )
248  << "Expected a '" << token::BEGIN_LIST
249  << "' while reading " << funcName
250  << ", found " << delimiter << endl;
251  fatalExit;
252  }
253 
254  return true;
255 }
256 
257 
258 bool pFlow::iIstream::readEnd(const char* funcName)
259 {
260  const token delimiter(*this);
261 
262  if (delimiter != token::END_LIST)
263  {
264  setBad();
265 
266  ioErrorInFile( name(), lineNumber())
267  << "Expected a '" << token::END_LIST
268  << "' while reading " << funcName
269  << ", found "
270  << delimiter <<endl;
271  fatalExit;
272  }
273 
274  return true;
275 }
276 
277 bool pFlow::iIstream::readBeginSquare(const char* funcName)
278 {
279  const token delimiter(*this);
280 
281  if (delimiter != token::BEGIN_SQR)
282  {
283  setBad();
284 
285  ioErrorInFile( name(), lineNumber())
286  << "Expected a '" << token::BEGIN_SQR
287  << "' while reading " << funcName
288  << ", found "
289  << delimiter <<endl;
290  fatalExit;
291  }
292 
293  return true;
294 }
295 
296 bool pFlow::iIstream::readEndSquare(const char* funcName)
297 {
298  const token delimiter(*this);
299 
300  if (delimiter != token::END_SQR)
301  {
302  setBad();
303 
304  ioErrorInFile( name(), lineNumber())
305  << "Expected a '" << token::END_SQR
306  << "' while reading " << funcName
307  << ", found "
308  << delimiter <<endl;
309  fatalExit;
310  }
311 
312  return true;
313 }
314 
315 char pFlow::iIstream::readBeginList(const char* funcName)
316 {
317  const token delimiter(*this);
318 
319  if (delimiter != token::BEGIN_LIST && delimiter != token::BEGIN_BLOCK)
320  {
321  setBad();
322  ioErrorInFile( name(), lineNumber() )
323  << "Expected a '" << token::BEGIN_LIST
324  << "' or a '" << token::BEGIN_BLOCK
325  << "' while reading " << funcName
326  << ", found " << delimiter<<endl;
327  fatalExit;
328 
329  return '\0';
330  }
331 
332  return delimiter.pToken();
333 }
334 
335 
336 char pFlow::iIstream::readEndList(const char* funcName)
337 {
338  const token delimiter(*this);
339 
340  if (delimiter != token::END_LIST && delimiter != token::END_BLOCK)
341  {
342  setBad();
343 
344  ioErrorInFile( name(), lineNumber() )
345  << "Expected a '" << token::END_LIST
346  << "' or a '" << token::END_BLOCK
347  << "' while reading " << funcName
348  << ", found " << delimiter
349  << " at stream position " << endl;
350  fatalExit;
351 
352  return '\0';
353  }
354 
355  return delimiter.pToken();
356 }
357 
358 
359 char pFlow::iIstream::readEndStatement(const char* funcName)
360 {
361  CONSUME_PARAM(funcName);
362  const token delimiter(*this);
363 
364  if (!delimiter.isEndStatement())
365  {
366  setBad();
367  ioErrorInFile( name(), lineNumber() ) <<
368  " expected ; but found " << delimiter << endl;
369  fatalExit;
370 
371  return '\0';
372  }
373 
374  return delimiter.pToken();
375 }
376 
378 {
379  if (!good())
380  {
381  check("iIstream::operator()");
382  fatalExit;
383  }
384 
385  return const_cast<iIstream&>(*this);
386 }
pFlow::iIstream::readBegin
bool readBegin(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: iIstream.cpp:238
notImplementedFunction
#define notImplementedFunction
Report that a function is yet not implemented.
Definition: error.hpp:84
pFlow::iIstream::findTokenAndNextSilent
virtual bool findTokenAndNextSilent(const word &w, word &nextW, int32 limitLine=100)
Definition: iIstream.cpp:203
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::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.hpp:470
iIstream.hpp
pFlow::token::isEndBlock
bool isEndBlock() const
Token is end endBlock.
Definition: tokenI.hpp:460
pFlow::token::END_BLOCK
@ END_BLOCK
Begin block [isseparator].
Definition: token.hpp:96
pFlow::token::error
bool error() const
Token is ERROR.
Definition: tokenI.hpp:402
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::iIstream::readEnd
bool readEnd(const char *funcName)
End read of data chunk, ends with ')' return true or FatalIOError.
Definition: iIstream.cpp:258
pFlow::iIstream::readEndStatement
char readEndStatement(const char *funcName)
End statement character ;.
Definition: iIstream.cpp:359
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::token::BEGIN_BLOCK
@ BEGIN_BLOCK
End dimensions [isseparator].
Definition: token.hpp:95
pFlow::iIstream::findTokenSilent
virtual bool findTokenSilent(const word &w, int32 limitLine=100)
search for all tokesn and find the first word token that matchs
Definition: iIstream.cpp:123
pFlow::iIstream::findTokenResume
virtual bool findTokenResume(const word &w)
search for all tokesn after the current file position and find the first word token tbat matchs w
Definition: iIstream.cpp:90
pFlow::token::reset
void reset()
Reset token to UNDEFINED and clear any allocated storage.
Definition: tokenI.hpp:263
pFlow::iIstream::findTokenResumeSilent
virtual bool findTokenResumeSilent(const word &w, int32 limitLine=100)
search for all tokesn after the current file position and find the first word token tbat matchs w
Definition: iIstream.cpp:129
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:204
pFlow::iIstream::getBack
bool getBack(token &tok)
Get the put back token if there is one and return true.
Definition: iIstream.cpp:45
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::iIstream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{' return starting delimiter or FatalIOError.
Definition: iIstream.cpp:315
pFlow::iIstream::putBack_
bool putBack_
Has a token been put back on the stream?
Definition: iIstream.hpp:45
pFlow::iIstream::putBack
void putBack(const token &tok)
Put back token Only a single put back is permitted.
Definition: iIstream.cpp:23
pFlow::iIstream::findTokenAndNext
virtual bool findTokenAndNext(const word &w, word &nextW, bool checkEndStatement=true)
search for all tokens and find the first word token and also next word token chekck if it is eneded w...
Definition: iIstream.cpp:157
pFlow::token::END_LIST
@ END_LIST
Begin list [isseparator].
Definition: token.hpp:92
pFlow::iIstream::operator()
iIstream & operator()() const
Definition: iIstream.cpp:377
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::iIstream::readBeginSquare
bool readBeginSquare(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: iIstream.cpp:277
pFlow::token::BEGIN_LIST
@ BEGIN_LIST
End entry [isseparator].
Definition: token.hpp:91
pFlow::token::BEGIN_SQR
@ BEGIN_SQR
End list [isseparator].
Definition: token.hpp:93
pFlow::iIstream::putBackToken_
token putBackToken_
The last token put back on the stream.
Definition: iIstream.hpp:48
pFlow::iIstream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}' return closing delimiter or FatalIOError.
Definition: iIstream.cpp:336
CONSUME_PARAM
#define CONSUME_PARAM(x)
Definition: pFlowMacros.hpp:38
pFlow::iIstream::findBinaryBlockStart
virtual size_t findBinaryBlockStart()
It seek for a character sequence that indicates the start of a binary block char sequence is 255 255 ...
Definition: iIstream.cpp:78
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
pFlow::iIstream::findToken
virtual bool findToken(const word &w)
search for all tokesn and find the first word token tbat matchs w
Definition: iIstream.cpp:84
pFlow::token::isEndStatement
bool isEndStatement() const
Token is end statement.
Definition: tokenI.hpp:449
pFlow::token::END_SQR
@ END_SQR
Begin dimensions [isseparator].
Definition: token.hpp:94
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
pFlow::token::isWord
bool isWord() const
Token is word or DIRECTIVE word.
Definition: tokenI.hpp:602
pFlow::iIstream::readEndSquare
bool readEndSquare(const char *funcName)
Begin read of data chunk, starts with '('.
Definition: iIstream.cpp:296
pFlow::iIstream::peekBack
bool peekBack(token &tok)
Peek at the put back token without removing it.
Definition: iIstream.cpp:64