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 
55 Foam::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
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 
141 bool Foam::dictionary::read(Istream& is)
142 {
143  return this->read(is, false);
144 }
145 
146 
147 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
148 
149 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
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 
172 void Foam::dictionary::writeEntry(const keyType& kw, Ostream& os) const
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 
223 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
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:63
Foam::autoPtr::New
static autoPtr< T > New(Args &&... args)
InfoInFunction
#define InfoInFunction
Definition: messageStream.H:387
Foam::entry::New
static bool New(dictionary &parentDict, Istream &is, const inputMode inpMode=inputMode::GLOBAL, const int endChar=0)
Definition: entryIO.C:98
Foam::dictionary::name
const fileName & name() const noexcept
Definition: dictionaryI.H:41
Foam::fileName
A class for handling file names.
Definition: fileName.H:71
Foam::dictionary::New
static autoPtr< dictionary > New(Istream &is)
Definition: dictionaryIO.C:62
Foam::dictionary::dictionary
dictionary()
Definition: dictionary.C:68
Foam::fileName::name
static std::string name(const std::string &str)
Definition: fileNameI.H:192
Foam::read
bool read(const char *buf, int32_t &val)
Definition: int32.H:125
dictName
const word dictName("faMeshDefinition")
Foam::FatalIOError
IOerror FatalIOError
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:223
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::token
A token holds an item read from Istream.
Definition: token.H:64
Foam::Ostream::beginBlock
virtual Ostream & beginBlock(const keyType &kw)
Definition: Ostream.C:84
Foam::IOstream::good
bool good() const noexcept
Definition: IOstream.H:229
Foam::dictionary::null
static const dictionary null
Definition: dictionary.H:388
Foam::blockMeshTools::read
void read(Istream &, label &val, const dictionary &)
Definition: blockMeshTools.C:50
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Definition: boundaryPatch.C:76
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:66
Foam::IOstream::name
virtual const fileName & name() const
Definition: IOstream.C:33
Foam::dictionary::writeEntry
void writeEntry(Ostream &os) const
Definition: dictionaryIO.C:157
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::IOstream::eof
bool eof() const noexcept
Definition: IOstream.H:235
Foam::entry::resetInputMode
static void resetInputMode()
Definition: entry.C:54
IFstream.H
Foam::token::END_BLOCK
@ END_BLOCK
End block [isseparator].
Definition: token.H:156
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary::writeEntries
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Definition: dictionaryIO.C:173
Foam::Ostream::endBlock
virtual Ostream & endBlock()
Definition: Ostream.C:102
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IOstream::bad
bool bad() const noexcept
Definition: IOstream.H:247
Foam::entry::inputMode::GLOBAL
@ GLOBAL
Use global value from globalInputMode variable.
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Begin block [isseparator].
Definition: token.H:155
Foam::dictionary::read
bool read(Istream &is)
Definition: dictionaryIO.C:134
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::autoPtr< Foam::dictionary >
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Definition: dictionaryIO.C:199
Foam::Istream::putBack
void putBack(const token &tok)
Definition: Istream.C:63
Foam::constant::electromagnetic::e
const dimensionedScalar e
Definition: createFields.H:11
dictionary.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Definition: error.H:494
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::dictionary::clear
void clear()
Definition: dictionary.C:850
WarningInFunction
#define WarningInFunction
Definition: messageStream.H:353