www.cemf.ir
iEntry.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 #include "iEntry.hpp"
24 #include "dictionary.hpp"
25 #include "dataEntry.hpp"
26 #include "error.hpp"
27 #include "iOstream.hpp"
28 #include "iIstream.hpp"
29 
30 
32 (
33  iIstream &is,
34  word& keyword,
35  token& tok
36 )
37 {
38 
39  while
40  (
41  !is.read(tok).bad() &&
42  !is.eof() &&
43  tok.good()
44  )
45  {
46  if(tok == token::END_STATEMENT) continue;
47 
48  if( tok.isWord())
49  {
50  keyword = tok.wordToken();
51  return true;
52  }
53  else
54  {
55  return false;
56  }
57  }
58 
59  return false;
60 }
61 
62 
64 (
65  dictionary& parDict,
66  iIstream& is,
67  bool hasBlockToken
68 )
69 {
71 
72  // reads tokens one-by-one
73  word key;
74  token nextTok;
75 
76  if( auto isKey = iEntry::readKeyword(is, key, nextTok ); isKey )
77  {
78  //output<< key << " "<<nextTok <<endl;
79  is.read(nextTok);
80  if( is.eof() || is.bad() || !nextTok.good() )
81  {
82  ioErrorInFile(is.name(), is.lineNumber())<<
83  "expecting a valid token after keyword " << key << endl;
84  fatalExit;
85  }
86  // output<< nextTok<<endl;
87  is.putBack(nextTok);
88 
89  if( nextTok == token::BEGIN_BLOCK )
90  {
91 
92  uniquePtr<iEntry> ptr = makeUnique<dictionary>(key, parDict, is);
93 
94  if( !parDict.addPtr(key, ptr ))
95  {
97  "unable to add dicrionary " << key <<
98  " to dictionary " << parDict.globalName() <<endl;
99  fatalExit;
100  }
101  }
102  else
103  {
104  uniquePtr<iEntry> ptr = makeUnique<dataEntry>(key, parDict, is);
105 
106  if( !parDict.addPtr(key, ptr ) )
107  {
109  "unable to add dataEntry " << key <<
110  " to dictionary " << parDict.globalName() <<endl;
111  fatalExit;
112  }
113  }
114  }
115  else
116  {
117 
118  if( nextTok.good() )
119  {
120  if(hasBlockToken)
121  {
122  if(nextTok != token::END_BLOCK )
123  {
124  ioErrorInFile(is.name(), is.lineNumber())<<
125  "expecting a } but found "<< nextTok <<endl;
126  fatalExit;
127  }else
128  {
129  return false;
130  }
131 
132  }else
133  {
134  ioErrorInFile(is.name(), is.lineNumber())<<
135  "not expecting " << nextTok <<endl;
136  return false;
137  }
138 
139  }
140  else if( !is.eof())
141  {
142  ioErrorInFile(is.name(), is.lineNumber())<<
143  "error in reading next token. \n";
144  fatalExit;
145  }
146 
147  }
148 
149  return true;
150 }
151 
152 
154 (
155  iOstream& os
156 )const
157 {
158  os.writeWordKeyword(keyword_);
159  os.fatalCheck("pFlow::iEntry::writeKeyword");
160  return true;
161 }
162 
163 
165 {
166  if(!e.write(os))
167  {
168  ioErrorInFile(os.name(), os.lineNumber())<<
169  "Error in wrting to file \n";
170  fatalExit;
171  }
172  return os;
173 }
174 
176 {
177  if( !e.read(is))
178  {
179  ioErrorInFile(is.name(), is.lineNumber())<<
180  "Error in reading from file \n";
181  fatalExit;
182  }
183 
184  return is;
185 }
pFlow::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.hpp:192
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
iIstream.hpp
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
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
pFlow::iEntry::write
virtual bool write(iOstream &os) const =0
write to stream
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::iEntry::read
virtual bool read(iIstream &is)=0
read from stream
pFlow::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.hpp:204
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::iEntry::createEntry
static bool createEntry(dictionary &parDict, iIstream &is, bool hasBlockToken=false)
create an entry (dataEntry or dictionary) from stream
Definition: iEntry.cpp:64
dictionary.hpp
pFlow::iIstream::putBack
void putBack(const token &tok)
Put back token Only a single put back is permitted.
Definition: iIstream.cpp:23
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.cpp:48
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
iEntry.hpp
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry, bool warning=true)
add a pointer entry (dictionary/dataEntry) replaces this entry with existing one and issue a warning
Definition: dictionary.cpp:386
pFlow::iEntry::writeKeyword
bool writeKeyword(iOstream &os) const
Definition: iEntry.cpp:154
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
dataEntry.hpp
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Report an error in file operation with supplied fileName and lineNumber.
Definition: error.hpp:87
pFlow::iEntry::readKeyword
static bool readKeyword(iIstream &is, word &keyword, token &tok)
read a keyword from stream
Definition: iEntry.cpp:32
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::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::token::wordToken
const word & wordToken() const
Return const reference to the word contents.
Definition: tokenI.hpp:618
pFlow::iOstream::writeWordKeyword
virtual iOstream & writeWordKeyword(const word &kw)
Write the keyword followed by an appropriate indentation.
Definition: iOstream.cpp:48
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
pFlow::token::isWord
bool isWord() const
Token is word or DIRECTIVE word.
Definition: tokenI.hpp:602
error.hpp