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 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "dictionaryListEntry.H"
29 #include "IOstreams.H"
30 
31 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
32 
33 // File-scope: The dictionary size without the "FoamFile" entry
34 static Foam::label realSize(const Foam::dictionary& dict)
35 {
36  if (dict.size() < 1 || dict.first()->keyword() != "FoamFile")
37  {
38  return dict.size();
39  }
40  else
41  {
42  return dict.size() - 1;
43  }
44 }
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
49 Foam::dictionaryListEntry::dictionaryListEntry
50 (
51  const dictionary& parentDict,
52  const dictionaryListEntry& dictEnt
53 )
54 :
55  dictionaryEntry(parentDict, dictEnt)
56 {}
57 
58 
59 Foam::dictionaryListEntry::dictionaryListEntry
60 (
61  const dictionary& parentDict,
62  Istream& is
63 )
64 :
66  (
67  word("entry" + Foam::name(realSize(parentDict))),
68  parentDict,
69  dictionary::null
70  )
71 {
72  token firstToken(is);
73  if (firstToken.isLabel())
74  {
75  const label sz = firstToken.labelToken();
76 
77  is.readBeginList("List");
78 
79  for (label i=0; i<sz; ++i)
80  {
81  entry::New(*this, is);
82  }
83  is.readEndList("List");
84  }
85  else if
86  (
87  firstToken.isPunctuation()
88  && firstToken.pToken() == token::BEGIN_LIST
89  )
90  {
91  while (true)
92  {
93  token nextToken(is);
94  if (nextToken.error())
95  {
97  << "parsing error " << nextToken.info()
98  << exit(FatalIOError);
99  }
100  else if
101  (
102  nextToken.isPunctuation()
103  && nextToken.pToken() == token::END_LIST
104  )
105  {
106  break;
107  }
108  is.putBack(nextToken);
109  entry::New(*this, is);
110  }
111  }
112  else
113  {
115  << "incorrect first token, expected <int> or '(', found "
116  << firstToken.info()
117  << exit(FatalIOError);
118  }
119 }
120 
121 
122 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
123 
125 {
126  os << nl << indent << size()
127  << token::SPACE << "// " << keyword() << nl
129 
130  // Write contents
131  dictionary::write(os, false);
132 
133  // Write end delimiter
134  os << decrIndent << indent << token::END_LIST << nl;
135 
136  os.check(FUNCTION_NAME);
137 }
138 
139 
140 // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
141 
143 {
144  e.write(os);
145  return os;
146 }
147 
148 
149 template<>
150 Foam::Ostream& Foam::operator<<
151 (
152  Ostream& os,
153  const InfoProxy<dictionaryListEntry>& ip
154 )
155 {
156  const dictionaryListEntry& e = ip.t_;
157 
158  os << " dictionaryListEntry '" << e.keyword() << "'" << endl;
159 
160  return os;
161 }
162 
163 
164 // ************************************************************************* //
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:487
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:481
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::entry::keyword
const keyType & keyword() const
Return keyword.
Definition: entry.H:187
Foam::Istream::readBeginList
char readBeginList(const char *funcName)
Begin read of list data, starts with '(' or '{'.
Definition: Istream.C:146
Foam::Istream::readEndList
char readEndList(const char *funcName)
End read of list data, ends with ')' or '}'.
Definition: Istream.C:167
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
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:327
realSize
static Foam::label realSize(const Foam::dictionary &dict)
Definition: dictionaryListEntryIO.C:34
Foam::token::pToken
punctuationToken pToken() const
Return punctuation character.
Definition: tokenI.H:459
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:551
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
dictionaryListEntry.H
Foam::token::isPunctuation
bool isPunctuation() const noexcept
Token is PUNCTUATION.
Definition: tokenI.H:453
Foam::dictionaryListEntry::write
virtual void write(Ostream &os) const
Write.
Definition: dictionaryListEntryIO.C:124
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::IOstream::check
virtual bool check(const char *operation) const
Check IOstream status for given operation.
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:121
Foam::token::error
bool error() const noexcept
Token is ERROR.
Definition: tokenI.H:411
Foam::decrIndent
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:334
Foam::indent
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:320
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:385
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 token.
Definition: Istream.C:53
Foam::token::SPACE
Space [isspace].
Definition: token.H:117
Foam::constant::electromagnetic::e
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::token::END_LIST
End list [isseparator].
Definition: token.H:123
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:401
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:122