1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
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
13 the Free Software Foundation, either version 3 of the License, or
14 (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "primitiveEntry.H"
27 #include "dictionaryEntry.H"
28 #include "functionEntry.H"
29 #include "includeEntry.H"
30 #include "inputModeEntry.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
38 // Read the next valid token discarding spurious ';'s
43 is.read(keywordToken).bad()
45 || !keywordToken.good()
51 while (keywordToken == token::END_STATEMENT);
53 // If the token is a valid keyword set 'keyword' return true...
54 if (keywordToken.isWord())
56 keyword = keywordToken.wordToken();
59 else if (keywordToken.isString())
62 keyword = keywordToken.stringToken();
65 // If it is the end of the dictionary or file return false...
66 else if (keywordToken == token::END_BLOCK || is.eof())
70 // Otherwise the token is invalid
73 cerr<< "--> FOAM Warning : " << std::endl
75 << "entry::getKeyword(keyType&, Istream&)" << std::endl
76 << " in file " << __FILE__
77 << " at line " << __LINE__ << std::endl
78 << " Reading " << is.name().c_str() << std::endl
79 << " found " << keywordToken << std::endl
80 << " expected either " << token::END_BLOCK << " or EOF"
88 bool Foam::entry::New(dictionary& parentDict, Istream& is)
90 is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
94 // Get the next keyword and if invalid return false
95 if (!getKeyword(keyword, is))
99 else // Keyword starts entry ...
103 !disableFunctionEntries
105 ) // ... Function entry
107 word functionName = keyword(1, keyword.size()-1);
108 return functionEntry::execute(functionName, parentDict, is);
112 !disableFunctionEntries
113 && keyword[0] == '$') // ... Substitution entry
115 parentDict.substituteKeyword(keyword);
120 !disableFunctionEntries
121 && keyword == "include"
122 ) // ... For backward compatibility
124 return functionEntries::includeEntry::execute(parentDict, is);
126 else // ... Data entries
129 is.putBack(nextToken);
131 // Deal with duplicate entries
132 bool mergeEntry = false;
134 // See (using exact match) if entry already present
135 entry* existingPtr = parentDict.lookupEntryPtr
144 if (functionEntries::inputModeEntry::merge())
148 else if (functionEntries::inputModeEntry::overwrite())
150 // clear dictionary so merge acts like overwrite
151 if (existingPtr->isDict())
153 existingPtr->dict().clear();
157 else if (functionEntries::inputModeEntry::protect())
159 // read and discard the entry
160 if (nextToken == token::BEGIN_BLOCK)
162 dictionaryEntry dummy(keyword, parentDict, is);
166 primitiveEntry dummy(keyword, parentDict, is);
170 else if (functionEntries::inputModeEntry::error())
174 "entry::New(const dictionary& parentDict, Istream&)",
177 << "ERROR! duplicate entry: " << keyword
178 << exit(FatalIOError);
184 if (nextToken == token::BEGIN_BLOCK)
186 return parentDict.add
188 new dictionaryEntry(keyword, parentDict, is),
194 return parentDict.add
196 new primitiveEntry(keyword, parentDict, is),
205 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
207 is.fatalCheck("entry::New(Istream&)");
211 // Get the next keyword and if invalid return false
212 if (!getKeyword(keyword, is))
214 return autoPtr<entry>(NULL);
216 else // Keyword starts entry ...
219 is.putBack(nextToken);
221 if (nextToken == token::BEGIN_BLOCK)
223 return autoPtr<entry>
225 new dictionaryEntry(keyword, dictionary::null, is)
230 return autoPtr<entry>
232 new primitiveEntry(keyword, is)
239 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
241 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
248 // ************************************************************************* //