initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / dictionary / dictionaryIO.C
blob96196e3a11ab9a9b8e2913405f54f85f28262302
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     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
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "dictionary.H"
28 #include "IFstream.H"
29 #include "inputModeEntry.H"
30 #include "regExp.H"
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
34 bool Foam::dictionary::read(Istream& is)
36     if (!is.good())
37     {
38         FatalIOErrorIn("dictionary::read(Istream&, const word&)", is)
39             << "Istream not OK for reading dictionary "
40             << exit(FatalIOError);
42         return false;
43     }
45     token currToken(is);
46     if (currToken != token::BEGIN_BLOCK)
47     {
48         is.putBack(currToken);
49     }
51     while (!is.eof() && entry::New(*this, is))
52     {}
54     // Remove the FoamFile header entry if it exists
55     remove("FoamFile");
57     if (is.bad())
58     {
59         Info<< "dictionary::read(Istream&, const word&) : "
60             << "Istream not OK after reading dictionary " << name()
61             << endl;
63         return false;
64     }
66     return true;
70 bool Foam::dictionary::substituteKeyword(const word& keyword)
72     word varName = keyword(1, keyword.size()-1);
74     // lookup the variable name in the given dictionary
75     const entry* ePtr = lookupEntryPtr(varName, true, true);
77     // if defined insert its entries into this dictionary
78     if (ePtr != NULL)
79     {
80         const dictionary& addDict = ePtr->dict();
82         forAllConstIter(IDLList<entry>, addDict, iter)
83         {
84             add(iter());
85         }
87         return true;
88     }
90     return false;
94 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
96 Foam::dictionary::dictionary
98     const dictionary& parentDict,
99     Istream& is
102     name_(is.name()),
103     parent_(parentDict)
105     read(is);
109 Foam::dictionary::dictionary(Istream& is)
111     name_(is.name()),
112     parent_(dictionary::null)
114     // Reset input mode as this is a "top-level" dictionary
115     functionEntries::inputModeEntry::clear();
117     read(is);
121 Foam::autoPtr<Foam::dictionary> Foam::dictionary::New(Istream& is)
123     return autoPtr<dictionary>(new dictionary(is));
127 // * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * * //
129 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
131     // Reset input mode assuming this is a "top-level" dictionary
132     functionEntries::inputModeEntry::clear();
134     dict.clear();
135     dict.read(is);
137     return is;
141 // * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * * //
143 void Foam::dictionary::write(Ostream& os, bool subDict) const
145     if (subDict)
146     {
147         os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
148     }
150     forAllConstIter(IDLList<entry>, *this, iter)
151     {
152         const entry& e = *iter;
154         // Write entry
155         os << e;
157         // Add extra new line between entries for "top-level" dictionaries
158         if (!subDict && parent() == dictionary::null && e != *last())
159         {
160             os << nl;
161         }
163         // Check stream before going to next entry.
164         if (!os.good())
165         {
166             WarningIn("dictionary::write(Ostream&, bool subDict)")
167                 << "Can't write entry " << iter().keyword()
168                 << " for dictionary " << name()
169                 << endl;
170         }
171     }
173     if (subDict)
174     {
175         os << decrIndent << indent << token::END_BLOCK << endl;
176     }
180 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
182     dict.write(os, true);
183     return os;
187 // ************************************************************************* //