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
Definition: IOstream.hpp:156
pFlow::iIstream::read
virtual iIstream & read(token &)=0
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::token
Definition: token.hpp:42
iIstream.hpp
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
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::iEntry::write
virtual bool write(iOstream &os) const =0
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::iEntry::read
virtual bool read(iIstream &is)=0
pFlow::IOstream::bad
bool bad() const
Definition: IOstream.hpp:168
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::iEntry::createEntry
static bool createEntry(dictionary &parDict, iIstream &is, bool hasBlockToken=false)
Definition: iEntry.cpp:64
dictionary.hpp
pFlow::iIstream::putBack
void putBack(const token &tok)
Definition: iIstream.cpp:5
pFlow::operator>>
INLINE_FUNCTION iIstream & operator>>(iIstream &str, AB3History &ab3)
Definition: AdamsBashforth3.hpp:41
pFlow::IOstream::fatalCheck
bool fatalCheck(const char *operation) const
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
Definition: IOstream.cpp:31
iEntry.hpp
pFlow::iEntry::writeKeyword
bool writeKeyword(iOstream &os) const
Definition: iEntry.cpp:154
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
dataEntry.hpp
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry)
Definition: dictionary.cpp:379
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::iEntry::readKeyword
static bool readKeyword(iIstream &is, word &keyword, token &tok)
Definition: iEntry.cpp:32
pFlow::iEntry
Definition: iEntry.hpp:38
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
iOstream.hpp
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::token::wordToken
const word & wordToken() const
Definition: tokenI.hpp:600
pFlow::iOstream::writeWordKeyword
virtual iOstream & writeWordKeyword(const word &kw)
Definition: iOstream.cpp:41
pFlow::dictionary
Definition: dictionary.hpp:38
pFlow::token::isWord
bool isWord() const
Definition: tokenI.hpp:584
error.hpp