www.cemf.ir
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 
125  if(tok != tokens.cbegin())os<<spaceToken();
126 
127  os << *tok;
128  }
129  lastPuncBegin = true;
130  lastPuncEnd = false;
131 
132  }
133  else if(isEndToken(*tok))
134  {
135  if(lastPuncBegin)
136  {
137  os<<spaceToken();
138  os << *tok;
139  }
140  else
141  os << *tok;
142  lastPuncEnd = true;
143  lastPuncBegin = false;
144  }
145  else
146  {
147 
148  if(!lastPuncBegin&&applySpace) os<<spaceToken();
149  os << *tok;
150  lastPuncEnd = false;
151  lastPuncBegin = false;
152  applySpace = true;
153  }
154 
155 
156  }
157 
158  os.endEntry();
159 
160  return true;
161 }
162 
164 :
165  iEntry("NULL_DATAENTRY"),
166  parDict_(dictionary::nullDict),
167  tokenStream_(parDict_.globalName())
168 {}
169 
170 
172 (
173  const word& keyword,
174  const dictionary& parDict
175 )
176 :
177  iEntry(keyword),
178  parDict_(parDict),
179  tokenStream_
180  (
181  groupNames(parDict.globalName(), keyword)
182  )
183 {
184 
185 }
186 
187 
189 (
190  const word& keyWord,
191  const dictionary& parDict,
192  const iTstream& is
193 )
194 :
195  iEntry(keyWord),
196  parDict_(parDict),
197  tokenStream_
198  (
199  groupNames(parDict.globalName(), keyWord),
200  is.tokens()
201  )
202 {
203  tokenStream_.rewind();
204 }
205 
207 (
208  const word& keyWord,
209  const dictionary& parDict,
210  iIstream& is
211 )
212 :
213  iEntry(keyWord),
214  parDict_(parDict),
215  tokenStream_
216  (
217  groupNames(parDict.globalName(),keyWord)
218  )
219 {
220  // reads the entry
221  if( !readDataEntry(is) )
222  {
223  ioErrorInFile(is.name(), is.lineNumber() ) <<
224  "error in reading data entry from file \n";
225  fatalExit;
226  }
227  tokenStream_.rewind();
228 }
229 
231 (
232  const word& keyword,
233  const dictionary& parDict,
234  const token& tok
235 )
236 :
237  iEntry(keyword),
238  parDict_(parDict),
239  tokenStream_
240  (
241  groupNames(parDict.globalName(),keyword)
242  )
243 {
244 
245  tokenStream_.appendToken(tok);
246  tokenStream_.rewind();
247 }
248 
249 
251 (
252  const word& keyword,
253  const dictionary& parDict,
254  const dataEntry& entry
255 )
256 :
257  dataEntry(keyword, parDict)
258 {
259  tokenStream_ = entry.tokenStream_.tokens();
260  tokenStream_.rewind();
261 }
262 
263 
265 (
266 )
267 {
268  tokenStream_.rewind();
269  return tokenStream_;
270 }
271 
272 
274 {
276  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
277  fatalExit;
278  return nullptr;
279 }
280 
281 
283 {
285  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
286  fatalExit;
287  return nullptr;
288 }
289 
291 {
292  return false;
293 }
294 
296 {
297  return tokenStream_.name();
298 }
299 
300 
302 {
303  return parDict_;
304 }
305 
306 
308 {
310  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
311  fatalExit;
312  return const_cast<dictionary&>(parDict_);
313 }
314 
315 
317 {
319  "Request for an entry that is not a dictionray. Entry name is " << globalName() << endl;
320  fatalExit;
321  return parDict_;
322 }
323 
324 
326 {
327  return makeUnique<dataEntry>(*this);
328 }
329 
330 
332 {
333  auto ptr = makeUnique<dataEntry>(*this);
334  return ptr.release();
335 }
336 
338 {
339  return makeUnique<dataEntry>(this->keyword(), parDict, *this);
340 }
341 
343 {
344  auto ptr = makeUnique<dataEntry>(this->keyword(), parDict, *this);
345  return ptr.release();
346 }
347 
350 {
351  token tok;
352  if(!readKeyword(is, keyword_, tok))
353  {
354  ioErrorInFile(is.name(), is.lineNumber()) <<
355  "expected a valid keyword for dataEntry but found " << tok <<endl;
356  fatalExit;
357  }
358 
359  if(!readDataEntry(is) )
360  {
362  "unsucceful in reading dataEntry from " << is.name() <<endl;
363  fatalExit;
364  }
365  return true;
366 }
367 
368 
369 
371 {
372  if( !writeDataEntry(os) )
373  {
374  ioErrorInFile( os.name(), os.lineNumber() );
375  fatalExit;
376  }
377 
378  return true;
379 }
pFlow::iTstream::tokens
const tokenList & tokens() const
const access to token list
Definition: iTstream.cpp:331
pFlow::dataEntry::readDataEntry
bool readDataEntry(iIstream &is)
read dataEntry from stream
Definition: dataEntry.cpp:35
pFlow::List< token >
iTstream.hpp
pFlow::iIstream::read
virtual iIstream & read(token &)=0
Return next token from stream.
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::dataEntry::stream
virtual iTstream & stream()
access to token stream
Definition: dataEntry.cpp:265
pFlow::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.hpp:470
iIstream.hpp
pFlow::endStatementToken
token endStatementToken()
Definition: token.hpp:513
pFlow::dataEntry::parrentDict
virtual const dictionary & parrentDict() const
const ref to parrent dictionary
Definition: dataEntry.cpp:301
pFlow::dataEntry::globalName
virtual word globalName() const
global name of entry, separated with dots
Definition: dataEntry.cpp:295
pFlow::token::isPunctuation
bool isPunctuation() const
Token is PUNCTUATION.
Definition: tokenI.hpp:444
pFlow::dataEntry::writeDataEntry
bool writeDataEntry(iOstream &os) const
write dataEntry to stream
Definition: dataEntry.cpp:98
pFlow::token::good
bool good() const
True if token is not UNDEFINED or ERROR.
Definition: tokenI.hpp:390
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
FUNCTION_NAME
#define FUNCTION_NAME
Definition: pFlowMacros.hpp:29
oTstream.hpp
pFlow::dictionary::globalName
virtual word globalName() const
global name of entry, separated with dots
Definition: dictionary.cpp:356
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
isEndToken
bool isEndToken(const token &tok)
Definition: helperTstream.hpp:44
pFlow::dataEntry::dataEntry
dataEntry()
construct null dataEntry
Definition: dataEntry.cpp:163
pFlow::dataEntry
Data entry to be used in dictionries.
Definition: dataEntry.hpp:48
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::dataEntry::tokenStream_
iTstream tokenStream_
list the tokens as input token stream
Definition: dataEntry.hpp:61
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::iTstream::rewind
virtual void rewind() override
Rewind the stream so that it may be read again.
Definition: iTstream.cpp:305
dictionary.hpp
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Write end entry (';') followed by newline.
Definition: iOstream.cpp:104
pFlow::dataEntry::dictPtr
virtual dictionary * dictPtr()
not permited to be called
Definition: dataEntry.cpp:273
pFlow::spaceToken
token spaceToken()
Definition: token.hpp:528
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::dataEntry::nullDataEntry
static dataEntry nullDataEntry
Definition: dataEntry.hpp:74
pFlow::dataEntry::isDictionary
virtual bool isDictionary() const
should returen false;
Definition: dataEntry.cpp:290
pFlow::iTstream
Input token stream.
Definition: iTstream.hpp:27
pFlow::dataEntry::read
virtual bool read(iIstream &is)
read from stream
Definition: dataEntry.cpp:349
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
dataEntry.hpp
pFlow::dataEntry::clone
virtual uniquePtr< iEntry > clone() const
Definition: dataEntry.cpp:325
pFlow::groupNames
word groupNames(const word &bw, const word &tw, char sep='.')
Group words and output bw.tw.
Definition: bTypesFunctions.cpp:179
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::iEntry
Interface calss for data entry and dictionary
Definition: iEntry.hpp:42
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:223
iOstream.hpp
pFlow::dataEntry::clonePtr
virtual iEntry * clonePtr() const
clone the object
Definition: dataEntry.cpp:331
pFlow::dataEntry::dict
virtual dictionary & dict()
not permited to be called
Definition: dataEntry.cpp:307
pFlow::dataEntry::write
virtual bool write(iOstream &os) const
write to stream
Definition: dataEntry.cpp:370
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
isBeginToken
bool isBeginToken(const token &tok)
Definition: helperTstream.hpp:35
pFlow::dictionary
Dictionary holds a set of data entries or sub-dictionaries that are enclosed in a curely braces or ar...
Definition: dictionary.hpp:67
error.hpp