dictionaryIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2019 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "dictionary.H"
27 #include "IOobject.H"
28 #include "inputSyntaxEntry.H"
29 #include "inputModeEntry.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const fileName& name,
36  const dictionary& parentDict,
37  Istream& is
38 )
39 :
41  (
42  parentDict.name().size()
43  ? parentDict.name()/name
44  : name
45  ),
46  parent_(parentDict)
47 {
48  read(is);
49 }
50 
51 
52 Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
53 :
54  dictionaryName(is.name()),
55  parent_(dictionary::null)
56 {
57  // Reset input syntax as this is a "top-level" dictionary
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 dictionary(is));
72 }
73 
74 
75 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
76 
77 bool Foam::dictionary::read(Istream& is, const bool keepHeader)
78 {
79  // Check for empty dictionary
80  if (is.eof())
81  {
82  return true;
83  }
84 
85  if (!is.good())
86  {
88  << "Istream not OK for reading dictionary "
89  << exit(FatalIOError);
90 
91  return false;
92  }
93 
94  token currToken(is);
95  if (currToken != token::BEGIN_BLOCK)
96  {
97  is.putBack(currToken);
98  }
99 
100  while (!is.eof() && entry::New(*this, is))
101  {}
102 
103  // normally remove the FoamFile header entry if it exists
104  if (!keepHeader)
105  {
106  remove(IOobject::foamFile);
107  }
108 
109  if (is.bad())
110  {
112  << "Istream not OK after reading dictionary " << name()
113  << endl;
114 
115  return false;
116  }
117 
118  return true;
119 }
120 
121 
123 {
124  word varName = keyword(1, keyword.size()-1);
125 
126  // lookup the variable name in the given dictionary
127  const entry* ePtr = lookupEntryPtr(varName, true, true);
128 
129  // if defined insert its entries into this dictionary
130  if (ePtr != nullptr)
131  {
132  const dictionary& addDict = ePtr->dict();
133 
134  forAllConstIter(IDLList<entry>, addDict, iter)
135  {
136  add(iter());
137  }
138 
139  return true;
140  }
141 
142  return false;
143 }
144 
145 
146 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
147 
148 void Foam::writeEntry(Ostream& os, const dictionary& value)
149 {
150  os << value;
151 }
152 
153 
154 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
155 
157 {
158  // Reset input syntax as this is a "top-level" dictionary
160 
161  // Reset input mode assuming this is a "top-level" dictionary
163 
164  dict.clear();
165  dict.name() = is.name();
166  dict.read(is);
167 
168  return is;
169 }
170 
171 
172 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
173 
175 {
176  if (subDict)
177  {
178  os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
179  }
180 
181  forAllConstIter(IDLList<entry>, *this, iter)
182  {
183  const entry& e = *iter;
184 
185  // Write entry
186  os << e;
187 
188  // Add extra new line between entries for "top-level" dictionaries
189  if (!subDict && 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  << "Can't write entry " << iter().keyword()
199  << " for dictionary " << name()
200  << endl;
201  }
202  }
203 
204  if (subDict)
205  {
206  os << decrIndent << indent << token::END_BLOCK << endl;
207  }
208 }
209 
210 
212 {
213  dict.write(os, true);
214  return os;
215 }
216 
217 
218 // ************************************************************************* //
Template class for intrusive linked lists.
Definition: ILList.H:50
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:174
dictionary dict
A class for handling file names.
Definition: fileName.H:79
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:706
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
static const dictionary null
Null dictionary.
Definition: dictionary.H:241
A token holds items read from Istream.
Definition: token.H:72
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:94
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool substituteKeyword(const word &keyword)
Substitute the given keyword prepended by &#39;$&#39; with the.
Definition: dictionaryIO.C:122
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
T * last()
Return the last entry.
Definition: UILList.H:121
bool read(Istream &, const bool keepHeader=false)
Read dictionary from Istream, optionally keeping the header.
Definition: dictionaryIO.C:77
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1056
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:934
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:464
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A class for handling words, derived from string.
Definition: word.H:59
const dictionary & parent() const
Return the parent dictionary.
Definition: dictionary.H:296
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
Istream & operator>>(Istream &, directionInfo &)
static void clear()
Reset the inputMode to default (ie, merge)
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
static const char nl
Definition: Ostream.H:260
static void clear()
Reset the inputSyntax to the default specified in etc/controlDict.
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:98
static autoPtr< dictionary > New(Istream &)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:69
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
void writeEntry(Ostream &os, const HashTable< T, Key, Hash > &ht)
Definition: HashTableIO.C:96
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Ostream & operator<<(Ostream &, const ensightPart &)
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
void clear()
Clear the dictionary.
Definition: dictionary.C:1388
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.