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-2021 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 
68 (
69  const fileName& fName,
70  const dictionary& parentDict
71 )
72 :
73  dictionary(fName),
74  global_(parentDict.topDict().global())
75 {
76  autoPtr<ISstream> ifsPtr
77  (
78  fileHandler().NewIFstream(fName)
79  );
80  ISstream& ifs = ifsPtr();
81 
82  if (!ifs || !ifs.good())
83  {
84  FatalIOErrorInFunction(parentDict)
85  << "Included dictionary file " << fName
86  << " cannot be found for dictionary " << parentDict.name()
87  << exit(FatalIOError);
88  }
89 
90  read(ifs);
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
95 
97 {
98  return autoPtr<dictionary>(new dictionary(is));
99 }
100 
101 
102 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
103 
104 bool Foam::dictionary::read(Istream& is, const bool keepHeader)
105 {
106  // Check for empty dictionary
107  if (is.eof())
108  {
109  return true;
110  }
111 
112  if (!is.good())
113  {
115  << "Istream not OK for reading dictionary "
116  << exit(FatalIOError);
117 
118  return false;
119  }
120 
121  token currToken(is);
122  if (currToken != token::BEGIN_BLOCK)
123  {
124  is.putBack(currToken);
125  }
126 
127  while (!is.eof() && entry::New(*this, is))
128  {}
129 
130  // normally remove the FoamFile header entry if it exists
131  if (!keepHeader)
132  {
133  remove(IOobject::foamFile);
134  }
135 
136  if (is.bad())
137  {
139  << "Istream not OK after reading dictionary " << name()
140  << endl;
141 
142  return false;
143  }
144 
145  return true;
146 }
147 
148 
150 {
151  return false;
152 }
153 
154 
156 {
157  word varName = keyword(1, keyword.size()-1);
158 
159  // lookup the variable name in the given dictionary
160  const entry* ePtr = lookupEntryPtr(varName, true, true);
161 
162  // if defined insert its entries into this dictionary
163  if (ePtr != nullptr)
164  {
165  const dictionary& addDict = ePtr->dict();
166 
167  forAllConstIter(IDLList<entry>, addDict, iter)
168  {
169  add(iter());
170  }
171 
172  return true;
173  }
174 
175  return false;
176 }
177 
178 
179 // * * * * * * * * * * * * * * * IOstream Functions * * * * * * * * * * * * //
180 
181 void Foam::writeEntry(Ostream& os, const dictionary& value)
182 {
183  os << value;
184 }
185 
186 
187 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
188 
190 {
191  // Reset input syntax as this is a "top-level" dictionary
193 
194  // Reset input mode assuming this is a "top-level" dictionary
196 
197  dict.clear();
198  dict.name() = is.name();
199  dict.read(is);
200 
201  return is;
202 }
203 
204 
205 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
206 
208 {
209  if (subDict)
210  {
211  os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
212  }
213 
214  forAllConstIter(IDLList<entry>, *this, iter)
215  {
216  const entry& e = *iter;
217 
218  // Write entry
219  os << e;
220 
221  // Add extra new line between entries for "top-level" dictionaries
222  if (!subDict && parent() == dictionary::null && (&e != last()))
223  {
224  os << nl;
225  }
226 
227  // Check stream before going to next entry.
228  if (!os.good())
229  {
231  << "Can't write entry " << iter().keyword()
232  << " for dictionary " << name()
233  << endl;
234  }
235  }
236 
237  if (subDict)
238  {
239  os << decrIndent << indent << token::END_BLOCK << endl;
240  }
241 }
242 
243 
245 {
246  dict.write(os, true);
247  return os;
248 }
249 
250 
251 // ************************************************************************* //
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:207
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:682
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:348
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
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:92
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:155
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:104
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1133
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:982
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:440
includedDictionary(const fileName &fName, const dictionary &parentDict)
Construct an included dictionary for the given parent.
Definition: dictionaryIO.C:68
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
A class for handling words, derived from string.
Definition: word.H:59
const dictionary & parent() const
Return the parent dictionary.
Definition: dictionary.H:301
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
Istream & operator>>(Istream &, directionInfo &)
static void clear()
Reset the inputMode to default (ie, merge)
static const dictionary null
Null dictionary.
Definition: dictionary.H:242
const dictionary & topDict() const
Return the top of the tree.
Definition: dictionary.C:568
const fileOperation & fileHandler()
Get current file handler.
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:336
virtual bool global() const
Return true if the dictionary global,.
Definition: dictionaryIO.C:149
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:96
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
Generic input stream.
Definition: ISstream.H:52
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
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:1463
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.