functionEntry.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-2021 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 \*---------------------------------------------------------------------------*/
25 
26 #include "functionEntry.H"
27 #include "IOstreams.H"
28 #include "ISstream.H"
29 #include "Pstream.H"
30 
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
32 
33 namespace Foam
34 {
36  (
37  functionEntry,
38  execute,
39  dictionaryIstream
40  );
41 
43  (
44  functionEntry,
45  execute,
46  primitiveEntryIstream
47  );
48 }
49 
50 
51 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
52 
53 Foam::token Foam::functionEntry::readLine(Istream& is)
54 {
55  if (isA<Pstream>(is))
56  {
57  return token(is);
58  }
59  else
60  {
61  return token(word(readFuncNameArgs(is)), is.lineNumber());
62  }
63 }
64 
65 
66 // * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * //
67 
69 {
70  string fNameArgs;
71 
72  // Read the function name with arguments if on the same line
73  const token fName(is);
74 
75  if (fName.isWord())
76  {
77  const word& fNameWord = fName.wordToken();
78 
79  if (fNameWord.find(token::BEGIN_LIST) != string::npos)
80  {
81  // If the function name includes a '(' push it back onto the stream
82  // and re-read as a list
83 
84  ISstream& iss = dynamic_cast<ISstream&>(is);
85 
86  for
87  (
88  string::const_reverse_iterator rit = fNameWord.rbegin();
89  rit != fNameWord.rend();
90  ++rit
91  )
92  {
93  iss.putback(*rit);
94  }
95 
96  iss.readList(fNameArgs);
97  }
98  else
99  {
100  // Read the next token to check for '('
101  // in case the optional arguments start on the next line
102 
103  const token nextToken(is);
104 
105  if
106  (
107  nextToken.isPunctuation()
108  && nextToken.pToken() == token::BEGIN_LIST
109  )
110  {
111  ISstream& iss = dynamic_cast<ISstream&>(is);
112 
114  iss.readList(fNameArgs);
115  fNameArgs = fNameWord + fNameArgs;
116  }
117  else
118  {
119  is.putBack(nextToken);
120  fNameArgs = fNameWord;
121  }
122  }
123  }
124  else if (fName.isString())
125  {
126  // If the function name is a string delimit with '"'s
127  fNameArgs = '"' + fName.stringToken() + '"';
128  }
129  else
130  {
131  // For any other kind of string return for error reporting
132  fNameArgs = fName.anyStringToken();
133  }
134 
135  return fNameArgs;
136 }
137 
138 
140 (
141  dictionary& parentDict,
142  const string& str
143 )
144 {
145  parentDict.read(IStringStream(str)());
146  return true;
147 }
148 
149 
151 (
152  const dictionary& parentDict,
153  primitiveEntry& thisEntry,
154  const string& str
155 )
156 {
157  thisEntry.read(parentDict, IStringStream(str)());
158  return true;
159 }
160 
161 
162 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
163 
165 (
166  const word& key,
167  const dictionary& dict,
168  Istream& is
169 )
170 :
171  primitiveEntry(key, readLine(is))
172 {}
173 
174 
175 // * * * * * * * * * * * * Member Function Selectors * * * * * * * * * * * * //
176 
178 (
179  const word& functionName,
180  dictionary& parentDict,
181  Istream& is
182 )
183 {
184  is.fatalCheck
185  (
186  "functionEntry::execute"
187  "(const word& functionName, dictionary& parentDict, Istream&)"
188  );
189 
190  if (!executedictionaryIstreamMemberFunctionTablePtr_)
191  {
192  cerr<< "functionEntry::execute"
193  << "(const word&, dictionary&, Istream&)"
194  << " not yet initialised, function = "
195  << functionName.c_str() << std::endl;
196 
197  // Return true to keep reading
198  return true;
199  }
200 
201  executedictionaryIstreamMemberFunctionTable::iterator mfIter =
202  executedictionaryIstreamMemberFunctionTablePtr_->find(functionName);
203 
204  if (mfIter == executedictionaryIstreamMemberFunctionTablePtr_->end())
205  {
207  << "Unknown functionEntry '" << functionName
208  << "' in " << is.name() << " near line " << is.lineNumber()
209  << nl << nl
210  << "Valid functionEntries are :" << endl
211  << executedictionaryIstreamMemberFunctionTablePtr_->toc()
212  << exit(FatalError);
213  }
214 
215  return mfIter()(parentDict, is);
216 }
217 
218 
220 (
221  const word& functionName,
222  const dictionary& parentDict,
223  primitiveEntry& entry,
224  Istream& is
225 )
226 {
227  is.fatalCheck
228  (
229  "functionEntry::execute"
230  "(const word&, const dictionary&, primitiveEntry&, Istream&)"
231  );
232 
233  if (!executeprimitiveEntryIstreamMemberFunctionTablePtr_)
234  {
235  cerr<< "functionEntry::execute"
236  << "(const word&, const dictionary&, primitiveEntry&, Istream&)"
237  << " not yet initialised, function = "
238  << functionName.c_str() << std::endl;
239 
240  // return true to keep reading anyhow
241  return true;
242  }
243 
244  executeprimitiveEntryIstreamMemberFunctionTable::iterator mfIter =
245  executeprimitiveEntryIstreamMemberFunctionTablePtr_->find(functionName);
246 
247  if (mfIter == executeprimitiveEntryIstreamMemberFunctionTablePtr_->end())
248  {
250  << "Unknown functionEntry '" << functionName
251  << "' in " << is.name() << " near line " << is.lineNumber()
252  << nl << nl
253  << "Valid functionEntries are :" << endl
254  << executeprimitiveEntryIstreamMemberFunctionTablePtr_->toc()
255  << exit(FatalError);
256  }
257 
258  return mfIter()(parentDict, entry, is);
259 }
260 
261 
263 {
264  writeKeyword(os, keyword());
265 
266  if (size() == 1)
267  {
268  os << operator[](0) << endl;
269  }
270  else
271  {
273  << "Incorrect number of tokens in functionEntry, "
274  "should be a single word."
275  << exit(FatalIOError);
276  }
277 }
278 
279 
280 // ************************************************************************* //
Istream & readList(string &)
Read a &#39;(...)&#39; delimited set of characters into a string.
Definition: ISstream.C:758
functionEntry(const word &, const dictionary &, Istream &)
Construct from keyword, parent dictionary and Istream.
bool isWord() const
Definition: tokenI.H:261
punctuationToken pToken() const
Definition: tokenI.H:248
Ostream & writeKeyword(Foam::Ostream &os, const keyType &kw)
Write the keyword to the Ostream with the current level of indentation.
Definition: keyType.C:155
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
static bool insert(dictionary &parentDict, const string &)
Insert the given string in a sub-dict context.
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
const word & wordToken() const
Definition: tokenI.H:266
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A token holds items read from Istream.
Definition: token.H:72
void putBack(const token &)
Put back token.
Definition: Istream.C:30
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:435
bool read(Istream &, const bool keepHeader=false)
Read dictionary from Istream, optionally keeping the header.
Definition: dictionaryIO.C:104
virtual void write(Ostream &) const
Write.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
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.
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
static bool execute(const word &functionName, dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
void fatalCheck(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:105
const string & anyStringToken() const
Definition: tokenI.H:363
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 string & stringToken() const
Definition: tokenI.H:320
bool isString() const
Definition: tokenI.H:315
ISstream & putback(const char &)
Low-level putback character function.
Definition: ISstreamI.H:78
Generic input stream.
Definition: ISstream.H:52
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
static string readFuncNameArgs(Istream &is)
Read the function name and optional argument list.
Definition: functionEntry.C:68
Input from memory buffer stream.
Definition: IStringStream.H:49
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
defineMemberFunctionSelectionTable(edgeMesh, write, fileExtension)
A class for handling character strings derived from std::string.
Definition: string.H:76
bool isPunctuation() const
Definition: tokenI.H:243
Namespace for OpenFOAM.
IOerror FatalIOError