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-2019 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 {
42  {
43  newElmt(tokenIndex()++) = currToken;
44  }
45  else if (currToken.isFunctionName())
46  {
47  if (!expandFunction(currToken.functionNameToken(), dict, is))
48  {
49  newElmt(tokenIndex()++) = currToken;
50  }
51  }
52  else if (currToken.isVariable())
53  {
54  if (!expandVariable(currToken.variableToken(), dict))
55  {
56  newElmt(tokenIndex()++) = currToken;
57  }
58  }
59  else
60  {
61  newElmt(tokenIndex()++) = currToken;
62  }
63 }
64 
65 
66 bool Foam::primitiveEntry::expandFunction
67 (
68  const functionName& hashFn,
69  const dictionary& parentDict,
70  Istream& is
71 )
72 {
73  const word fn = hashFn(1, hashFn.size() - 1);
74  return functionEntry::execute(fn, parentDict, *this, is);
75 }
76 
77 
79 {
80  is.fatalCheck
81  (
82  "primitiveEntry::read(const dictionary&, Istream&) start"
83  );
84 
85  label blockCount = 0;
86  token currToken;
87 
88  if
89  (
90  !is.read(currToken).bad()
91  && currToken.good()
92  && currToken != token::END_STATEMENT
93  )
94  {
95  append(currToken, dict, is);
96 
97  if
98  (
99  currToken == token::BEGIN_BLOCK
100  || currToken == token::BEGIN_LIST
101  )
102  {
103  blockCount++;
104  }
105 
106  while
107  (
108  !is.read(currToken).bad()
109  && currToken.good()
110  && !(currToken == token::END_STATEMENT && blockCount == 0)
111  )
112  {
113  if
114  (
115  currToken == token::BEGIN_BLOCK
116  || currToken == token::BEGIN_LIST
117  )
118  {
119  blockCount++;
120  }
121  else if
122  (
123  currToken == token::END_BLOCK
124  || currToken == token::END_LIST
125  )
126  {
127  blockCount--;
128  }
129 
130  append(currToken, dict, is);
131  }
132  }
133 
134  is.fatalCheck
135  (
136  "primitiveEntry::read(const dictionary&, Istream&) end"
137  );
138 
139  if (currToken.good())
140  {
141  return true;
142  }
143  else
144  {
145  return false;
146  }
147 }
148 
149 
150 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
151 {
152  label keywordLineNumber = is.lineNumber();
153  tokenIndex() = 0;
154 
155  if (read(dict, is))
156  {
157  setSize(tokenIndex());
158  tokenIndex() = 0;
159  }
160  else
161  {
162  std::ostringstream os;
163  os << "ill defined primitiveEntry starting at keyword '"
164  << keyword() << '\''
165  << " on line " << keywordLineNumber
166  << " and ending at line " << is.lineNumber();
167 
169  (
170  is,
171  os.str()
172  );
173  }
174 }
175 
176 
177 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
178 
180 (
181  const keyType& key,
182  const dictionary& dict,
183  Istream& is
184 )
185 :
186  entry(key),
187  ITstream
188  (
189  is.name() + '/' + key,
190  tokenList(10),
191  is.format(),
192  is.version()
193  )
194 {
195  readEntry(dict, is);
196 }
197 
198 
200 :
201  entry(key),
202  ITstream
203  (
204  is.name() + '/' + key,
205  tokenList(10),
206  is.format(),
207  is.version()
208  )
209 {
210  readEntry(dictionary::null, is);
211 }
212 
213 
214 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
215 
216 void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
217 {
218  if (!contentsOnly && keyword().size())
219  {
220  writeKeyword(os, keyword());
221  }
222 
223  for (label i=0; i<size(); ++i)
224  {
225  os << operator[](i);;
226 
227  if (i < size()-1)
228  {
229  os << token::SPACE;
230  }
231  }
232 
233  if (!contentsOnly)
234  {
235  os << token::END_STATEMENT << endl;
236  }
237 }
238 
239 
241 {
242  this->write(os, false);
243 }
244 
245 
246 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
247 
248 template<>
249 Foam::Ostream& Foam::operator<<
250 (
251  Ostream& os,
252  const InfoProxy<primitiveEntry>& ip
253 )
254 {
255  const primitiveEntry& e = ip.t_;
256 
257  e.print(os);
258 
259  const label nPrintTokens = 10;
260 
261  os << " primitiveEntry '" << e.keyword() << "' comprises ";
262 
263  for (label i=0; i<min(e.size(), nPrintTokens); i++)
264  {
265  os << nl << " " << e[i].info();
266  }
267 
268  if (e.size() > nPrintTokens)
269  {
270  os << " ...";
271  }
272 
273  os << endl;
274 
275  return os;
276 }
277 
278 
279 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
void write(Ostream &) const
Write.
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
const keyType & keyword() const
Return keyword.
Definition: entry.H:123
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
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:156
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:59
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:164
A token holds items read from Istream.
Definition: token.H:72
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:152
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
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:396
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
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:294
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.
ITstream(const string &name, const UList< token > &tokens, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from components.
Definition: ITstream.H:69
static const dictionary null
Null dictionary.
Definition: dictionary.H:242
bool good() const
Definition: tokenI.H:228
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
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:54
static const char nl
Definition: Ostream.H:260
const dictionary & dict() const
This entry is not a dictionary,.
void setSize(const label)
Reset size of List.
Definition: List.C:281
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:45
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.
const doubleScalar e
Elementary charge.
Definition: doubleScalar.H:105
label size() const
Return the number of elements in the UList.
Definition: ListI.H:171
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:333