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
111  }
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
117 void Foam::dictionaryListEntry::write(Ostream& os) const
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 
130 }
131 
132 
133 // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
134 
135 Foam::Ostream& Foam::operator<<(Ostream& os, const dictionaryListEntry& e)
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:61
Foam::token::labelToken
label labelToken() const
Definition: tokenI.H:506
Foam::entry::New
static bool New(dictionary &parentDict, Istream &is, const inputMode inpMode=inputMode::GLOBAL, const int endChar=0)
Definition: entryIO.C:98
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
Definition: tokenI.H:490
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:63
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:41
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Definition: Istream.C:141
Foam::Istream::readEndList
char readEndList(const char *funcName)
Definition: Istream.C:162
Foam::entry::keyword
const keyType & keyword() const noexcept
Definition: entry.H:191
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Definition: Ostream.H:381
Foam::token
A token holds an item read from Istream.
Definition: token.H:64
Foam::incrIndent
Ostream & incrIndent(Ostream &os)
Definition: Ostream.H:352
realSize
static Foam::label realSize(const Foam::dictionary &dict)
Definition: dictionaryListEntryIO.C:28
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Definition: boundaryPatch.C:76
Foam::token::info
InfoProxy< token > info() const
Definition: token.H:582
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
dictionaryListEntry.H
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Definition: tokenI.H:452
Foam::dictionaryListEntry::write
virtual void write(Ostream &os) const
Definition: dictionaryListEntryIO.C:110
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Definition: IOstream.C:51
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:119
os
OBJstream os(runTime.globalPath()/outputName)
Foam
Definition: atmBoundaryLayer.C:26
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Definition: Ostream.H:361
Foam::indent
Ostream & indent(Ostream &os)
Definition: Ostream.H:343
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
Foam::New
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
Definition: DimensionedFieldReuseFunctions.H:100
Foam::nl
constexpr char nl
Definition: Ostream.H:424
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Definition: dictionaryIO.C:199
Foam::Istream::putBack
void putBack(const token &tok)
Definition: Istream.C:63
Foam::token::SPACE
@ SPACE
Space [isspace].
Definition: token.H:121
Foam::constant::electromagnetic::e
const dimensionedScalar e
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:302
Foam::token::END_LIST
@ END_LIST
End list [isseparator].
Definition: token.H:152
Foam::name
word name(const expressions::valueTypeCode typeCode)
Definition: exprTraits.C:52
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Definition: error.H:494
Foam::dictionaryListEntry
Read/write List of dictionaries.
Definition: dictionaryListEntry.H:60
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:52
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Begin list [isseparator].
Definition: token.H:151