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 | Copyright (C) 2011-2016 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 "IFstream.H"
28 #include "inputModeEntry.H"
29 #include "regExp.H"
30 
31 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
32 
34 (
35  const fileName& name,
36  const dictionary& parentDict,
37  Istream& is
38 )
39 :
40  dictionaryName(parentDict.name() + '.' + name),
41  parent_(parentDict)
42 {
43  read(is);
44 }
45 
46 
48 :
49  dictionaryName(is.name()),
50  parent_(dictionary::null)
51 {
52  // Reset input mode as this is a "top-level" dictionary
54 
55  read(is);
56 }
57 
58 
59 Foam::dictionary::dictionary(Istream& is, const bool keepHeader)
60 :
61  dictionaryName(is.name()),
62  parent_(dictionary::null)
63 {
64  // Reset input mode as this is a "top-level" dictionary
66 
67  read(is, keepHeader);
68 }
69 
70 
71 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
72 
74 {
75  return autoPtr<dictionary>(new dictionary(is));
76 }
77 
78 
79 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
80 
81 bool Foam::dictionary::read(Istream& is, const bool keepHeader)
82 {
83  // Check for empty dictionary
84  if (is.eof())
85  {
86  return true;
87  }
88 
89  if (!is.good())
90  {
92  << "Istream not OK for reading dictionary "
93  << exit(FatalIOError);
94 
95  return false;
96  }
97 
98  token currToken(is);
99  if (currToken != token::BEGIN_BLOCK)
100  {
101  is.putBack(currToken);
102  }
103 
104  while (!is.eof() && entry::New(*this, is))
105  {}
106 
107  // normally remove the FoamFile header entry if it exists
108  if (!keepHeader)
109  {
110  remove("FoamFile");
111  }
112 
113  if (is.bad())
114  {
116  << "Istream not OK after reading dictionary " << name()
117  << endl;
118 
119  return false;
120  }
121 
122  return true;
123 }
124 
125 
127 {
128  return this->read(is, false);
129 }
130 
131 
133 {
134  word varName = keyword(1, keyword.size()-1);
135 
136  // lookup the variable name in the given dictionary
137  const entry* ePtr = lookupEntryPtr(varName, true, true);
138 
139  // if defined insert its entries into this dictionary
140  if (ePtr != NULL)
141  {
142  const dictionary& addDict = ePtr->dict();
143 
144  forAllConstIter(IDLList<entry>, addDict, iter)
145  {
146  add(iter());
147  }
148 
149  return true;
150  }
151 
152  return false;
153 }
154 
155 
156 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
157 
159 {
160  // Reset input mode assuming this is a "top-level" dictionary
162 
163  dict.clear();
164  dict.name() = is.name();
165  dict.read(is);
166 
167  return is;
168 }
169 
170 
171 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
172 
174 {
175  if (subDict)
176  {
177  os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
178  }
179 
180  forAllConstIter(IDLList<entry>, *this, iter)
181  {
182  const entry& e = *iter;
183 
184  // Write entry
185  os << e;
186 
187  // Add extra new line between entries for "top-level" dictionaries
188  if (!subDict && parent() == dictionary::null && e != *last())
189  {
190  os << nl;
191  }
192 
193  // Check stream before going to next entry.
194  if (!os.good())
195  {
197  << "Can't write entry " << iter().keyword()
198  << " for dictionary " << name()
199  << endl;
200  }
201  }
202 
203  if (subDict)
204  {
205  os << decrIndent << indent << token::END_BLOCK << endl;
206  }
207 }
208 
209 
211 {
212  dict.write(os, true);
213  return os;
214 }
215 
216 
217 // ************************************************************************* //
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
dictionary dict
A class for handling file names.
Definition: fileName.H:69
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:223
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
const double e
Elementary charge.
Definition: doubleFloat.H:78
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:193
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:633
A token holds items read from Istream.
Definition: token.H:69
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:91
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:345
bool substituteKeyword(const word &keyword)
Substitute the given keyword prepended by &#39;$&#39; with the.
Definition: dictionaryIO.C:132
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
entry * last()
Return the last entry.
Definition: UILList.H:118
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:737
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:113
bool read(const char *, int32_t &)
Definition: int32IO.C:85
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
static void clear()
Reset the inputMode to default (ie, merge)
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:29
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
static const char nl
Definition: Ostream.H:262
static autoPtr< dictionary > New(Istream &)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:73
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:237
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
#define WarningInFunction
Report a warning using Foam::Warning.
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Ostream & operator<<(Ostream &, const ensightPart &)
const dictionary & parent() const
Return the parent dictionary.
Definition: dictionary.H:251
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:53
void clear()
Clear the dictionary.
Definition: dictionary.C:1058
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:230
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.