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& 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 
78 void Foam::primitiveEntry::readEntry(const dictionary& dict, Istream& is)
79 {
80  tokenIndex() = 0;
81 
82  if (read(dict, is))
83  {
84  setSize(tokenIndex());
85  tokenIndex() = 0;
86  }
87  else
88  {
89  std::ostringstream os;
90  os << "ill defined primitiveEntry starting at keyword '"
91  << keyword() << '\''
92  << " on line " << startLineNumber()
93  << " and ending at line " << is.lineNumber();
94 
96  (
97  is,
98  os.str()
99  );
100  }
101 }
102 
103 
104 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
105 
107 (
108  const keyType& key,
109  const dictionary& dict,
110  Istream& is
111 )
112 :
113  entry(key, is.lineNumber()),
114  ITstream
115  (
116  is.name() + '/' + key,
117  tokenList(10),
118  is.format(),
119  is.version(),
120  is.global()
121  )
122 {
123  readEntry(dict, is);
124 }
125 
126 
128 :
129  entry(key, is.lineNumber()),
130  ITstream
131  (
132  is.name() + '/' + key,
133  tokenList(10),
134  is.format(),
135  is.version(),
136  is.global()
137  )
138 {
139  readEntry(dictionary::null, is);
140 }
141 
142 
143 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
144 
146 {
147  is.fatalCheck
148  (
149  "primitiveEntry::read(const dictionary&, Istream&) start"
150  );
151 
152  // Set the line number of the keyword
153  startLineNumber() = is.lineNumber();
154 
155  label blockCount = 0;
156  token currToken;
157 
158  if
159  (
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.read(currToken).bad()
179  && currToken.good()
180  && !(currToken == token::END_STATEMENT && blockCount == 0)
181  )
182  {
183  if
184  (
185  currToken == token::BEGIN_BLOCK
186  || currToken == token::BEGIN_LIST
187  )
188  {
189  blockCount++;
190  }
191  else if
192  (
193  currToken == token::END_BLOCK
194  || currToken == token::END_LIST
195  )
196  {
197  blockCount--;
198  }
199 
200  append(currToken, dict, is);
201  }
202  }
203 
204  is.fatalCheck
205  (
206  "primitiveEntry::read(const dictionary&, Istream&) end"
207  );
208 
209  if (currToken.good())
210  {
211  return true;
212  }
213  else
214  {
215  return false;
216  }
217 }
218 
219 
220 void Foam::primitiveEntry::write(Ostream& os, const bool contentsOnly) const
221 {
222  if (!contentsOnly && keyword().size())
223  {
224  writeKeyword(os, keyword());
225  }
226 
227  for (label i=0; i<size(); ++i)
228  {
229  os << operator[](i);;
230 
231  if (i < size()-1)
232  {
233  os << token::SPACE;
234  }
235  }
236 
237  if (!contentsOnly)
238  {
239  os << token::END_STATEMENT << endl;
240  }
241 }
242 
243 
245 {
246  this->write(os, false);
247 }
248 
249 
250 // * * * * * * * * * * * * * Ostream operator * * * * * * * * * * * * * * * //
251 
252 template<>
253 Foam::Ostream& Foam::operator<<
254 (
255  Ostream& os,
256  const InfoProxy<primitiveEntry>& ip
257 )
258 {
259  const primitiveEntry& e = ip.t_;
260 
261  e.print(os);
262 
263  const label nPrintTokens = 10;
264 
265  os << " primitiveEntry '" << e.keyword() << "' comprises ";
266 
267  for (label i=0; i<min(e.size(), nPrintTokens); i++)
268  {
269  os << nl << " " << e[i].info();
270  }
271 
272  if (e.size() > nPrintTokens)
273  {
274  os << " ...";
275  }
276 
277  os << endl;
278 
279  return os;
280 }
281 
282 
283 // ************************************************************************* //
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:105
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Input token stream.
Definition: ITstream.H:53
label tokenIndex() const
Return the current token index.
Definition: ITstream.H:142
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 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:273
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
static int disableFunctionEntries
Definition: entry.H:102
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:113
@ END_BLOCK
Definition: token.H:114
@ END_STATEMENT
Definition: token.H:108
@ BEGIN_LIST
Definition: token.H:109
@ END_LIST
Definition: token.H:110
bool good() const
Definition: tokenI.H:265
#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:258
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
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:267
word format(conversionProperties.lookup("format"))
points setSize(newPointi)
dictionary dict