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

dictionaryIO.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 "dictionary.H"
00027 #include <OpenFOAM/IFstream.H>
00028 #include <OpenFOAM/inputModeEntry.H>
00029 #include <OSspecific/regExp.H>
00030 
00031 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
00032 
00033 bool Foam::dictionary::read(Istream& is)
00034 {
00035     if (!is.good())
00036     {
00037         FatalIOErrorIn("dictionary::read(Istream&, const word&)", is)
00038             << "Istream not OK for reading dictionary "
00039             << exit(FatalIOError);
00040 
00041         return false;
00042     }
00043 
00044     token currToken(is);
00045     if (currToken != token::BEGIN_BLOCK)
00046     {
00047         is.putBack(currToken);
00048     }
00049 
00050     while (!is.eof() && entry::New(*this, is))
00051     {}
00052 
00053     // Remove the FoamFile header entry if it exists
00054     remove("FoamFile");
00055 
00056     if (is.bad())
00057     {
00058         Info<< "dictionary::read(Istream&, const word&) : "
00059             << "Istream not OK after reading dictionary " << name()
00060             << endl;
00061 
00062         return false;
00063     }
00064 
00065     return true;
00066 }
00067 
00068 
00069 bool Foam::dictionary::substituteKeyword(const word& keyword)
00070 {
00071     word varName = keyword(1, keyword.size()-1);
00072 
00073     // lookup the variable name in the given dictionary
00074     const entry* ePtr = lookupEntryPtr(varName, true, true);
00075 
00076     // if defined insert its entries into this dictionary
00077     if (ePtr != NULL)
00078     {
00079         const dictionary& addDict = ePtr->dict();
00080 
00081         forAllConstIter(IDLList<entry>, addDict, iter)
00082         {
00083             add(iter());
00084         }
00085 
00086         return true;
00087     }
00088 
00089     return false;
00090 }
00091 
00092 
00093 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00094 
00095 Foam::dictionary::dictionary
00096 (
00097     const fileName& name,
00098     const dictionary& parentDict,
00099     Istream& is
00100 )
00101 :
00102     dictionaryName(parentDict.name() + "::" + name),
00103     parent_(parentDict)
00104 {
00105     read(is);
00106 }
00107 
00108 
00109 Foam::dictionary::dictionary(Istream& is)
00110 :
00111     dictionaryName(is.name()),
00112     parent_(dictionary::null)
00113 {
00114     // Reset input mode as this is a "top-level" dictionary
00115     functionEntries::inputModeEntry::clear();
00116 
00117     read(is);
00118 }
00119 
00120 
00121 Foam::autoPtr<Foam::dictionary> Foam::dictionary::New(Istream& is)
00122 {
00123     return autoPtr<dictionary>(new dictionary(is));
00124 }
00125 
00126 
00127 // * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * * //
00128 
00129 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
00130 {
00131     // Reset input mode assuming this is a "top-level" dictionary
00132     functionEntries::inputModeEntry::clear();
00133 
00134     dict.clear();
00135     dict.name() = is.name();
00136     dict.read(is);
00137 
00138     return is;
00139 }
00140 
00141 
00142 // * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * * //
00143 
00144 void Foam::dictionary::write(Ostream& os, bool subDict) const
00145 {
00146     if (subDict)
00147     {
00148         os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
00149     }
00150 
00151     forAllConstIter(IDLList<entry>, *this, iter)
00152     {
00153         const entry& e = *iter;
00154 
00155         // Write entry
00156         os << e;
00157 
00158         // Add extra new line between entries for "top-level" dictionaries
00159         if (!subDict && parent() == dictionary::null && e != *last())
00160         {
00161             os << nl;
00162         }
00163 
00164         // Check stream before going to next entry.
00165         if (!os.good())
00166         {
00167             WarningIn("dictionary::write(Ostream&, bool subDict)")
00168                 << "Can't write entry " << iter().keyword()
00169                 << " for dictionary " << name()
00170                 << endl;
00171         }
00172     }
00173 
00174     if (subDict)
00175     {
00176         os << decrIndent << indent << token::END_BLOCK << endl;
00177     }
00178 }
00179 
00180 
00181 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
00182 {
00183     dict.write(os, true);
00184     return os;
00185 }
00186 
00187 
00188 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines