1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "primitiveEntry.H"
28 #include "dictionaryEntry.H"
29 #include "functionEntry.H"
30 #include "includeEntry.H"
31 #include "inputModeEntry.H"
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
39 // Read the next valid token discarding spurious ';'s
44 is.read(keywordToken).bad()
46 || !keywordToken.good()
52 while (keywordToken == token::END_STATEMENT);
54 // If the token is a valid keyword set 'keyword' return true...
55 if (keywordToken.isWord())
57 keyword = keywordToken.wordToken();
60 else if (keywordToken.isString())
63 keyword = keywordToken.stringToken();
66 // If it is the end of the dictionary or file return false...
67 else if (keywordToken == token::END_BLOCK || is.eof())
71 // Otherwise the token is invalid
74 cerr<< "--> FOAM Warning : " << std::endl
76 << "entry::getKeyword(keyType&, Istream&)" << std::endl
77 << " in file " << __FILE__
78 << " at line " << __LINE__ << std::endl
79 << " Reading " << is.name().c_str() << std::endl
80 << " found " << keywordToken << std::endl
81 << " expected either " << token::END_BLOCK << " or EOF"
89 bool Foam::entry::New(dictionary& parentDict, Istream& is)
91 is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
95 // Get the next keyword and if invalid return false
96 if (!getKeyword(keyword, is))
100 else // Keyword starts entry ...
102 if (keyword[0] == '#') // ... Function entry
104 word functionName = keyword(1, keyword.size()-1);
105 return functionEntry::execute(functionName, parentDict, is);
107 else if (keyword[0] == '$') // ... Substitution entry
109 parentDict.substituteKeyword(keyword);
112 else if (keyword == "include") // ... For backward compatibility
114 return functionEntries::includeEntry::execute(parentDict, is);
116 else // ... Data entries
119 is.putBack(nextToken);
121 // Deal with duplicate entries
122 bool mergeEntry = false;
124 // See (using exact match) if entry already present
125 entry* existingPtr = parentDict.lookupEntryPtr
134 if (functionEntries::inputModeEntry::merge())
138 else if (functionEntries::inputModeEntry::overwrite())
140 // clear dictionary so merge acts like overwrite
141 if (existingPtr->isDict())
143 existingPtr->dict().clear();
147 else if (functionEntries::inputModeEntry::protect())
149 // read and discard the entry
150 if (nextToken == token::BEGIN_BLOCK)
152 dictionaryEntry dummy(keyword, parentDict, is);
156 primitiveEntry dummy(keyword, parentDict, is);
160 else if (functionEntries::inputModeEntry::error())
164 "entry::New(const dictionary& parentDict, Istream&)",
167 << "ERROR! duplicate entry: " << keyword
168 << exit(FatalIOError);
174 if (nextToken == token::BEGIN_BLOCK)
176 return parentDict.add
178 new dictionaryEntry(keyword, parentDict, is),
184 return parentDict.add
186 new primitiveEntry(keyword, parentDict, is),
195 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
197 is.fatalCheck("entry::New(Istream&)");
201 // Get the next keyword and if invalid return false
202 if (!getKeyword(keyword, is))
204 return autoPtr<entry>(NULL);
206 else // Keyword starts entry ...
209 is.putBack(nextToken);
211 if (nextToken == token::BEGIN_BLOCK)
213 return autoPtr<entry>
215 new dictionaryEntry(keyword, dictionary::null, is)
220 return autoPtr<entry>
222 new primitiveEntry(keyword, is)
229 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
231 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
238 // ************************************************************************* //