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 
204 {
205  int64 lval(0);
206  is>>lval;
207  val = static_cast<int16>(lval);
208  return is;
209 }
210 
212 {
213  int64 lval(0);
214  is>>lval;
215  val = static_cast<int8>(lval);
216  return is;
217 }
218 
220 {
221  int64 lval(0);
222  is>>lval;
223  val = static_cast<label>(lval);
224  return is;
225 }
226 
228 {
229  int64 lval(0);
230  is>>lval;
231  val = static_cast<uint32>(lval);
232  return is;
233 }
234 
236 {
237  int64 lval(0);
238  is>>lval;
239  val = static_cast<uint16>(lval);
240  return is;
241 }
242 
243 
244 inline pFlow::iIstream& pFlow::operator>>( iIstream& is, float& val)
245 {
246  token t(is);
247 
248  if (!t.good())
249  {
250  ioErrorInFile(is.name(), is.lineNumber())
251  << "Bad token - could not get float value";
252  fatalExit;
253  is.setBad();
254  return is;
255  }
256 
257  if (t.isNumber())
258  {
259  val = t.number();
260  }
261  else
262  {
263  ioErrorInFile(is.name(), is.lineNumber())
264  << "Wrong token type - expected float value, found "
265  << t;
266  fatalExit;
267  is.setBad();
268  return is;
269  }
270 
271  is.check(FUNCTION_NAME);
272  return is;
273 }
274 
275 inline pFlow::iIstream& pFlow::operator>>( iIstream& is, double& val)
276 {
277  token t(is);
278  if (!t.good())
279  {
280  ioErrorInFile(is.name(), is.lineNumber())
281  << "Bad token - could not get double value";
282  fatalExit;
283  is.setBad();
284  return is;
285  }
286 
287  if (t.isNumber())
288  {
289  val = t.number();
290  }
291  else
292  {
293  ioErrorInFile(is.name(), is.lineNumber())
294  << "Wrong token type - expected double value, found "
295  << t;
296  fatalExit;
297  is.setBad();
298  return is;
299  }
300 
301  is.check(FUNCTION_NAME);
302  return is;
303 }
304 
pFlow::iIstream::lookupDataOrSet
T lookupDataOrSet(const word &keyword, const T &setVal)
Definition: iIstreamI.hpp:68
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::token::stringToken
const word & stringToken() const
Definition: tokenI.hpp:624
pFlow::token::error
bool error() const
Definition: tokenI.hpp:384
pFlow::token::good
bool good() const
Definition: tokenI.hpp:372
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::token::number
real number() const
Definition: tokenI.hpp:568
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::iIstream::findKeywordAndVal
bool findKeywordAndVal(const word &keyword, T &val, bool checkEndStatement=true)
Definition: iIstreamI.hpp:24
pFlow::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.cpp:42
pFlow::int16
short int int16
Definition: builtinTypes.hpp:51
pFlow::uint16
unsigned short int uint16
Definition: builtinTypes.hpp:57
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::iIstream::nextData
bool nextData(const word &keyword, T &data)
Definition: iIstreamI.hpp:81
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::IOstream::setBad
void setBad()
Definition: IOstream.hpp:238
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::iIstream::lookupData
T lookupData(const word &keyword)
Definition: iIstreamI.hpp:52
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::iIstream::findToken
virtual bool findToken(const word &w)
Definition: iIstream.cpp:60
pFlow::token::isNumber
bool isNumber() const
Definition: tokenI.hpp:562
pFlow::token::isEndStatement
bool isEndStatement() const
Definition: tokenI.hpp:431
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::token::isString
bool isString() const
Definition: tokenI.hpp:615
pFlow::token::isWord
bool isWord() const
Definition: tokenI.hpp:584