BUG: UListIO: byteSize overflowing on really big faceLists
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / dictionary / dictionaryIO.C
blob03ce2b76a7f4df417d74029d2ffd1f7959f32dbb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "dictionary.H"
27 #include "IFstream.H"
28 #include "inputModeEntry.H"
29 #include "regExp.H"
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 Foam::dictionary::dictionary
35     const fileName& name,
36     const dictionary& parentDict,
37     Istream& is
40     dictionaryName(parentDict.name() + "::" + name),
41     parent_(parentDict)
43     read(is);
47 Foam::dictionary::dictionary(Istream& is)
49     dictionaryName(is.name()),
50     parent_(dictionary::null)
52     // Reset input mode as this is a "top-level" dictionary
53     functionEntries::inputModeEntry::clear();
55     read(is);
59 Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
61     dictionaryName(is.name()),
62     parent_(dictionary::null)
64     // Reset input mode as this is a "top-level" dictionary
65     functionEntries::inputModeEntry::clear();
67     read(is, keepHeader);
71 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
73 Foam::autoPtr<Foam::dictionary> Foam::dictionary::New(Istream& is)
75     return autoPtr<dictionary>(new dictionary(is));
79 // * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * * //
81 bool Foam::dictionary::read(Istream& is, const bool keepHeader)
83     if (!is.good())
84     {
85         FatalIOErrorIn("dictionary::read(Istream&, bool)", is)
86             << "Istream not OK for reading dictionary "
87             << exit(FatalIOError);
89         return false;
90     }
92     token currToken(is);
93     if (currToken != token::BEGIN_BLOCK)
94     {
95         is.putBack(currToken);
96     }
98     while (!is.eof() && entry::New(*this, is))
99     {}
101     // normally remove the FoamFile header entry if it exists
102     if (!keepHeader)
103     {
104         remove("FoamFile");
105     }
107     if (is.bad())
108     {
109         Info<< "dictionary::read(Istream&, bool) : "
110             << "Istream not OK after reading dictionary " << name()
111             << endl;
113         return false;
114     }
116     return true;
120 bool Foam::dictionary::read(Istream& is)
122     return this->read(is, false);
126 bool Foam::dictionary::substituteKeyword(const word& keyword)
128     word varName = keyword(1, keyword.size()-1);
130     // lookup the variable name in the given dictionary
131     const entry* ePtr = lookupEntryPtr(varName, true, true);
133     // if defined insert its entries into this dictionary
134     if (ePtr != NULL)
135     {
136         const dictionary& addDict = ePtr->dict();
138         forAllConstIter(IDLList<entry>, addDict, iter)
139         {
140             add(iter());
141         }
143         return true;
144     }
146     return false;
150 // * * * * * * * * * * * * * * Istream Operator  * * * * * * * * * * * * * * //
152 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
154     // Reset input mode assuming this is a "top-level" dictionary
155     functionEntries::inputModeEntry::clear();
157     dict.clear();
158     dict.name() = is.name();
159     dict.read(is);
161     return is;
165 // * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * * //
167 void Foam::dictionary::write(Ostream& os, bool subDict) const
169     if (subDict)
170     {
171         os  << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
172     }
174     forAllConstIter(IDLList<entry>, *this, iter)
175     {
176         const entry& e = *iter;
178         // Write entry
179         os  << e;
181         // Add extra new line between entries for "top-level" dictionaries
182         if (!subDict && parent() == dictionary::null && e != *last())
183         {
184             os  << nl;
185         }
187         // Check stream before going to next entry.
188         if (!os.good())
189         {
190             WarningIn("dictionary::write(Ostream&, bool subDict)")
191                 << "Can't write entry " << iter().keyword()
192                 << " for dictionary " << name()
193                 << endl;
194         }
195     }
197     if (subDict)
198     {
199         os  << decrIndent << indent << token::END_BLOCK << endl;
200     }
204 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
206     dict.write(os, true);
207     return os;
211 // ************************************************************************* //