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 -------------------------------------------------------------------------------
11 License
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 
77 bool 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()
95  << exit(FatalIOError);
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 
180 void 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 
206 void 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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
Construct autoPtr of T with forwarding arguments.
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
Foam::entry::New
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:105
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::dictionary::New
static autoPtr< dictionary > New(Istream &is)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:69
Foam::dictionary::dictionary
dictionary()
Default construct, a top-level empty dictionary.
Definition: dictionary.C:75
Foam::read
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:108
dictName
const word dictName("faMeshDefinition")
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:91
Foam::dictionary::name
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Foam::dictionary::null
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:392
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
Definition: blockMeshTools.C:57
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:68
Foam::dictionary::writeEntry
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:164
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::IOstream::eof
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:239
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
Foam::entry::resetInputMode
static void resetInputMode()
Reset the globalInputMode to merge.
Definition: entry.C:61
IFstream.H
Foam::token::END_BLOCK
End block [isseparator].
Definition: token.H:160
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary::writeEntries
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:109
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IOstream::bad
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
Foam::entry::inputMode::GLOBAL
Use global value from globalInputMode variable.
Foam::token::BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:159
Foam::dictionary::read
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::autoPtr< Foam::dictionary >
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:206
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
dictionary.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::dictionary::clear
void clear()
Clear the dictionary.
Definition: dictionary.C:857
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:328