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 // ************************************************************************* //
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:348
Input token stream.
Definition: ITstream.H:53
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:140
A helper class for outputting values to Ostream.
Definition: InfoProxy.H:50
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
virtual Istream & read(token &)=0
Return next token from stream.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: List.H:91
T & newElmt(const label)
Return subscript-checked element of UList.
Definition: ListI.H:152
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
static const dictionary null
Null dictionary.
Definition: dictionary.H:242
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
static int disableFunctionEntries
Definition: entry.H:86
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
A class for handling keywords in dictionaries.
Definition: keyType.H:69
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
void write(Ostream &) const
Write.
virtual Istream & read(token &)
Inherit read from ITstream.
Definition: ITstream.C:56
primitiveEntry(const keyType &, Istream &)
Construct from keyword and a Istream.
const dictionary & dict() const
This entry is not a dictionary,.
A token holds items read from Istream.
Definition: token.H:73
@ BEGIN_BLOCK
Definition: token.H:110
@ END_BLOCK
Definition: token.H:111
@ END_STATEMENT
Definition: token.H:105
@ BEGIN_LIST
Definition: token.H:106
@ END_LIST
Definition: token.H:107
bool good() const
Definition: tokenI.H:228
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:333
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
const doubleScalar e
Definition: doubleScalar.H:105
bool read(const char *, int32_t &)
Definition: int32IO.C:85
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static const char nl
Definition: Ostream.H:260
word format(conversionProperties.lookup("format"))
points setSize(newPointi)
dictionary dict