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 
51 (
52  const dictionary& parentDict,
53  const dictionaryListEntry& dictEnt
54 )
55 :
56  dictionaryEntry(parentDict, dictEnt)
57 {}
58 
59 
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<<
136 (
137  Ostream& os,
138  const dictionaryListEntry& e
139 )
140 {
141  e.write(os);
142  return os;
143 }
144 
145 
146 template<>
147 Foam::Ostream& Foam::operator<<
148 (
149  Ostream& os,
150  const InfoProxy<dictionaryListEntry>& iproxy
151 )
152 {
153  const auto& e = *iproxy;
154 
155  os << " dictionaryListEntry '" << e.keyword() << "'" << endl;
156 
157  return os;
158 }
159 
160 
161 // ************************************************************************* //
dictionary dict
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:449
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:45
char readEndList(const char *funcName)
End read of list data, ends with &#39;)&#39; or &#39;}&#39;.
Definition: Istream.C:162
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:49
A token holds an item read from Istream.
Definition: token.H:64
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
Begin list [isseparator].
Definition: token.H:158
dictionaryListEntry(const dictionaryListEntry &)=delete
No copy construct.
A keyword and a list of tokens is a &#39;dictionaryEntry&#39;.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
void putBack(const token &tok)
Put back a token. Only a single put back is permitted.
Definition: Istream.C:63
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
A class for handling words, derived from Foam::string.
Definition: word.H:63
Space [isspace].
Definition: token.H:128
End list [isseparator].
Definition: token.H:159
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:141
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:55
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:467
static Foam::label realSize(const Foam::dictionary &dict)
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:199
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
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
Ostream & incrIndent(Ostream &os)
Increment the indent level.
Definition: Ostream.H:458
Namespace for OpenFOAM.
virtual void write(Ostream &os) const
Write.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...