FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/IFstream.H>
29 #include <OSspecific/regExp.H>
30 
31 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
32 
34 {
35  if (!is.good())
36  {
37  FatalIOErrorIn("dictionary::read(Istream&, const word&)", is)
38  << "Istream not OK for reading dictionary "
39  << exit(FatalIOError);
40 
41  return false;
42  }
43 
44  token currToken(is);
45  if (currToken != token::BEGIN_BLOCK)
46  {
47  is.putBack(currToken);
48  }
49 
50  while (!is.eof() && entry::New(*this, is))
51  {}
52 
53  // Remove the FoamFile header entry if it exists
54  remove("FoamFile");
55 
56  if (is.bad())
57  {
58  Info<< "dictionary::read(Istream&, const word&) : "
59  << "Istream not OK after reading dictionary " << name()
60  << endl;
61 
62  return false;
63  }
64 
65  return true;
66 }
67 
68 
70 {
71  word varName = keyword(1, keyword.size()-1);
72 
73  // lookup the variable name in the given dictionary
74  const entry* ePtr = lookupEntryPtr(varName, true, true);
75 
76  // if defined insert its entries into this dictionary
77  if (ePtr != NULL)
78  {
79  const dictionary& addDict = ePtr->dict();
80 
81  forAllConstIter(IDLList<entry>, addDict, iter)
82  {
83  add(iter());
84  }
85 
86  return true;
87  }
88 
89  return false;
90 }
91 
92 
93 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
94 
96 (
97  const fileName& name,
98  const dictionary& parentDict,
99  Istream& is
100 )
101 :
102  dictionaryName(parentDict.name() + "::" + name),
103  parent_(parentDict)
104 {
105  read(is);
106 }
107 
108 
110 :
111  dictionaryName(is.name()),
112  parent_(dictionary::null)
113 {
114  // Reset input mode as this is a "top-level" dictionary
116 
117  read(is);
118 }
119 
120 
122 {
123  return autoPtr<dictionary>(new dictionary(is));
124 }
125 
126 
127 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
128 
130 {
131  // Reset input mode assuming this is a "top-level" dictionary
133 
134  dict.clear();
135  dict.name() = is.name();
136  dict.read(is);
137 
138  return is;
139 }
140 
141 
142 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
143 
144 void Foam::dictionary::write(Ostream& os, bool subDict) const
145 {
146  if (subDict)
147  {
148  os << nl << indent << token::BEGIN_BLOCK << incrIndent << nl;
149  }
150 
151  forAllConstIter(IDLList<entry>, *this, iter)
152  {
153  const entry& e = *iter;
154 
155  // Write entry
156  os << e;
157 
158  // Add extra new line between entries for "top-level" dictionaries
159  if (!subDict && parent() == dictionary::null && e != *last())
160  {
161  os << nl;
162  }
163 
164  // Check stream before going to next entry.
165  if (!os.good())
166  {
167  WarningIn("dictionary::write(Ostream&, bool subDict)")
168  << "Can't write entry " << iter().keyword()
169  << " for dictionary " << name()
170  << endl;
171  }
172  }
173 
174  if (subDict)
175  {
176  os << decrIndent << indent << token::END_BLOCK << endl;
177  }
178 }
179 
180 
182 {
183  dict.write(os, true);
184  return os;
185 }
186 
187 
188 // ************************ vim: set sw=4 sts=4 et: ************************ //