Loading...
Searching...
No Matches
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
50:
51 dictionary(is, false)
52{}
53
54
55Foam::dictionary::dictionary(Istream& is, bool keepHeader)
56:
57 name_(is.name()),
58 parent_(dictionary::null)
59{
60 // Reset input mode as this is a "top-level" dictionary
63 read(is, keepHeader);
64}
65
66
67// * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
68
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
141bool Foam::dictionary::read(Istream& is)
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);
158 return is;
159}
160
161
162// * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
163
166 os.beginBlock(dictName());
168 os.endBlock();
169}
170
171
172void Foam::dictionary::writeEntry(const keyType& kw, Ostream& os) const
174 os.beginBlock(kw);
176 os.endBlock();
177}
178
179
180void Foam::dictionary::writeEntries(Ostream& os, const bool extraNewLine) const
181{
182 // Add extra new line separation between entries
183 // for "top-level" dictionaries
184
185 const bool addLine = (extraNewLine && parent() == dictionary::null);
186 bool separator = false;
187
188 for (const entry& e : *this)
189 {
190 if (separator)
191 {
192 os << nl;
193 }
194 separator = addLine;
195
196 // Write entry
197 os << e;
198
199 // Check stream before going to next entry.
200 if (!os.good())
201 {
203 << "Cannot write entry " << e.keyword()
204 << " for dictionary " << name()
205 << endl;
206 }
207 }
208}
209
210
211void Foam::dictionary::write(Ostream& os, const bool subDict) const
212{
213 if (subDict)
214 {
215 os << nl;
216 os.beginBlock();
217 }
218
219 writeEntries(os, !subDict);
220
221 if (subDict)
222 {
223 os.endBlock();
224 }
225}
226
227
228Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
229{
230 dict.write(os, true);
231 return os;
232}
233
234
235// ************************************************************************* //
bool good() const noexcept
True if next operation might succeed.
Definition IOstream.H:281
bool bad() const noexcept
True if stream is corrupted.
Definition IOstream.H:305
virtual const fileName & name() const
The name of the stream.
Definition IOstream.C:33
bool eof() const noexcept
True if end of input seen.
Definition IOstream.H:289
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition Istream.C:71
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition Ostream.H:59
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition Ostream.C:90
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const fileName & name() const noexcept
The dictionary name.
Definition dictionaryI.H:41
const dictionary & subDict(const word &keyword, enum keyType::option matchOpt=keyType::REGEX) const
Find and return a sub-dictionary.
Definition dictionary.C:441
dictionary()
Default construct, a top-level empty dictionary.
Definition dictionary.C:68
bool remove(const word &keyword)
Remove an entry specified by keyword.
static autoPtr< dictionary > New(Istream &is)
Construct top-level dictionary on freestore from Istream.
friend class entry
Declare friendship with the entry class for IO.
Definition dictionary.H:338
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
const dictionary & parent() const noexcept
Return the parent dictionary.
Definition dictionaryI.H:77
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition dictionary.H:487
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
static void resetInputMode()
Reset the globalInputMode to merge.
Definition entry.C:54
static bool New(dictionary &parentDict, Istream &is, const inputMode inpMode=inputMode::GLOBAL, const int endChar=0)
Construct from an Istream and insert into the dictionary.
Definition entryIO.C:98
@ GLOBAL
Use global value from globalInputMode variable.
Definition entry.H:81
A class for handling file names.
Definition fileName.H:75
A class for handling keywords in dictionaries.
Definition keyType.H:69
A token holds an item read from Istream.
Definition token.H:70
@ BEGIN_BLOCK
Begin block [isseparator].
Definition token.H:178
@ END_BLOCK
End block [isseparator].
Definition token.H:179
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
OBJstream os(runTime.globalPath()/outputName)
const word dictName("faMeshDefinition")
auto & name
#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:127
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces).
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition Ostream.H:519
Istream & operator>>(Istream &, directionInfo &)
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition exprTraits.C:127
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
dictionary dict
volScalarField & e