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

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