OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

entryIO.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "primitiveEntry.H"
00028 #include "dictionaryEntry.H"
00029 #include "functionEntry.H"
00030 #include "includeEntry.H"
00031 #include "inputModeEntry.H"
00032 
00033 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00034 
00035 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
00036 {
00037     token keywordToken;
00038 
00039     // Read the next valid token discarding spurious ';'s
00040     do
00041     {
00042         if
00043         (
00044             is.read(keywordToken).bad()
00045          || is.eof()
00046          || !keywordToken.good()
00047         )
00048         {
00049             return false;
00050         }
00051     }
00052     while (keywordToken == token::END_STATEMENT);
00053 
00054     // If the token is a valid keyword set 'keyword' return true...
00055     if (keywordToken.isWord())
00056     {
00057         keyword = keywordToken.wordToken();
00058         return true;
00059     }
00060     else if (keywordToken.isString())
00061     {
00062         // Enable wildcards
00063         keyword = keywordToken.stringToken();
00064         return true;
00065     }
00066     // If it is the end of the dictionary or file return false...
00067     else if (keywordToken == token::END_BLOCK || is.eof())
00068     {
00069         return false;
00070     }
00071     // Otherwise the token is invalid
00072     else
00073     {
00074         cerr<< "--> FOAM Warning : " << std::endl
00075             << "    From function "
00076             << "entry::getKeyword(keyType&, Istream&)" << std::endl
00077             << "    in file " << __FILE__
00078             << " at line " << __LINE__ << std::endl
00079             << "    Reading " << is.name().c_str() << std::endl
00080             << "    found " << keywordToken << std::endl
00081             << "    expected either " << token::END_BLOCK << " or EOF"
00082             << std::endl;
00083 
00084         return false;
00085     }
00086 }
00087 
00088 
00089 bool Foam::entry::New(dictionary& parentDict, Istream& is)
00090 {
00091     is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
00092 
00093     keyType keyword;
00094 
00095     // Get the next keyword and if invalid return false
00096     if (!getKeyword(keyword, is))
00097     {
00098         return false;
00099     }
00100     else  // Keyword starts entry ...
00101     {
00102         if (keyword[0] == '#')         // ... Function entry
00103         {
00104             word functionName = keyword(1, keyword.size()-1);
00105             return functionEntry::execute(functionName, parentDict, is);
00106         }
00107         else if (keyword[0] == '$')    // ... Substitution entry
00108         {
00109             parentDict.substituteKeyword(keyword);
00110             return true;
00111         }
00112         else if (keyword == "include") // ... For backward compatibility
00113         {
00114             return functionEntries::includeEntry::execute(parentDict, is);
00115         }
00116         else                           // ... Data entries
00117         {
00118             token nextToken(is);
00119             is.putBack(nextToken);
00120 
00121             // Deal with duplicate entries
00122             bool mergeEntry = false;
00123 
00124             // See (using exact match) if entry already present
00125             entry* existingPtr = parentDict.lookupEntryPtr
00126             (
00127                 keyword,
00128                 false,
00129                 false
00130             );
00131 
00132             if (existingPtr)
00133             {
00134                 if (functionEntries::inputModeEntry::merge())
00135                 {
00136                     mergeEntry = true;
00137                 }
00138                 else if (functionEntries::inputModeEntry::overwrite())
00139                 {
00140                     // clear dictionary so merge acts like overwrite
00141                     if (existingPtr->isDict())
00142                     {
00143                         existingPtr->dict().clear();
00144                     }
00145                     mergeEntry = true;
00146                 }
00147                 else if (functionEntries::inputModeEntry::protect())
00148                 {
00149                     // read and discard the entry
00150                     if (nextToken == token::BEGIN_BLOCK)
00151                     {
00152                         dictionaryEntry dummy(keyword, parentDict, is);
00153                     }
00154                     else
00155                     {
00156                         primitiveEntry  dummy(keyword, parentDict, is);
00157                     }
00158                     return true;
00159                 }
00160                 else if (functionEntries::inputModeEntry::error())
00161                 {
00162                     FatalIOErrorIn
00163                     (
00164                         "entry::New(const dictionary& parentDict, Istream&)",
00165                         is
00166                     )
00167                         << "ERROR! duplicate entry: " << keyword
00168                         << exit(FatalIOError);
00169 
00170                     return false;
00171                 }
00172             }
00173 
00174             if (nextToken == token::BEGIN_BLOCK)
00175             {
00176                 return parentDict.add
00177                 (
00178                     new dictionaryEntry(keyword, parentDict, is),
00179                     mergeEntry
00180                 );
00181             }
00182             else
00183             {
00184                 return parentDict.add
00185                 (
00186                     new primitiveEntry(keyword, parentDict, is),
00187                     mergeEntry
00188                 );
00189             }
00190         }
00191     }
00192 }
00193 
00194 
00195 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
00196 {
00197     is.fatalCheck("entry::New(Istream&)");
00198 
00199     keyType keyword;
00200 
00201     // Get the next keyword and if invalid return false
00202     if (!getKeyword(keyword, is))
00203     {
00204         return autoPtr<entry>(NULL);
00205     }
00206     else // Keyword starts entry ...
00207     {
00208         token nextToken(is);
00209         is.putBack(nextToken);
00210 
00211         if (nextToken == token::BEGIN_BLOCK)
00212         {
00213             return autoPtr<entry>
00214             (
00215                 new dictionaryEntry(keyword, dictionary::null, is)
00216             );
00217         }
00218         else
00219         {
00220             return autoPtr<entry>
00221             (
00222                 new primitiveEntry(keyword, is)
00223             );
00224         }
00225     }
00226 }
00227 
00228 
00229 // * * * * * * * * * * * * * Ostream operator  * * * * * * * * * * * * * * * //
00230 
00231 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
00232 {
00233     e.write(os);
00234     return os;
00235 }
00236 
00237 
00238 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd