primitiveEntryIO.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 | Copyright (C) 2011-2015 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 
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  (
47  disableFunctionEntries
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  (
64  disableFunctionEntries
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 
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::readData(const dictionary&, Istream&)"
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::readData(const dictionary&, Istream&)"
154  );
155 
156  if (currToken.good())
157  {
158  return true;
159  }
160  else
161  {
162  return false;
163  }
164 }
165 
166 
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 {
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::IOstream::format
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
setSize
points setSize(newPointi)
Foam::token::END_STATEMENT
@ END_STATEMENT
Definition: token.H:99
format
word format(conversionProperties.lookup("format"))
Foam::IOstream::fatalCheck
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
w
volScalarField w(IOobject("w", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::NO_WRITE), mesh, dimensionedScalar("w", dimensionSet(0, 0, 0, 0, 0, 0, 0), 0.0))
Foam::primitiveEntry
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
Definition: primitiveEntry.H:62
primitiveEntry.H
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::InfoProxy
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
Foam::primitiveEntry::append
void append(const UList< token > &)
Append the given tokens starting at the current tokenIndex.
Definition: primitiveEntry.C:33
Foam::primitiveEntry::read
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
Definition: primitiveEntryIO.C:95
Foam::read
bool read(const char *, int32_t &)
Definition: int32IO.C:87
Foam::token::wordToken
const word & wordToken() const
Definition: tokenI.H:226
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::token
A token holds items read from Istream.
Definition: token.H:67
functionEntry.H
Foam::token::good
bool good() const
Definition: tokenI.H:188
Foam::token::stringToken
const string & stringToken() const
Definition: tokenI.H:249
Foam::primitiveEntry::expandFunction
bool expandFunction(const word &, const dictionary &, Istream &)
Expand the given function (keyword starts with #)
Definition: primitiveEntryIO.C:84
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:56
Foam::token::type
tokenType type() const
Definition: tokenI.H:178
Foam::primitiveEntry::readEntry
void readEntry(const dictionary &, Istream &)
Read the complete entry from the given stream.
Definition: primitiveEntryIO.C:167
Foam::label
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
Foam::ITstream
Input token stream.
Definition: ITstream.H:49
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Ostream::write
virtual Ostream & write(const token &)=0
Write next token to stream.
Foam::nl
static const char nl
Definition: Ostream.H:260
Foam::primitiveEntry::primitiveEntry
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
Definition: primitiveEntryIO.C:216
Foam::token::isVariable
bool isVariable() const
Definition: tokenI.H:239
dict
dictionary dict
Definition: searchingEngine.H:14
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:345
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::token::isWord
bool isWord() const
Definition: tokenI.H:221
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Foam::e
const double e
Elementary charge.
Definition: doubleFloat.H:94
Foam::token::BEGIN_LIST
@ BEGIN_LIST
Definition: token.H:100
Foam::IOstream::version
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
Foam::token::END_BLOCK
@ END_BLOCK
Definition: token.H:105
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Foam::Ostream::writeKeyword
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
Foam::primitiveEntry::write
void write(Ostream &) const
Write.
Definition: primitiveEntryIO.C:267
Foam::primitiveEntry::dict
const dictionary & dict() const
This entry is not a dictionary,.
Definition: primitiveEntry.C:187
Foam::IOstream::lineNumber
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
Foam::token::BEGIN_BLOCK
@ BEGIN_BLOCK
Definition: token.H:104
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
Foam::min
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
Foam::token::VERBATIMSTRING
@ VERBATIMSTRING
Definition: token.H:81
write
Tcoeff write()
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::null
static const dictionary null
Null dictionary.
Definition: dictionary.H:193
Foam::token::END_LIST
@ END_LIST
Definition: token.H:101
Foam::token::SPACE
@ SPACE
Definition: token.H:95
Foam::Istream::read
virtual Istream & read(token &)=0
Return next token from stream.