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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "dictionary.H"
30 #include "IFstream.H"
31 
32 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
33 
35 (
36  const fileName& name,
37  const dictionary& parentDict,
38  Istream& is,
39  bool keepHeader
40 )
41 :
42  name_(fileName::concat(parentDict.name(), name, '/')),
43  parent_(parentDict)
44 {
45  read(is, keepHeader);
46 }
47 
48 
50 :
51  dictionary(is, false)
52 {}
53 
54 
55 Foam::dictionary::dictionary(Istream& is, bool keepHeader)
56 :
57  name_(is.name()),
58  parent_(dictionary::null)
59 {
60  // Reset input mode as this is a "top-level" dictionary
62 
63  read(is, keepHeader);
64 }
65 
66 
67 // * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * * //
68 
70 {
71  return autoPtr<dictionary>::New(is);
72 }
73 
74 
75 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
76 
77 bool Foam::dictionary::read(Istream& is, bool keepHeader)
78 {
79  // Normally remove FoamFile header when read, but avoid this if it already
80  // existed prior to the current read.
81  // We would otherwise lose it with every top-level '#include ...'
82 
83  keepHeader = keepHeader || hashedEntries_.found("FoamFile");
84 
85  // Check for empty dictionary
86  if (is.eof())
87  {
88  return true;
89  }
90 
91  if (!is.good())
92  {
94  << "Istream not OK for reading dictionary " << name()
95  << exit(FatalIOError);
96 
97  return false;
98  }
99 
100  // The expected end character
101  int endChar = token::END_BLOCK;
102  token currToken(is);
103 
104  if (currToken == token::END_BLOCK)
105  {
107  << "Dictionary input cannot start with '}'" << nl
108  << exit(FatalIOError);
109  }
110  else if (currToken != token::BEGIN_BLOCK)
111  {
112  is.putBack(currToken);
113  endChar = 0;
114  }
115 
116  while
117  (
118  !is.eof()
119  && entry::New(*this, is, entry::inputMode::GLOBAL, endChar)
120  )
121  {}
122 
123  if (!keepHeader)
124  {
125  remove("FoamFile");
126  }
127 
128  if (is.bad())
129  {
131  << "Istream not OK after reading dictionary " << name()
132  << endl;
133 
134  return false;
135  }
136 
137  return true;
138 }
139 
140 
141 bool Foam::dictionary::read(Istream& is)
142 {
143  return this->read(is, false);
144 }
145 
146 
147 // * * * * * * * * * * * * * * Istream Operator * * * * * * * * * * * * * * //
148 
149 Foam::Istream& Foam::operator>>(Istream& is, dictionary& dict)
150 {
151  // Reset input mode assuming this is a "top-level" dictionary
153 
154  dict.clear();
155  dict.name() = is.name();
156  dict.read(is);
158  return is;
159 }
160 
161 
162 // * * * * * * * * * * * * * * Ostream Operator * * * * * * * * * * * * * * //
163 
165 {
167  writeEntries(os);
168  os.endBlock();
169 }
170 
171 
172 void Foam::dictionary::writeEntry(const keyType& kw, Ostream& os) const
173 {
174  os.beginBlock(kw);
175  writeEntries(os);
176  os.endBlock();
177 }
178 
179 
180 void Foam::dictionary::writeEntries(Ostream& os, const bool extraNewLine) const
181 {
182  // Add extra new line separation between entries
183  // for "top-level" dictionaries
184 
185  const bool addLine = (extraNewLine && parent() == dictionary::null);
186  bool separator = false;
187 
188  for (const entry& e : *this)
189  {
190  if (separator)
191  {
192  os << nl;
193  }
194  separator = addLine;
195 
196  // Write entry
197  os << e;
198 
199  // Check stream before going to next entry.
200  if (!os.good())
201  {
203  << "Cannot write entry " << e.keyword()
204  << " for dictionary " << name()
205  << endl;
206  }
207  }
208 }
209 
210 
211 void Foam::dictionary::write(Ostream& os, const bool subDict) const
212 {
213  if (subDict)
214  {
215  os << nl;
216  os.beginBlock();
217  }
218 
219  writeEntries(os, !subDict);
220 
221  if (subDict)
222  {
223  os.endBlock();
224  }
225 }
226 
227 
228 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionary& dict)
229 {
230  dict.write(os, true);
231  return os;
232 }
233 
234 
235 // ************************************************************************* //
void writeEntry(Ostream &os) const
Write sub-dictionary with its dictName as its header.
Definition: dictionaryIO.C:157
Begin block [isseparator].
Definition: token.H:165
A class for handling keywords in dictionaries.
Definition: keyType.H:66
dictionary dict
A class for handling file names.
Definition: fileName.H:72
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:305
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
Definition: dictionaryIO.C:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
const word dictName("faMeshDefinition")
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:41
static void resetInputMode()
Reset the globalInputMode to merge.
Definition: entry.C:54
bool read(const char *buf, int32_t &val)
Same as readInt32.
Definition: int32.H:127
dictionary()
Default construct, a top-level empty dictionary.
Definition: dictionary.C:68
void putBack(const token &tok)
Put back a token (copy). Only a single put back is permitted.
Definition: Istream.C:71
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
static autoPtr< dictionary > New(Istream &is)
Construct top-level dictionary on freestore from Istream.
Definition: dictionaryIO.C:62
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
Istream & operator>>(Istream &, directionInfo &)
virtual Ostream & endBlock()
Write end block group.
Definition: Ostream.C:108
static const dictionary null
An empty dictionary, which is also the parent for all dictionaries.
Definition: dictionary.H:474
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
Definition: fileNameI.H:192
void read(Istream &, label &val, const dictionary &)
In-place read with dictionary lookup.
OBJstream os(runTime.globalPath()/outputName)
Use global value from globalInputMode variable.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:204
#define WarningInFunction
Report a warning using Foam::Warning.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
virtual Ostream & beginBlock(const keyType &kw)
Write begin block group with the given name.
Definition: Ostream.C:90
static bool New(dictionary &parentDict, Istream &is, const inputMode inpMode=inputMode::GLOBAL, const int endChar=0)
Construct from an Istream and insert into the dictionary.
Definition: entryIO.C:98
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition: autoPtr.H:178
void clear()
Clear the dictionary.
Definition: dictionary.C:844
End block [isseparator].
Definition: token.H:166
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:173
bool eof() const noexcept
True if end of input seen.
Definition: IOstream.H:289
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.