FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include <OpenFOAM/primitiveEntry.H>
00027 #include <OpenFOAM/dictionaryEntry.H>
00028 #include <OpenFOAM/functionEntry.H>
00029 #include <OpenFOAM/includeEntry.H>
00030 #include <OpenFOAM/inputModeEntry.H>
00031 
00032 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00033 
00034 bool Foam::entry::getKeyword(keyType& keyword, Istream& is)
00035 {
00036     token keywordToken;
00037 
00038     // Read the next valid token discarding spurious ';'s
00039     do
00040     {
00041         if
00042         (
00043             is.read(keywordToken).bad()
00044          || is.eof()
00045          || !keywordToken.good()
00046         )
00047         {
00048             return false;
00049         }
00050     }
00051     while (keywordToken == token::END_STATEMENT);
00052 
00053     // If the token is a valid keyword set 'keyword' return true...
00054     if (keywordToken.isWord())
00055     {
00056         keyword = keywordToken.wordToken();
00057         return true;
00058     }
00059     else if (keywordToken.isString())
00060     {
00061         // Enable wildcards
00062         keyword = keywordToken.stringToken();
00063         return true;
00064     }
00065     // If it is the end of the dictionary or file return false...
00066     else if (keywordToken == token::END_BLOCK || is.eof())
00067     {
00068         return false;
00069     }
00070     // Otherwise the token is invalid
00071     else
00072     {
00073         cerr<< "--> FOAM Warning : " << std::endl
00074             << "    From function "
00075             << "entry::getKeyword(keyType&, Istream&)" << std::endl
00076             << "    in file " << __FILE__
00077             << " at line " << __LINE__ << std::endl
00078             << "    Reading " << is.name().c_str() << std::endl
00079             << "    found " << keywordToken << std::endl
00080             << "    expected either " << token::END_BLOCK << " or EOF"
00081             << std::endl;
00082 
00083         return false;
00084     }
00085 }
00086 
00087 
00088 bool Foam::entry::New(dictionary& parentDict, Istream& is)
00089 {
00090     is.fatalCheck("entry::New(const dictionary& parentDict, Istream&)");
00091 
00092     keyType keyword;
00093 
00094     // Get the next keyword and if invalid return false
00095     if (!getKeyword(keyword, is))
00096     {
00097         return false;
00098     }
00099     else  // Keyword starts entry ...
00100     {
00101         if (keyword[0] == '#')         // ... Function entry
00102         {
00103             word functionName = keyword(1, keyword.size()-1);
00104             return functionEntry::execute(functionName, parentDict, is);
00105         }
00106         else if (keyword[0] == '$')    // ... Substitution entry
00107         {
00108             parentDict.substituteKeyword(keyword);
00109             return true;
00110         }
00111         else if (keyword == "include") // ... For backward compatibility
00112         {
00113             return functionEntries::includeEntry::execute(parentDict, is);
00114         }
00115         else                           // ... Data entries
00116         {
00117             token nextToken(is);
00118             is.putBack(nextToken);
00119 
00120             // Deal with duplicate entries
00121             bool mergeEntry = false;
00122 
00123             // See (using exact match) if entry already present
00124             entry* existingPtr = parentDict.lookupEntryPtr
00125             (
00126                 keyword,
00127                 false,
00128                 false
00129             );
00130 
00131             if (existingPtr)
00132             {
00133                 if (functionEntries::inputModeEntry::merge())
00134                 {
00135                     mergeEntry = true;
00136                 }
00137                 else if (functionEntries::inputModeEntry::overwrite())
00138                 {
00139                     // clear dictionary so merge acts like overwrite
00140                     if (existingPtr->isDict())
00141                     {
00142                         existingPtr->dict().clear();
00143                     }
00144                     mergeEntry = true;
00145                 }
00146                 else if (functionEntries::inputModeEntry::protect())
00147                 {
00148                     // read and discard the entry
00149                     if (nextToken == token::BEGIN_BLOCK)
00150                     {
00151                         dictionaryEntry dummy(keyword, parentDict, is);
00152                     }
00153                     else
00154                     {
00155                         primitiveEntry  dummy(keyword, parentDict, is);
00156                     }
00157                     return true;
00158                 }
00159                 else if (functionEntries::inputModeEntry::error())
00160                 {
00161                     FatalIOErrorIn
00162                     (
00163                         "entry::New(const dictionary& parentDict, Istream&)",
00164                         is
00165                     )
00166                         << "ERROR! duplicate entry: " << keyword
00167                         << exit(FatalIOError);
00168 
00169                     return false;
00170                 }
00171             }
00172 
00173             if (nextToken == token::BEGIN_BLOCK)
00174             {
00175                 return parentDict.add
00176                 (
00177                     new dictionaryEntry(keyword, parentDict, is),
00178                     mergeEntry
00179                 );
00180             }
00181             else
00182             {
00183                 return parentDict.add
00184                 (
00185                     new primitiveEntry(keyword, parentDict, is),
00186                     mergeEntry
00187                 );
00188             }
00189         }
00190     }
00191 }
00192 
00193 
00194 Foam::autoPtr<Foam::entry> Foam::entry::New(Istream& is)
00195 {
00196     is.fatalCheck("entry::New(Istream&)");
00197 
00198     keyType keyword;
00199 
00200     // Get the next keyword and if invalid return false
00201     if (!getKeyword(keyword, is))
00202     {
00203         return autoPtr<entry>(NULL);
00204     }
00205     else // Keyword starts entry ...
00206     {
00207         token nextToken(is);
00208         is.putBack(nextToken);
00209 
00210         if (nextToken == token::BEGIN_BLOCK)
00211         {
00212             return autoPtr<entry>
00213             (
00214                 new dictionaryEntry(keyword, dictionary::null, is)
00215             );
00216         }
00217         else
00218         {
00219             return autoPtr<entry>
00220             (
00221                 new primitiveEntry(keyword, is)
00222             );
00223         }
00224     }
00225 }
00226 
00227 
00228 // * * * * * * * * * * * * * Ostream operator  * * * * * * * * * * * * * * * //
00229 
00230 Foam::Ostream& Foam::operator<<(Ostream& os, const entry& e)
00231 {
00232     e.write(os);
00233     return os;
00234 }
00235 
00236 
00237 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines