primitiveEntryIO.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25  PrimitiveEntry constructor from Istream and Ostream output operator.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "primitiveEntry.H"
30 #include "functionEntry.H"
31 
32 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
33 
34 void Foam::primitiveEntry::append
35 (
36  const token& currToken,
37  const dictionary& dict,
38  Istream& is
39 )
40 {
41  if (currToken.isWord())
42  {
43  const word& w = currToken.wordToken();
44 
45  if
46  (
48  || w.size() == 1
49  || (
50  !(w[0] == '$' && expandVariable(w, dict))
51  && !(w[0] == '#' && expandFunction(w, dict, is))
52  )
53  )
54  {
55  newElmt(tokenIndex()++) = currToken;
56  }
57  }
58  else if (currToken.isVariable())
59  {
60  const string& w = currToken.stringToken();
61 
62  if
63  (
65  || w.size() <= 3
66  || !(
67  w[0] == '$'
68  && w[1] == token::BEGIN_BLOCK
69  && expandVariable(w, dict)
70  )
71  )
72  {
73  newElmt(tokenIndex()++) = currToken;
74  }
75  }
76  else
77  {
78  newElmt(tokenIndex()++) = currToken;
79  }
80 }
81 
82 
83 bool Foam::primitiveEntry::expandFunction
84 (
85  const word& keyword,
86  const dictionary& parentDict,
87  Istream& is
88 )
89 {
90  word functionName = keyword(1, keyword.size()-1);
91  return functionEntry::execute(functionName, parentDict, *this, is);
92 }
93 
94 
96 {
97  is.fatalCheck
98  (
99  "primitiveEntry::read(const dictionary&, Istream&) start"
100  );
101 
102  label blockCount = 0;
103  token currToken;
104 
105  if
106  (
107  !is.read(currToken).bad()
108  && currToken.good()
109  && currToken != token::END_STATEMENT
110  )
111  {
112  append(currToken, dict, is);
113 
114  if
115  (
116  currToken == token::BEGIN_BLOCK
117  || currToken == token::BEGIN_LIST
118  )
119  {
120  blockCount++;
121  }
122 
123  while
124  (
125  !is.read(currToken).bad()
126  && currToken.good()
127  && !(currToken == token::END_STATEMENT && blockCount == 0)
128  )
129  {
130  if
131  (
132  currToken == token::BEGIN_BLOCK
133  || currToken == token::BEGIN_LIST
134  )
135  {
136  blockCount++;
137  }
138  else if
139  (
140  currToken == token::END_BLOCK
141  || currToken == token::END_LIST
142  )
143  {
144  blockCount--;
145  }
146 
147  append(currToken, dict, is);
148  }
149  }
150 
151  is.fatalCheck
152  (
153  "primitiveEntry::read(const dictionary&, Istream&) end"
154  );
155 
156  if (currToken.good())
157  {
158  return true;
159  }
160  else
161  {
162  return false;
163  }
164 }
165 
166 
167 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
168 {
169  label keywordLineNumber = is.lineNumber();
170  tokenIndex() = 0;
171 
172  if (read(dict, is))
173  {
174  setSize(tokenIndex());
175  tokenIndex() = 0;
176  }
177  else
178  {
179  std::ostringstream os;
180  os << "ill defined primitiveEntry starting at keyword '"
181  << keyword() << '\''
182  << " on line " << keywordLineNumber
183  << " and ending at line " << is.lineNumber();
184 
186  (
187  is,
188  os.str()
189  );
190  }
191 }
192 
193 
194 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
195 
197 (
198  const keyType& key,
199  const dictionary& dict,
200  Istream& is
201 )
202 :
203  entry(key),
204  ITstream
205  (
206  is.name() + '.' + key,
207  tokenList(10),
208  is.format(),
209  is.version()
210  )
211 {
212  readEntry(dict, is);
213 }
214 
215 
217 :
218  entry(key),
219  ITstream
220  (
221  is.name() + '.' + key,
222  tokenList(10),
223  is.format(),
224  is.version()
225  )
226 {
227  readEntry(dictionary::null, is);
228 }
229 
230 
231 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
232 
233 void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
234 {
235  if (!contentsOnly)
236  {
237  os.writeKeyword(keyword());
238  }
239 
240  for (label i=0; i<size(); ++i)
241  {
242  const token& t = operator[](i);
243  if (t.type() == token::VERBATIMSTRING)
244  {
245  // Bypass token output operator to avoid losing verbatimness.
246  // Handle in Ostreams themselves
247  os.write(t);
248  }
249  else
250  {
251  os << t;
252  }
253 
254  if (i < size()-1)
255  {
256  os << token::SPACE;
257  }
258  }
259 
260  if (!contentsOnly)
261  {
262  os << token::END_STATEMENT << endl;
263  }
264 }
265 
266 
268 {
269  this->write(os, false);
270 }
271 
272 
273 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
274 
275 template<>
276 Foam::Ostream& Foam::operator<<
277 (
278  Ostream& os,
279  const InfoProxy<primitiveEntry>& ip
280 )
281 {
282  const primitiveEntry& e = ip.t_;
283 
284  e.print(os);
285 
286  const label nPrintTokens = 10;
287 
288  os << " primitiveEntry '" << e.keyword() << "' comprises ";
289 
290  for (label i=0; i<min(e.size(), nPrintTokens); i++)
291  {
292  os << nl << " " << e[i].info();
293  }
294 
295  if (e.size() > nPrintTokens)
296  {
297  os << " ...";
298  }
299 
300  os << endl;
301 
302  return os;
303 }
304 
305 
306 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
void write(Ostream &) const
Write.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:42
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
T & operator[](const label)
Return element of UList.
Definition: UListI.H:167
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:60
InfoProxy< primitiveEntry > info() const
Return info proxy.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:163
static const dictionary null
Null dictionary.
Definition: dictionary.H:202
A token holds items read from Istream.
Definition: token.H:69
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:151
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
Istream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Istream.H:76
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
entry(const keyType &)
Construct from keyword.
Definition: entry.C:40
static int disableFunctionEntries
Definition: entry.H:86
virtual Istream & read(token &)=0
Return next token from stream.
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:140
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: functionEntry.C:81
ITstream(const string &name, const UList< token > &tokens, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from components.
Definition: ITstream.H:69
bool good() const
Definition: tokenI.H:197
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
static const char nl
Definition: Ostream.H:265
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:54
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
void setSize(const label)
Reset size of List.
Definition: List.C:281
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
tokenType type() const
Definition: tokenI.H:187
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: ITstream.C:31
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
const fileName & name() const
Return the dictionary name.
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
virtual Ostream & write(const token &)=0
Write next token to stream.
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:98
label size() const
Return the number of elements in the UList.
Definition: ListI.H:170
Input token stream.
Definition: ITstream.H:49
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:346