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-2025 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& funcName,
69  const dictionary& parentDict,
70  Istream& is
71 )
72 {
73  return functionEntry::execute(funcName, parentDict, *this, is);
74 }
75 
76 
77 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
78 {
79  tokenIndex() = 0;
80 
81  if (read(dict, is))
82  {
83  setSize(tokenIndex());
84  tokenIndex() = 0;
85  }
86  else
87  {
88  std::ostringstream os;
89  os << "ill defined primitiveEntry starting at keyword '"
90  << keyword() << '\''
91  << " on line " << startLineNumber()
92  << " and ending at line " << is.lineNumber();
93 
95  (
96  is,
97  os.str()
98  );
99  }
100 }
101 
102 
103 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
104 
106 (
107  const keyType& key,
108  const dictionary& dict,
109  Istream& is
110 )
111 :
112  entry(key, is.lineNumber()),
113  ITstream
114  (
115  is.name() + '/' + key,
116  tokenList(10),
117  is.format(),
118  is.version(),
119  is.global()
120  )
121 {
122  readEntry(dict, is);
123 }
124 
125 
127 :
128  entry(key, is.lineNumber()),
129  ITstream
130  (
131  is.name() + '/' + key,
132  tokenList(10),
133  is.format(),
134  is.version(),
135  is.global()
136  )
137 {
138  readEntry(dictionary::null, is);
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143 
145 {
146  is.fatalCheck
147  (
148  "primitiveEntry::read(const dictionary&, Istream&) start"
149  );
150 
151  // Set the line number of the keyword
152  startLineNumber() = is.lineNumber();
153 
154  label blockCount = 0;
155  token currToken;
156 
157  if
158  (
159  !is.eof()
160  && !is.read(currToken).bad()
161  && currToken.good()
162  && currToken != token::END_STATEMENT
163  )
164  {
165  append(currToken, dict, is);
166 
167  if
168  (
169  currToken == token::BEGIN_BLOCK
170  || currToken == token::BEGIN_LIST
171  )
172  {
173  blockCount++;
174  }
175 
176  while
177  (
178  !is.eof()
179  && !is.read(currToken).bad()
180  && currToken.good()
181  && !(currToken == token::END_STATEMENT && blockCount == 0)
182  )
183  {
184  if
185  (
186  currToken == token::BEGIN_BLOCK
187  || currToken == token::BEGIN_LIST
188  )
189  {
190  blockCount++;
191  }
192  else if
193  (
194  currToken == token::END_BLOCK
195  || currToken == token::END_LIST
196  )
197  {
198  blockCount--;
199  }
200 
201  append(currToken, dict, is);
202  }
203  }
204 
205  is.fatalCheck
206  (
207  "primitiveEntry::read(const dictionary&, Istream&) end"
208  );
209 
210  if (currToken.good())
211  {
212  return true;
213  }
214  else
215  {
216  return false;
217  }
218 }
219 
220 
221 void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
222 {
223  if (!contentsOnly && keyword().size())
224  {
225  writeKeyword(os, keyword());
226  }
227 
228  for (label i=0; i<size(); ++i)
229  {
230  os << operator[](i);
231 
232  if (i < size()-1)
233  {
234  os << token::SPACE;
235  }
236  }
237 
238  if (!contentsOnly)
239  {
240  os << token::END_STATEMENT << endl;
241  }
242 }
243 
244 
246 {
247  this->write(os, false);
248 }
249 
250 
251 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
252 
253 template<>
254 Foam::Ostream& Foam::operator<<
255 (
256  Ostream& os,
257  const InfoProxy<primitiveEntry>& ip
258 )
259 {
260  const primitiveEntry& e = ip.t_;
261 
262  e.print(os);
263 
264  const label nPrintTokens = 10;
265 
266  os << " primitiveEntry '" << e.keyword() << "' comprises ";
267 
268  for (label i=0; i<min(e.size(), nPrintTokens); i++)
269  {
270  os << nl << " " << e[i].info();
271  }
272 
273  if (e.size() > nPrintTokens)
274  {
275  os << " ...";
276  }
277 
278  os << endl;
279 
280  return os;
281 }
282 
283 
284 // ************************************************************************* //
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:121
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Input token stream.
Definition: ITstream.H:56
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:181
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.
bool eof() const
Return true if end of input seen.
Definition: Istream.H:107
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 keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
static const dictionary null
Null dictionary.
Definition: dictionary.H:261
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
static int disableFunctionEntries
Definition: entry.H:102
virtual bool execute(dictionary &contextDict, Istream &is)=0
Expand the functionEntry into the contextDict.
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
const dictionary & dict() const
This entry is not a dictionary,.
primitiveEntry(const label keyLineNumber, const keyType &key)
Construct from line number and keyword.
A token holds items read from Istream.
Definition: token.H:74
@ BEGIN_BLOCK
Definition: token.H:114
@ END_BLOCK
Definition: token.H:115
@ END_STATEMENT
Definition: token.H:109
@ BEGIN_LIST
Definition: token.H:110
@ END_LIST
Definition: token.H:111
bool good() const
Definition: tokenI.H:309
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:361
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
const doubleScalar e
Definition: doubleScalar.H:106
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:288
dimensioned< Type > min(const DimensionedField< Type, GeoMesh, PrimitiveField > &df)
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
static const char nl
Definition: Ostream.H:297
word format(conversionProperties.lookup("format"))
points setSize(newPointi)
dictionary dict