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-2015 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  {
115  Info<< "dictionary::read(Istream&, bool) : "
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 
173 void Foam::dictionary::write(Ostream& os, bool subDict) const
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::dictionary::New
static autoPtr< dictionary > New(Istream &)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:73
inputModeEntry.H
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::IOstream::eof
bool eof() const
Return true if end of input seen.
Definition: IOstream.H:339
Foam::dictionary::dictionary
dictionary()
Construct top-level dictionary null.
Definition: dictionary.C:113
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::FatalIOError
IOerror FatalIOError
Foam::functionEntries::inputModeEntry::clear
static void clear()
Reset the inputMode to default (ie, merge)
Definition: inputModeEntry.C:109
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:228
forAllConstIter
forAllConstIter(PtrDictionary< phaseModel >, mixture.phases(), phase)
Definition: pEqn.H:39
Foam::dictionary::substituteKeyword
bool substituteKeyword(const word &keyword)
Substitute the given keyword prepended by '$' with the.
Definition: dictionaryIO.C:132
Foam::IDLList< entry >
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::Info
messageStream Info
IFstream.H
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:870
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:235
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:221
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::autoPtr
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:117
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::Istream::putBack
void putBack(const token &)
Put back token.
Definition: Istream.C:30
dictionary.H
Foam::operator>>
Istream & operator>>(Istream &, edgeMesh &)
Definition: edgeMeshIO.C:141
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
Foam::entry::New
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:91
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::dictionary::read
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
regExp.H
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::dictionary::clear
void clear()
Clear the dictionary.
Definition: dictionary.C:1050
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
WarningInFunction
#define WarningInFunction
Report a warning using Foam::Warning.
Definition: messageStream.H:259
Foam::dictionaryName
Definition: dictionary.H:78
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::null
static const dictionary null
Null dictionary.
Definition: dictionary.H:193
Foam::dictionary::write
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173