dictionaryIO.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2016-2020 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29#include "dictionary.H"
30#include "IFstream.H"
31
32// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33
35(
36 const fileName& name,
37 const dictionary& parentDict,
38 Istream& is,
39 bool keepHeader
40)
41:
42 name_(fileName::concat(parentDict.name(), name, '.')),
43 parent_(parentDict)
44{
45 read(is, keepHeader);
46}
47
48
50:
51 dictionary(is, false)
52{}
53
54
56:
57 name_(is.name()),
58 parent_(dictionary::null)
59{
60 // Reset input mode as this is a "top-level" dictionary
62
63 read(is, keepHeader);
64}
65
66
67// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
68
70{
71 return autoPtr<dictionary>::New(is);
72}
73
74
75// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
76
77bool Foam::dictionary::read(Istream& is, bool keepHeader)
78{
79 // Normally remove FoamFile header when read, but avoid this if it already
80 // existed prior to the current read.
81 // We would otherwise lose it with every top-level '#include ...'
82
83 keepHeader = keepHeader || hashedEntries_.found("FoamFile");
84
85 // Check for empty dictionary
86 if (is.eof())
87 {
88 return true;
89 }
90
91 if (!is.good())
92 {
94 << "Istream not OK for reading dictionary " << name()
96
97 return false;
98 }
99
100 // The expected end character
101 int endChar = token::END_BLOCK;
102 token currToken(is);
103
104 if (currToken == token::END_BLOCK)
105 {
107 << "Dictionary input cannot start with '}'" << nl
108 << exit(FatalIOError);
109 }
110 else if (currToken != token::BEGIN_BLOCK)
111 {
112 is.putBack(currToken);
113 endChar = 0;
114 }
115
116 while
117 (
118 !is.eof()
119 && entry::New(*this, is, entry::inputMode::GLOBAL, endChar)
120 )
121 {}
122
123 if (!keepHeader)
124 {
125 remove("FoamFile");
126 }
127
128 if (is.bad())
129 {
131 << "Istream not OK after reading dictionary " << name()
132 << endl;
133
134 return false;
135 }
136
137 return true;
138}
139
140
142{
143 return this->read(is, false);
144}
145
146
147// * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
148
150{
151 // Reset input mode assuming this is a "top-level" dictionary
153
154 dict.clear();
155 dict.name() = is.name();
156 dict.read(is);
157
158 return is;
159}
160
161
162// * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
163
165{
167 writeEntries(os);
168 os.endBlock();
169}
170
171
173{
174 os.beginBlock(kw);
175 writeEntries(os);
176 os.endBlock();
177}
178
179
180void Foam::dictionary::writeEntries(Ostream& os, const bool extraNewLine) const
181{
182 for (const entry& e : *this)
183 {
184 // Write entry
185 os << e;
186
187 // Add extra new line between entries for "top-level" dictionaries,
188 // but not after the last entry (looks ugly).
189 if (extraNewLine && parent() == dictionary::null && e != *last())
190 {
191 os << nl;
192 }
193
194 // Check stream before going to next entry.
195 if (!os.good())
196 {
198 << "Cannot write entry " << e.keyword()
199 << " for dictionary " << name()
200 << endl;
201 }
202 }
203}
204
205
206void Foam::dictionary::write(Ostream& os, const bool subDict) const
207{
208 if (subDict)
209 {
210 os << nl;
211 os.beginBlock();
212 }
213
214 writeEntries(os, !subDict);
215
216 if (subDict)
217 {
218 os.endBlock();
219 }
220}
221
222
224{
225 dict.write(os, true);
226 return os;
227}
228
229
230// ************************************************************************* //
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:105
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:87
virtual bool read()
Re-read model coefficients if they have changed.
static autoPtr< Time > New()
Construct (dummy) Time - no functionObjects or libraries.
Definition: Time.C:717
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
dictionary()
Default construct, a top-level empty dictionary.
Definition: dictionary.C:75
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:164
void clear()
Clear the dictionary.
Definition: dictionary.C:857
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:206
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
static void resetInputMode()
Reset the globalInputMode to merge.
Definition: entry.C:61
@ GLOBAL
Use global value from globalInputMode variable.
A class for handling file names.
Definition: fileName.H:76
virtual bool write()
Write the output fields.
A class for handling keywords in dictionaries.
Definition: keyType.H:71
A token holds an item read from Istream.
Definition: token.H:69
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
@ END_BLOCK
End block [isseparator].
Definition: token.H:160
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
OBJstream os(runTime.globalPath()/outputName)
const word dictName("faMeshDefinition")
#define WarningInFunction
Report a warning using Foam::Warning.
#define InfoInFunction
Report an information message using Foam::Info.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict
volScalarField & e
Definition: createFields.H:11