dictionaryListEntryIO.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) 2016 OpenFOAM Foundation
9  Copyright (C) 2021 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 "dictionaryListEntry.H"
30 #include "IOstreams.H"
31 
32 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
33 
34 // File-scope: The dictionary size without the "FoamFile" entry
35 static Foam::label realSize(const Foam::dictionary& dict)
36 {
37  if (dict.size() < 1 || dict.first()->keyword() != "FoamFile")
38  {
39  return dict.size();
40  }
41  else
42  {
43  return dict.size() - 1;
44  }
45 }
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
50 Foam::dictionaryListEntry::dictionaryListEntry
51 (
52  const dictionary& parentDict,
53  const dictionaryListEntry& dictEnt
54 )
55 :
56  dictionaryEntry(parentDict, dictEnt)
57 {}
58 
59 
60 Foam::dictionaryListEntry::dictionaryListEntry
61 (
62  const dictionary& parentDict,
63  Istream& is
64 )
65 :
67  (
68  word("entry" + Foam::name(realSize(parentDict))),
69  parentDict,
70  dictionary::null
71  )
72 {
73  token tok(is);
74  if (tok.isLabel())
75  {
76  const label len = tok.labelToken();
77 
78  is.readBeginList("List");
79 
80  for (label i=0; i<len; ++i)
81  {
82  entry::New(*this, is);
83  }
84  is.readEndList("List");
85  }
86  else if (tok.isPunctuation(token::BEGIN_LIST))
87  {
88  while (true)
89  {
90  is >> tok;
91  if (tok.error())
92  {
94  << "parsing error " << tok.info() << nl
95  << exit(FatalIOError);
96  }
97  else if (tok.isPunctuation(token::END_LIST))
98  {
99  break;
100  }
101  is.putBack(tok);
102  entry::New(*this, is);
103  }
104  }
105  else
106  {
108  << "incorrect first token, expected <int> or '(', found "
109  << tok.info() << nl
110  << exit(FatalIOError);
111  }
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
118 {
119  os << nl << indent << size()
120  << token::SPACE << "// " << keyword() << nl
122 
123  // Write contents
124  dictionary::write(os, false);
125 
126  // Write end delimiter
127  os << decrIndent << indent << token::END_LIST << nl;
128 
129  os.check(FUNCTION_NAME);
130 }
131 
132 
133 // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
134 
136 {
137  e.write(os);
138  return os;
139 }
140 
141 
142 template<>
143 Foam::Ostream& Foam::operator<<
144 (
145  Ostream& os,
146  const InfoProxy<dictionaryListEntry>& ip
147 )
148 {
149  const dictionaryListEntry& e = ip.t_;
150 
151  os << " dictionaryListEntry '" << e.keyword() << "'" << endl;
152 
153  return os;
154 }
155 
156 
157 // ************************************************************************* //
Foam::dictionaryEntry
A keyword and a list of tokens is a 'dictionaryEntry'.
Definition: dictionaryEntry.H:65
Foam::token::labelToken
label labelToken() const
Return label value.
Definition: tokenI.H:513
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::token::isLabel
bool isLabel() const noexcept
Token is LABEL.
Definition: tokenI.H:497
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:148
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:169
Foam::entry::keyword
const keyType & keyword() const noexcept
Return keyword.
Definition: entry.H:195
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:346
realSize
static Foam::label realSize(const Foam::dictionary &dict)
Definition: dictionaryListEntryIO.C:35
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::token::info
InfoProxy< token > info() const
Return info proxy for printing token information to a stream.
Definition: token.H:586
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
dictionaryListEntry.H
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:459
Foam::dictionaryListEntry::write
virtual void write(Ostream &os) const
Write.
Definition: dictionaryListEntryIO.C:117
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:353
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:339
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Global function forwards to reuseTmpDimensionedField::New.
Definition: DimensionedFieldReuseFunctions.H:105
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:206
Foam::Istream::putBack
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:70
Foam::token::SPACE
Space [isspace].
Definition: token.H:125
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:295
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:156
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Foam::dictionaryListEntry
Read/write List of dictionaries.
Definition: dictionaryListEntry.H:64
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::token::BEGIN_LIST
Begin list [isseparator].
Definition: token.H:155