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-2018 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 "inputModeEntry.H"
28 #include "regExp.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
33 (
34  const fileName& name,
35  const dictionary& parentDict,
36  Istream& is
37 )
38 :
39  dictionaryName(parentDict.name() + '.' + name),
40  parent_(parentDict)
41 {
42  read(is);
43 }
44 
45 
47 :
48  dictionaryName(is.name()),
49  parent_(dictionary::null)
50 {
51  // Reset input mode as this is a "top-level" dictionary
53 
54  read(is);
55 }
56 
57 
58 Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
59 :
60  dictionaryName(is.name()),
61  parent_(dictionary::null)
62 {
63  // Reset input mode as this is a "top-level" dictionary
65 
66  read(is, keepHeader);
67 }
68 
69 
70 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
71 
73 {
74  return autoPtr<dictionary>(new dictionary(is));
75 }
76 
77 
78 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
79 
80 bool Foam::dictionary::read(Istream& is, const bool keepHeader)
81 {
82  // Check for empty dictionary
83  if (is.eof())
84  {
85  return true;
86  }
87 
88  if (!is.good())
89  {
91  << "Istream not OK for reading dictionary "
92  << exit(FatalIOError);
93 
94  return false;
95  }
96 
97  token currToken(is);
98  if (currToken != token::BEGIN_BLOCK)
99  {
100  is.putBack(currToken);
101  }
102 
103  while (!is.eof() && entry::New(*this, is))
104  {}
105 
106  // normally remove the FoamFile header entry if it exists
107  if (!keepHeader)
108  {
109  remove("FoamFile");
110  }
111 
112  if (is.bad())
113  {
115  << "Istream not OK after reading dictionary " << name()
116  << endl;
117 
118  return false;
119  }
120 
121  return true;
122 }
123 
124 
126 {
127  return this->read(is, false);
128 }
129 
130 
132 {
133  word varName = keyword(1, keyword.size()-1);
134 
135  // lookup the variable name in the given dictionary
136  const entry* ePtr = lookupEntryPtr(varName, true, true);
137 
138  // if defined insert its entries into this dictionary
139  if (ePtr != nullptr)
140  {
141  const dictionary& addDict = ePtr->dict();
142 
143  forAllConstIter(IDLList<entry>, addDict, iter)
144  {
145  add(iter());
146  }
147 
148  return true;
149  }
150 
151  return false;
152 }
153 
154 
155 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
156 
158 {
159  // Reset input mode assuming this is a "top-level" dictionary
161 
162  dict.clear();
163  dict.name() = is.name();
164  dict.read(is);
165 
166  return is;
167 }
168 
169 
170 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
171 
173 {
174  if (subDict)
175  {
176  os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
177  }
178 
179  forAllConstIter(IDLList<entry>, *this, iter)
180  {
181  const entry& e = *iter;
182 
183  // Write entry
184  os << e;
185 
186  // Add extra new line between entries for "top-level" dictionaries
187  if (!subDict && parent() == dictionary::null && e != *last())
188  {
189  os << nl;
190  }
191 
192  // Check stream before going to next entry.
193  if (!os.good())
194  {
196  << "Can't write entry " << iter().keyword()
197  << " for dictionary " << name()
198  << endl;
199  }
200  }
201 
202  if (subDict)
203  {
204  os << decrIndent << indent << token::END_BLOCK << endl;
205  }
206 }
207 
208 
210 {
211  dict.write(os, true);
212  return os;
213 }
214 
215 
216 // ************************************************************************* //
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:172
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:125
dictionary dict
A class for handling file names.
Definition: fileName.H:69
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:226
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:470
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:137
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:202
A token holds items read from Istream.
Definition: token.H:69
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:107
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
bool substituteKeyword(const word &keyword)
Substitute the given keyword prepended by &#39;$&#39; with the.
Definition: dictionaryIO.C:131
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
T * last()
Return the last entry.
Definition: UILList.H:118
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:814
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:692
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:238
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
A class for handling words, derived from string.
Definition: word.H:59
const dictionary & parent() const
Return the parent dictionary.
Definition: dictionary.H:260
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:53
static const char nl
Definition: Ostream.H:265
static autoPtr< dictionary > New(Istream &)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:72
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:240
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:98
void clear()
Clear the dictionary.
Definition: dictionary.C:1135
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:233
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.