entryIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "entry.H"
27 #include "primitiveEntry.H"
28 #include "dictionaryEntry.H"
29 #include "functionEntry.H"
30 #include "includeEntry.H"
31 #include "inputModeEntry.H"
32 #include "stringOps.H"
33 #include "dictionaryListEntry.H"
34 
35 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
36 
37 bool Foam::entry::getKeyword(keyType& keyword, token& keywordToken, Istream& is)
38 {
39  // Read the next valid token discarding spurious ';'s
40  do
41  {
42  if
43  (
44  is.read(keywordToken).bad()
45  || is.eof()
46  || !keywordToken.good()
47  )
48  {
49  return false;
50  }
51  }
52  while (keywordToken == token::END_STATEMENT);
53 
54  keyword = keywordToken;
55 
56  return !keyword.isUndefined();
57 }
58 
59 
60 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
61 {
62  token keywordToken;
63  bool ok = getKeyword(keyword, keywordToken, is);
64 
65  if (ok)
66  {
67  return true;
68  }
69  else
70  {
71  // Do some more checking
72  if (keywordToken == token::END_BLOCK || is.eof())
73  {
74  return false;
75  }
76  else
77  {
78  // Otherwise the token is invalid
79  cerr<< "--> FOAM Warning : " << std::endl
80  << " From function "
81  << "entry::getKeyword(keyType&, Istream&)" << std::endl
82  << " in file " << __FILE__
83  << " at line " << __LINE__ << std::endl
84  << " Reading " << is.name().c_str() << std::endl
85  << " found " << keywordToken << std::endl
86  << " expected either " << token::END_BLOCK << " or EOF"
87  << std::endl;
88  return false;
89  }
90  }
91 }
92 
93 
94 bool Foam::entry::New(dictionary& parentDict, Istream& is)
95 {
96  is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
97 
99  token keyToken;
100 
101  // Get the next keyword and if a valid keyword return true
102  bool valid = getKeyword(keyword, keyToken, is);
103 
104  if (!valid)
105  {
106  // Do some more checking
107  if (keyToken == token::END_BLOCK || is.eof())
108  {
109  return false;
110  }
111  else if
112  (
113  keyToken.isLabel()
114  || (keyToken.isPunctuation() && keyToken.pToken() == token::BEGIN_LIST)
115  )
116  {
117  is.putBack(keyToken);
118  return parentDict.add
119  (
120  new dictionaryListEntry(parentDict, is),
121  false
122  );
123  }
124  else
125  {
126  // Otherwise the token is invalid
127  cerr<< "--> FOAM Warning : " << std::endl
128  << " From function "
129  << "entry::New(dictionary&, Istream&)" << std::endl
130  << " in file " << __FILE__
131  << " at line " << __LINE__ << std::endl
132  << " Reading " << is.name().c_str() << std::endl
133  << " found " << keyToken << std::endl
134  << " expected either " << token::END_BLOCK << " or EOF"
135  << std::endl;
136  return false;
137  }
138  }
139  else // Keyword starts entry ...
140  {
141  if (keyword.isFunctionName()) // ... Function entry
142  {
144  {
145  return parentDict.add
146  (
147  new functionEntry
148  (
149  keyword,
150  parentDict,
151  is
152  ),
153  false
154  );
155  }
156  else
157  {
158  const word functionName = keyword(1, keyword.size() - 1);
159  return functionEntry::execute(functionName, parentDict, is);
160  }
161  }
162  else if
163  (
165  && keyword.isVariable()
166  ) // ... Substitution entry
167  {
168  token nextToken(is);
169  is.putBack(nextToken);
170 
171  if (keyword.size() > 2 && keyword[1] == token::BEGIN_BLOCK)
172  {
173  // Recursive substitution mode. Replace between {} with
174  // expansion and then let standard variable expansion deal
175  // with rest.
176  string s(keyword(2, keyword.size() - 3));
177 
178  // Substitute dictionary and environment variables. Do not allow
179  // empty substitutions.
180  stringOps::inplaceExpand(s, parentDict, true, false);
181  keyword.std::string::replace(1, keyword.size() - 1, s);
182  }
183 
184  if (nextToken == token::BEGIN_BLOCK)
185  {
186  word varName = keyword(1, keyword.size() - 1);
187 
188  // Lookup the variable name in the given dictionary
189  const entry* ePtr = parentDict.lookupScopedEntryPtr
190  (
191  varName,
192  true,
193  true
194  );
195 
196  if (ePtr)
197  {
198  // Read as primitiveEntry
199  const keyType newKeyword(ePtr->stream());
200 
201  return parentDict.add
202  (
203  new dictionaryEntry(newKeyword, parentDict, is),
204  false
205  );
206  }
207  else
208  {
210  << "Attempt to use undefined variable " << varName
211  << " as keyword"
212  << exit(FatalIOError);
213  return false;
214  }
215  }
216  else
217  {
218  parentDict.substituteScopedKeyword(keyword);
219  }
220 
221  return true;
222  }
223  else // ... Data entries
224  {
225  token nextToken(is);
226  is.putBack(nextToken);
227 
228  // Deal with duplicate entries
229  bool mergeEntry = false;
230 
231  // If function entries are disabled allow duplicate entries
233  {
234  mergeEntry = false;
235  }
236  else
237  {
238  // See (using exact match) if entry already present
239  entry* existingPtr = parentDict.lookupEntryPtr
240  (
241  keyword,
242  false,
243  false
244  );
245 
246  if (existingPtr)
247  {
249  {
250  mergeEntry = true;
251  }
253  {
254  // Clear dictionary so merge acts like overwrite
255  if (existingPtr->isDict())
256  {
257  existingPtr->dict().clear();
258  }
259  mergeEntry = true;
260  }
262  {
263  // Read and discard the entry
264  if (nextToken == token::BEGIN_BLOCK)
265  {
266  dictionaryEntry dummy(keyword, parentDict, is);
267  }
268  else
269  {
270  primitiveEntry dummy(keyword, parentDict, is);
271  }
272  return true;
273  }
275  {
277  << "ERROR! duplicate entry: " << keyword
278  << exit(FatalIOError);
279  return false;
280  }
281  }
282  }
283 
284  if (nextToken == token::BEGIN_BLOCK)
285  {
286  return parentDict.add
287  (
288  new dictionaryEntry(keyword, parentDict, is),
289  mergeEntry
290  );
291  }
292  else
293  {
294  return parentDict.add
295  (
296  new primitiveEntry(keyword, parentDict, is),
297  mergeEntry
298  );
299  }
300  }
301  }
302 }
303 
304 
306 {
307  is.fatalCheck("entry::New(Istream&)");
308 
310 
311  // Get the next keyword and if invalid return false
312  if (!getKeyword(keyword, is))
313  {
314  return autoPtr<entry>(nullptr);
315  }
316  else // Keyword starts entry ...
317  {
318  token nextToken(is);
319  is.putBack(nextToken);
320 
321  if (nextToken == token::BEGIN_BLOCK)
322  {
323  return autoPtr<entry>
324  (
325  new dictionaryEntry(keyword, dictionary::null, is)
326  );
327  }
328  else
329  {
330  return autoPtr<entry>
331  (
332  new primitiveEntry(keyword, is)
333  );
334  }
335  }
336 }
337 
338 
339 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
340 
342 {
343  e.write(os);
344  return os;
345 }
346 
347 
348 // ************************************************************************* //
bool isLabel() const
Definition: tokenI.H:392
A class for handling keywords in dictionaries.
Definition: keyType.H:66
punctuationToken pToken() const
Definition: tokenI.H:248
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:706
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual void write(Ostream &) const =0
Write.
static const dictionary null
Null dictionary.
Definition: dictionary.H:241
A token holds items read from Istream.
Definition: token.H:72
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:94
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool isFunctionName() const
Return true if the keyword is a functionName.
Definition: keyTypeI.H:85
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
bool isVariable() const
Return true if the keyword is a variable.
Definition: keyTypeI.H:91
A keyword and a list of tokens is a &#39;dictionaryEntry&#39;.
const entry * lookupScopedEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:823
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1056
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
static bool protect()
Return true if the inputMode is protect.
gmvFile<< "tracers "<< particles.size()<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().x()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().y()<< " ";}gmvFile<< nl;forAllConstIter(Cloud< passiveParticle >, particles, iter){ gmvFile<< iter().position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
static int disableFunctionEntries
Definition: entry.H:86
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:63
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
Read/write List of dictionaries.
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:156
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static bool merge()
Return true if the inputMode is merge.
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:81
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Ostream & operator<<(Ostream &, const ensightPart &)
bool substituteScopedKeyword(const word &keyword)
Substitute the given scoped keyword prepended by &#39;$&#39; with the.
Definition: dictionary.C:864
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
static bool overwrite()
Return true if the inputMode is overwrite.
void clear()
Clear the dictionary.
Definition: dictionary.C:1388
static bool error()
Return true if the inputMode is error.
bool isPunctuation() const
Definition: tokenI.H:243
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
IOerror FatalIOError