dataEntry.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 dictionary, with some modifications/simplifications
21 // to be tailored to our needs
22 
23 
24 #include "dataEntry.hpp"
25 #include "dictionary.hpp"
26 #include "error.hpp"
27 #include "iIstream.hpp"
28 #include "iOstream.hpp"
29 #include "iTstream.hpp"
30 #include "oTstream.hpp"
31 
33 
35 (
36  iIstream& is
37 )
38 {
39  // reset the stream (only tokens)
40  tokenStream_.reset();
41 
42  int32 level = 0;
43  token nextTok;
44 
45  while
46  (
47  !is.read(nextTok).bad() && nextTok.good() &&
48  !(level == 0 && nextTok == token::END_STATEMENT)
49  )
50  {
51 
52  if( nextTok.isPunctuation() )
53  {
54  auto t = nextTok.pToken();
55  if( t == token::BEGIN_LIST ||
56  t == token::BEGIN_BLOCK ||
57  t == token::BEGIN_SQR
58  )
59  {
60  level ++;
61  }
62  else if (
63  t == token::END_LIST ||
64  t == token::END_BLOCK ||
65  t == token::END_SQR )
66  {
67  level--;
68  }
69  }
70 
71  // checks if there are imbalance (, { or [
72  if( level < 0 )
73  {
74  ioErrorInFile(is.name(), is.lineNumber()) <<
75  "number of opening and closing ( or, [ or { does not match, closing is greater than opening \n";
76  fatalExit;
77  }
78 
79  // add token to tokenStream
80  // iTstream will take care of invalid tokens (space, tab, new line and null)
81  tokenStream_.appendToken(nextTok);
82  }
83 
84 
85  if( level )
86  {
87  ioErrorInFile(is.name(), is.lineNumber()) <<
88  "imbalance number of ( or { or [ \n";
89  fatalExit;
90  }
91 
93  return nextTok.good();
94 }
95 
96 
98 (
99  iOstream& os
100 )const
101 {
102  writeKeyword(os);
103  auto lastPuncBegin = false;
104  auto lastPuncEnd = false;
105  auto applySpace = false;
106 
107  const tokenList& tokens = tokenStream_.tokens();
108 
109 
110  for(auto tok = tokens.cbegin(); tok != tokens.cend(); tok++)
111  {
112  if(*tok == endStatementToken()) continue;
113 
114 
115  if(isBeginToken(*tok))
116  {
117  if(lastPuncEnd)
118  {
119  os<<spaceToken();
120  os << *tok;
121  }
122  else
123  {
124  os << *tok;
125  }
126  lastPuncBegin = true;
127  lastPuncEnd = false;
128 
129  }
130  else if(isEndToken(*tok))
131  {
132  if(lastPuncBegin)
133  {
134  os<<spaceToken();
135  os << *tok;
136  }
137  else
138  os << *tok;
139  lastPuncEnd = true;
140  lastPuncBegin = false;
141  }
142  else
143  {
144  if(!lastPuncBegin&&applySpace) os<<spaceToken();
145  os << *tok;
146  lastPuncEnd = false;
147  lastPuncBegin = false;
148  applySpace = true;
149  }
150 
151 
152  }
153 
154  os.endEntry();
155 
156  return true;
157 }
158 
160 :
161  iEntry("NULL_DATAENTRY"),
162  parDict_(dictionary::nullDict),
163  tokenStream_(parDict_.globalName())
164 {}
165 
166 
168 (
169  const word& keyword,
170  const dictionary& parDict
171 )
172 :
173  iEntry(keyword),
174  parDict_(parDict),
175  tokenStream_
176  (
177  groupNames(parDict.globalName(), keyword)
178  )
179 {
180 
181 }
182 
183 
185 (
186  const word& keyWord,
187  const dictionary& parDict,
188  const iTstream& is
189 )
190 :
191  iEntry(keyWord),
192  parDict_(parDict),
193  tokenStream_
194  (
195  groupNames(parDict.globalName(), keyWord),
196  is.tokens()
197  )
198 {
199  tokenStream_.rewind();
200 }
201 
203 (
204  const word& keyWord,
205  const dictionary& parDict,
206  iIstream& is
207 )
208 :
209  iEntry(keyWord),
210  parDict_(parDict),
211  tokenStream_
212  (
213  groupNames(parDict.globalName(),keyWord)
214  )
215 {
216  // reads the entry
217  if( !readDataEntry(is) )
218  {
219  ioErrorInFile(is.name(), is.lineNumber() ) <<
220  "error in reading data entry from file \n";
221  fatalExit;
222  }
223  tokenStream_.rewind();
224 }
225 
227 (
228  const word& keyword,
229  const dictionary& parDict,
230  const token& tok
231 )
232 :
233  iEntry(keyword),
234  parDict_(parDict),
235  tokenStream_
236  (
237  groupNames(parDict.globalName(),keyword)
238  )
239 {
240 
241  tokenStream_.appendToken(tok);
242  tokenStream_.rewind();
243 }
244 
245 
247 (
248  const word& keyword,
249  const dictionary& parDict,
250  const dataEntry& entry
251 )
252 :
253  dataEntry(keyword, parDict)
254 {
255  tokenStream_ = entry.tokenStream_.tokens();
256  tokenStream_.rewind();
257 }
258 
259 
261 (
262 )
263 {
264  tokenStream_.rewind();
265  return tokenStream_;
266 }
267 
268 
270 {
272  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
273  fatalExit;
274  return nullptr;
275 }
276 
277 
279 {
281  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
282  fatalExit;
283  return nullptr;
284 }
285 
287 {
288  return false;
289 }
290 
292 {
293  return tokenStream_.name();
294 }
295 
296 
298 {
299  return parDict_;
300 }
301 
302 
304 {
306  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
307  fatalExit;
308  return const_cast<dictionary&>(parDict_);
309 }
310 
311 
313 {
315  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
316  fatalExit;
317  return parDict_;
318 }
319 
320 
322 {
323  return makeUnique<dataEntry>(*this);
324 }
325 
326 
328 {
329  auto ptr = makeUnique<dataEntry>(*this);
330  return ptr.release();
331 }
332 
334 {
335  return makeUnique<dataEntry>(this->keyword(), parDict, *this);
336 }
337 
339 {
340  auto ptr = makeUnique<dataEntry>(this->keyword(), parDict, *this);
341  return ptr.release();
342 }
343 
346 {
347  token tok;
348  if(!readKeyword(is, keyword_, tok))
349  {
350  ioErrorInFile(is.name(), is.lineNumber()) <<
351  "expected a valid keyword for dataEntry but found " << tok <<endl;
352  fatalExit;
353  }
354 
355  if(!readDataEntry(is) )
356  {
358  "unsucceful in reading dataEntry from " << is.name() <<endl;
359  fatalExit;
360  }
361  return true;
362 }
363 
364 
365 
367 {
368  if( !writeDataEntry(os) )
369  {
370  ioErrorInFile( os.name(), os.lineNumber() );
371  fatalExit;
372  }
373 
374  return true;
375 }
pFlow::iTstream::tokens
const tokenList & tokens() const
Definition: iTstream.cpp:323
pFlow::dataEntry::readDataEntry
bool readDataEntry(iIstream &is)
Definition: dataEntry.cpp:35
pFlow::List< token >
iTstream.hpp
pFlow::iIstream::read
virtual iIstream & read(token &)=0
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
pFlow::dataEntry::stream
virtual iTstream & stream()
Definition: dataEntry.cpp:261
pFlow::token::pToken
punctuationToken pToken() const
Definition: tokenI.hpp:452
iIstream.hpp
pFlow::endStatementToken
token endStatementToken()
Definition: token.hpp:504
pFlow::dataEntry::parrentDict
virtual const dictionary & parrentDict() const
Definition: dataEntry.cpp:297
pFlow::dataEntry::globalName
virtual word globalName() const
Definition: dataEntry.cpp:291
pFlow::token::isPunctuation
bool isPunctuation() const
Definition: tokenI.hpp:426
pFlow::dataEntry::writeDataEntry
bool writeDataEntry(iOstream &os) const
Definition: dataEntry.cpp:98
pFlow::token::good
bool good() const
Definition: tokenI.hpp:372
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
oTstream.hpp
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::iTstream::rewind
virtual void rewind()
Definition: iTstream.cpp:307
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
isEndToken
bool isEndToken(const token &tok)
Definition: helperTstream.hpp:26
pFlow::dataEntry::dataEntry
dataEntry()
Definition: dataEntry.cpp:159
pFlow::dataEntry
Definition: dataEntry.hpp:40
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOstream::bad
bool bad() const
Definition: IOstream.hpp:168
pFlow::dataEntry::tokenStream_
iTstream tokenStream_
Definition: dataEntry.hpp:53
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
dictionary.hpp
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Definition: IOstream.cpp:48
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Definition: iOstream.cpp:97
pFlow::dataEntry::dictPtr
virtual dictionary * dictPtr()
Definition: dataEntry.cpp:269
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:519
pFlow::IOstream::name
virtual const word & name() const
Definition: IOstream.cpp:31
pFlow::dataEntry::nullDataEntry
static dataEntry nullDataEntry
Definition: dataEntry.hpp:66
pFlow::dataEntry::isDictionary
virtual bool isDictionary() const
Definition: dataEntry.cpp:286
pFlow::iTstream
Definition: iTstream.hpp:21
pFlow::dataEntry::read
virtual bool read(iIstream &is)
Definition: dataEntry.cpp:345
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
dataEntry.hpp
pFlow::dataEntry::clone
virtual uniquePtr< iEntry > clone() const
Definition: dataEntry.cpp:321
pFlow::groupNames
word groupNames(const word &bw, const word &tw, char sep='.')
Definition: bTypesFunctions.cpp:151
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::iEntry
Definition: iEntry.hpp:38
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
iOstream.hpp
pFlow::dataEntry::clonePtr
virtual iEntry * clonePtr() const
Definition: dataEntry.cpp:327
pFlow::dataEntry::dict
virtual dictionary & dict()
Definition: dataEntry.cpp:303
pFlow::dataEntry::write
virtual bool write(iOstream &os) const
Definition: dataEntry.cpp:366
pFlow::iOstream
Definition: iOstream.hpp:53
isBeginToken
bool isBeginToken(const token &tok)
Definition: helperTstream.hpp:17
pFlow::dictionary
Definition: dictionary.hpp:38
error.hpp