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 inline Foam::label realSize(const Foam::dictionary& dict)
36 {
37  return
38  (
39  (dict.empty() || (dict.front()->keyword() != "FoamFile"))
40  ? dict.size()
41  : dict.size() - 1
42  );
43 }
44 
45 
46 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
47 
49 (
50  const dictionary& parentDict,
51  const dictionaryListEntry& dictEnt
52 )
53 :
54  dictionaryEntry(parentDict, dictEnt)
55 {}
56 
57 
59 (
60  const dictionary& parentDict,
61  Istream& is
62 )
63 :
65  (
66  word("entry" + Foam::name(realSize(parentDict))),
67  parentDict,
68  dictionary::null
69  )
70 {
71  token tok(is);
72  if (tok.isLabel())
73  {
74  const label len = tok.labelToken();
75 
76  is.readBeginList("List");
77 
78  for (label i=0; i<len; ++i)
79  {
80  entry::New(*this, is);
81  }
82  is.readEndList("List");
83  }
84  else if (tok.isPunctuation(token::BEGIN_LIST))
85  {
86  while (true)
87  {
88  is >> tok;
89  if (tok.error())
90  {
92  << "parsing error " << tok.info() << nl
93  << exit(FatalIOError);
94  }
95  else if (tok.isPunctuation(token::END_LIST))
96  {
97  break;
98  }
99  is.putBack(tok);
100  entry::New(*this, is);
101  }
102  }
103  else
104  {
106  << "incorrect first token, expected <int> or '(', found "
107  << tok.info() << nl
109  }
110 }
111 
112 
113 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
114 
115 void Foam::dictionaryListEntry::write(Ostream& os) const
116 {
117  os << nl << indent << size()
118  << token::SPACE << "// " << keyword() << nl
120 
121  // Write contents
122  dictionary::write(os, false);
123 
124  // Write end delimiter
125  os << decrIndent << indent << token::END_LIST << nl;
126 
128 }
129 
130 
131 // * * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * //
132 
133 Foam::Ostream& Foam::operator<<
134 (
135  Ostream& os,
136  const dictionaryListEntry& e
137 )
138 {
139  e.write(os);
140  return os;
141 }
142 
143 
144 template<>
145 Foam::Ostream& Foam::operator<<
146 (
147  Ostream& os,
148  const InfoProxy<dictionaryListEntry>& iproxy
149 )
150 {
151  const auto& e = *iproxy;
152 
153  os << " dictionaryListEntry '" << e.keyword() << "'" << endl;
154 
155  return os;
156 }
157 
158 
159 // ************************************************************************* //
dictionary dict
Ostream & indent(Ostream &os)
Indent stream.
Definition: Ostream.H:493
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:129
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:192
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
A token holds an item read from Istream.
Definition: token.H:65
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
Begin list [isseparator].
Definition: token.H:161
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 (copy). Only a single put back is permitted.
Definition: Istream.C:71
const dimensionedScalar e
Elementary charge.
Definition: createFields.H:11
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for expressions::valueTypeCode::INVALID.
Definition: exprTraits.C:127
A class for handling words, derived from Foam::string.
Definition: word.H:63
Space [isspace].
Definition: token.H:131
End list [isseparator].
Definition: token.H:162
char readBeginList(const char *funcName)
Begin read of list data, starts with &#39;(&#39; or &#39;{&#39;.
Definition: Istream.C:171
Read/write List of dictionaries.
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
Ostream & decrIndent(Ostream &os)
Decrement the indent level.
Definition: Ostream.H:511
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:204
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
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:502
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 ...