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-2012 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::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 
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  "primitiveEntry::readEntry(const dictionary&, Istream&)",
188  is,
189  os.str()
190  );
191  }
192 }
193 
194 
195 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
196 
198 (
199  const keyType& key,
200  const dictionary& dict,
201  Istream& is
202 )
203 :
204  entry(key),
205  ITstream
206  (
207  is.name() + '.' + key,
208  tokenList(10),
209  is.format(),
210  is.version()
211  )
212 {
213  readEntry(dict, is);
214 }
215 
216 
218 :
219  entry(key),
220  ITstream
221  (
222  is.name() + '.' + key,
223  tokenList(10),
224  is.format(),
225  is.version()
226  )
227 {
228  readEntry(dictionary::null, is);
229 }
230 
231 
232 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
233 
234 void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
235 {
236  if (!contentsOnly)
237  {
238  os.writeKeyword(keyword());
239  }
240 
241  for (label i=0; i<size(); ++i)
242  {
243  const token& t = operator[](i);
244  if (t.type() == token::VERBATIMSTRING)
245  {
246  // Bypass token output operator to avoid losing verbatimness.
247  // Handle in Ostreams themselves
248  os.write(t);
249  }
250  else
251  {
252  os << t;
253  }
254 
255  if (i < size()-1)
256  {
257  os << token::SPACE;
258  }
259  }
260 
261  if (!contentsOnly)
262  {
263  os << token::END_STATEMENT << endl;
264  }
265 }
266 
267 
269 {
270  this->write(os, false);
271 }
272 
273 
274 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
275 
276 template<>
277 Foam::Ostream& Foam::operator<<
278 (
279  Ostream& os,
280  const InfoProxy<primitiveEntry>& ip
281 )
282 {
283  const primitiveEntry& e = ip.t_;
284 
285  e.print(os);
286 
287  const label nPrintTokens = 10;
288 
289  os << " primitiveEntry '" << e.keyword() << "' comprises ";
290 
291  for (label i=0; i<min(e.size(), nPrintTokens); i++)
292  {
293  os << nl << " " << e[i].info();
294  }
295 
296  if (e.size() > nPrintTokens)
297  {
298  os << " ...";
299  }
300 
301  os << endl;
302 
303  return os;
304 }
305 
306 
307 // ************************************************************************* //
Input token stream.
Definition: ITstream.H:49
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
ITstream(const string &name, const UList< token > &tokens, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from components.
Definition: ITstream.H:69
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
virtual Istream & read(token &)=0
Return next token from stream.
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:64
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
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: functionEntry.C:51
void size(const label)
Override size to be inconsistent with allocated storage.
Definition: ListI.H:76
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
const fileName & name() const
Return the dictionary name.
A class for handling keywords in dictionaries.
Definition: keyType.H:56
const keyType & keyword() const
Return keyword.
Definition: entry.H:120
static const dictionary null
Null dictionary.
Definition: dictionary.H:193
void write(Ostream &) const
Write.
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
T & operator[](const label)
Return element of UList.
Definition: UListI.H:163
tokenType type() const
Definition: tokenI.H:178
static const char nl
Definition: Ostream.H:260
const double e
Elementary charge.
Definition: doubleFloat.H:78
void setSize(const label)
Reset size of List.
Definition: List.C:318
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:107
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:338
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual Ostream & write(const token &)=0
Write next token to stream.
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
A token holds items read from Istream.
Definition: token.H:67
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:140
Ostream & writeKeyword(const keyType &)
Write the keyword followed by an appropriate indentation.
Definition: Ostream.C:59
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
Istream(streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Set stream status.
Definition: Istream.H:76
void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: ITstream.C:31
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
static int disableFunctionEntries
Definition: entry.H:83
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
label size() const
Return the number of elements in the UList.
Definition: ListI.H:83
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
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.
List< token > tokenList
List of tokens, used for a IOdictionary entry.
Definition: tokenList.H:43
InfoProxy< primitiveEntry > info() const
Return info proxy.
bool good() const
Definition: tokenI.H:188
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
entry(const keyType &)
Construct from keyword.
Definition: entry.C:40