www.cemf.ir
iIstreamI.hpp
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 
22 // * * * * * * * * * * * * * * templates * * * * * * * * * * * * * * * * *//
23 template<typename T>
24 bool pFlow::iIstream::findKeywordAndVal(const word& keyword, T& val, bool checkEndStatement)
25 {
26  if( findToken(keyword) )
27  {
28  iIstream& is = *this;
29  is >> val;
30 
31  if( checkEndStatement )
32  {
33  token next(is);
34 
35  if( !next.good() || !next.isEndStatement() )
36  {
37  return false;
38  }
39  }
40  }
41  else
42  {
43  return false;
44  }
45 
46  return true;
47 }
48 
49 
50 template<typename T>
52 (
53  const word& keyword
54 )
55 {
56  T val;
57  if( !findKeywordAndVal(keyword, val) )
58  {
59  ioErrorInFile( name(), lineNumber() )<<
60  " error in finding keyword "<< keyword <<" and value next to it.";
61  fatalExit;
62  }
63 
64  return val;
65 }
66 
67 template<typename T>
68 T pFlow::iIstream::lookupDataOrSet(const word& keyword, const T& setVal)
69 {
70  T val;
71 
72  if(!findKeywordAndVal(keyword, val))
73  {
74  val = setVal;
75  }
76 
77  return val;
78 }
79 
80 template<typename T>
81 bool pFlow::iIstream::nextData(const word& keyword, T& data)
82 {
83 
84  token next;
85 
86  if( !eof() && good() )
87  {
88  read(next);
89  if(next.error())
90  {
91  ioErrorInFile(name(), lineNumber());
92  return false;
93  }
94 
95  if( !next.isWord() )
96  {
97  ioErrorInFile(name(), lineNumber())<<
98  " expected word token.";
99  return false;
100  }
101  if(next.wordToken() != keyword)
102  {
103  ioErrorInFile(name(), lineNumber())<<
104  " expected keyword "<< keyword << " but found "<<next.wordToken()<<endl;
105  return false;
106  }
107 
108  iIstream& is = *this;
109  is >> data;
110 
111  read(next);
112 
113  if( !next.good() || !next.isEndStatement() )
114  {
115  ioErrorInFile(name(),lineNumber())<<
116  " expected ; but found "<< next<<endl;
117  return false;
118  }
119 
120  return true;
121 
122  }
123 
124  return false;
125 
126 }
127 
128 // * * * * * * * * * * * * * * IO operations * * * * * * * * * * * * * * * * *//
130 {
131  token t(is);
132  if (!t.good())
133  {
134  ioErrorInFile(is.name(), is.lineNumber())
135  << "Bad token - could not get word/string value";
136  fatalExit;
137  is.setBad();
138  return is;
139  }
140 
141  if (t.isString())
142  {
143  w = t.stringToken();
144  }
145  else if(t.isWord() )
146  {
147  w = t.wordToken();
148  }
149  else
150  {
151  ioErrorInFile(is.name(), is.lineNumber())
152  << "Wrong token type - expected word/string value, found "
153  << t;
154  fatalExit;
155  is.setBad();
156  return is;
157  }
158 
159  is.check(FUNCTION_NAME);
160  return is;
161 }
162 
164 {
165  token t(is);
166 
167  if (!t.good())
168  {
169  ioErrorInFile(is.name(), is.lineNumber())
170  << "Bad token - could not get int64 value";
171  fatalExit;
172  is.setBad();
173  return is;
174  }
175 
176  if (t.isNumber())
177  {
178  val = t.number();
179  }
180  else
181  {
182  ioErrorInFile(is.name(), is.lineNumber())
183  << "Wrong token type - expected int64 value, found "
184  << t;
185  fatalExit;
186  is.setBad();
187  return is;
188 
189  }
190 
191  is.check(FUNCTION_NAME);
192  return is;
193 }
194 
196 {
197  int64 lval(0);
198  is>>lval;
199  val = static_cast<int32>(lval);
200  return is;
201 }
202 
203 
205 {
206  int64 lval(0);
207  is>>lval;
208  val = static_cast<int8>(lval);
209  return is;
210 }
211 
213 {
214  int64 lval(0);
215  is>>lval;
216  val = static_cast<uint64>(lval);
217  return is;
218 }
219 
221 {
222  int64 lval(0);
223  is>>lval;
224  val = static_cast<uint32>(lval);
225  return is;
226 }
227 
229 {
230  int64 lval(0);
231  is>>lval;
232  val = static_cast<uint8>(lval);
233  return is;
234 }
235 
236 
237 inline pFlow::iIstream& pFlow::operator>>( iIstream& is, float& val)
238 {
239  token t(is);
240 
241  if (!t.good())
242  {
243  ioErrorInFile(is.name(), is.lineNumber())
244  << "Bad token - could not get float value";
245  fatalExit;
246  is.setBad();
247  return is;
248  }
249 
250  if (t.isNumber())
251  {
252  val = t.number();
253  }
254  else
255  {
256  ioErrorInFile(is.name(), is.lineNumber())
257  << "Wrong token type - expected float value, found "
258  << t;
259  fatalExit;
260  is.setBad();
261  return is;
262  }
263 
264  is.check(FUNCTION_NAME);
265  return is;
266 }
267 
268 inline pFlow::iIstream& pFlow::operator>>( iIstream& is, double& val)
269 {
270  token t(is);
271  if (!t.good())
272  {
273  ioErrorInFile(is.name(), is.lineNumber())
274  << "Bad token - could not get double value";
275  fatalExit;
276  is.setBad();
277  return is;
278  }
279 
280  if (t.isNumber())
281  {
282  val = t.number();
283  }
284  else
285  {
286  ioErrorInFile(is.name(), is.lineNumber())
287  << "Wrong token type - expected double value, found "
288  << t;
289  fatalExit;
290  is.setBad();
291  return is;
292  }
293 
294  is.check(FUNCTION_NAME);
295  return is;
296 }
297 
298 inline pFlow::iIstream& pFlow::operator>>( iIstream& is, size_t& val)
299 {
300  token t(is);
301  if (!t.good())
302  {
303  ioErrorInFile(is.name(), is.lineNumber())
304  << "Bad token - could not get double value";
305  fatalExit;
306  is.setBad();
307  return is;
308  }
309 
310  if (t.isNumber())
311  {
312  val = static_cast<size_t>(t.number());
313  }
314  else
315  {
316  ioErrorInFile(is.name(), is.lineNumber())
317  << "Wrong token type - expected double value, found "
318  << t;
319  fatalExit;
320  is.setBad();
321  return is;
322  }
323 
324  is.check(FUNCTION_NAME);
325  return is;
326 }
327 
328 
329 
pFlow::iIstream::lookupDataOrSet
T lookupDataOrSet(const word &keyword, const T &setVal)
lookup for keyword and data; set to setVal if lookup fails.
Definition: iIstreamI.hpp:68
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::stringToken
const word & stringToken() const
Return const reference to the string contents.
Definition: tokenI.hpp:642
pFlow::token::error
bool error() const
Token is ERROR.
Definition: tokenI.hpp:402
pFlow::token::good
bool good() const
True if token is not UNDEFINED or ERROR.
Definition: tokenI.hpp:390
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::token::number
real number() const
Return int64, float or double value.
Definition: tokenI.hpp:586
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::iIstream::findKeywordAndVal
bool findKeywordAndVal(const word &keyword, T &val, bool checkEndStatement=true)
find a pair of keyword and data terminated by ; keyword data; return false if keyword does not exist ...
Definition: iIstreamI.hpp:24
pFlow::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:42
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::iIstream::nextData
bool nextData(const word &keyword, T &data)
read the data next to keword keyword data; check the keyword is correct or not
Definition: iIstreamI.hpp:81
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::IOstream::setBad
void setBad()
Set stream to be bad.
Definition: IOstream.hpp:274
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::iIstream::lookupData
T lookupData(const word &keyword)
lookup for keyword and data; fatalExit if fails
Definition: iIstreamI.hpp:52
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
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::isNumber
bool isNumber() const
Token is int, float or duble.
Definition: tokenI.hpp:580
pFlow::token::isEndStatement
bool isEndStatement() const
Token is end statement.
Definition: tokenI.hpp:449
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
pFlow::token::isString
bool isString() const
Token is STRING, VARIABLE or VERBATIM string.
Definition: tokenI.hpp:633
pFlow::token::isWord
bool isWord() const
Token is word or DIRECTIVE word.
Definition: tokenI.hpp:602