includeEntry.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-2026 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 "includeEntry.H"
27 #include "includeIfPresentEntry.H"
28 #include "stringOps.H"
29 #include "IOobject.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
36 
37 namespace Foam
38 {
39 namespace functionEntries
40 {
43 
45  (
48  execute,
49  primitiveEntryIstream
50  );
51 }
52 }
53 
54 
55 // * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * * //
56 
58 Foam::functionEntries::includeEntry::insertNamedArgs
59 (
60  dictionary& contextDict,
61  const Tuple2<string, label>& fNameArgs
62 ) const
63 {
65 
66  // If the next character is a '(' process the arguments
67  if (fNameArgs.first().size())
68  {
69  // Parse the argument string
70  word funcType;
72  dictArgList(fNameArgs, funcType, args, namedArgs);
73 
74  // Add the named arguments as entries into the contextDict
75  // temporarily renaming any existing entries with the same name
76  forAll(namedArgs, i)
77  {
78  const Pair<word> dAk(dictAndKeyword(namedArgs[i].first()));
79  dictionary& subDict(contextDict.scopedDict(dAk.first()));
80 
81  // Rename the original entry adding a '_'
82  if (subDict.found(dAk.second()))
83  {
84  keyType tmpName(dAk.second());
85  tmpName += '_';
86  subDict.changeKeyword(dAk.second(), tmpName);
87  }
88 
89  // Add the temporary argument entry
90  IStringStream entryStream
91  (
92  dAk.second()
93  + ' '
94  + expandArg
95  (
96  namedArgs[i].second(),
97  contextDict,
98  namedArgs[i].third()
99  )
100  + ';'
101  );
102  subDict.set(entry::New(entryStream).ptr());
103  }
104  }
105 
106  return namedArgs;
107 }
108 
109 
110 void Foam::functionEntries::includeEntry::removeInsertNamedArgs
111 (
112  dictionary& contextDict,
113  const List<Tuple3<word, string, label>>& namedArgs
114 ) const
115 {
116  forAll(namedArgs, i)
117  {
118  // Remove the temporary argument entry
119  contextDict.remove(namedArgs[i].first());
120 
121  // Reinstate the original entry
122  const Pair<word> dAk(dictAndKeyword(namedArgs[i].first()));
123  dictionary& subDict(contextDict.scopedDict(dAk.first()));
124  keyType tmpName(dAk.second());
125  tmpName += '_';
126  subDict.changeKeyword(tmpName, dAk.second());
127  }
128 }
129 
130 
131 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
132 
134 (
135  const fileName& dir,
136  const fileName& f,
137  const dictionary& dict
138 ) const
139 {
140  fileName fName(f);
141 
142  // Substitute dictionary and environment variables. Allow empty
143  // substitutions.
144  stringOps::inplaceExpandEntry(fName, dict, true, true);
145 
146  if (fName.empty() || fName.isAbsolute())
147  {
148  return fName;
149  }
150  else
151  {
152  // relative name
153  return dir/fName;
154  }
155 }
156 
157 
159 (
160  const dictionary& contextDict,
161  primitiveEntry& contextEntry,
162  Istream& is
163 )
164 {
165  const fileName fName
166  (
167  includeFileName(is.name().path(), this->fName(), contextDict)
168  );
169 
170  if (!fileHandler().exists(fName))
171  {
172  if (includeIfPresent())
173  {
174  return true;
175  }
176  else
177  {
179  (
180  is
181  ) << "Cannot find include file " << fName
182  << " while reading dictionary " << contextDict.name()
183  << exit(FatalIOError);
184  }
185  }
186 
187  // Cache the optional named arguments
188  // temporarily inserted into contextDict
190  (
191  insertNamedArgs(const_cast<dictionary&>(contextDict), args())
192  );
193 
194  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
195  ISstream& ifs = ifsPtr();
196 
197  if (ifs)
198  {
200  {
201  Info<< fName << endl;
202  }
203 
204  contextEntry.read(contextDict, ifs);
205  }
206  else
207  {
209  (
210  is
211  ) << "Cannot open include file "
212  << (ifs.name().size() ? ifs.name() : this->fName())
213  << " while reading dictionary " << contextDict.name()
214  << exit(FatalIOError);
215  }
216 
217  // Remove named argument entries from contextDict
218  // renaming any existing entries which had the same name
219  removeInsertNamedArgs(const_cast<dictionary&>(contextDict), namedArgs);
220 
221  return true;
222 }
223 
224 
225 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
226 
228 (
229  const functionName& functionType,
230  const label lineNumber,
231  const dictionary& parentDict,
232  Istream& is
233 )
234 :
236  (
237  functionType,
238  lineNumber,
239  parentDict,
240  is,
241  readFileNameArgList(functionType, is)
242  )
243 {}
244 
245 
247 (
248  const label lineNumber,
249  const dictionary& parentDict,
250  Istream& is
251 )
252 :
253  includeEntry(typeName, lineNumber, parentDict, is)
254 {}
255 
256 
257 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
258 
260 (
261  dictionary& contextDict,
262  Istream& is
263 )
264 {
265  const fileName fName
266  (
267  includeFileName(is.name().path(), this->fName(), contextDict)
268  );
269 
270  if (!fileHandler().exists(fName))
271  {
272  if (includeIfPresent())
273  {
274  return true;
275  }
276  else
277  {
279  (
280  is
281  ) << "Cannot find include file " << fName
282  << " while reading dictionary " << contextDict.name()
283  << exit(FatalIOError);
284  }
285  }
286 
287  // Cache the optional named arguments
288  // temporarily inserted into contextDict
290  (
291  insertNamedArgs(contextDict, args())
292  );
293 
294  autoPtr<ISstream> ifsPtr
295  (
296  fileHandler().NewIFstream(fName, is.format(), is.version())
297  );
298  ISstream& ifs = ifsPtr();
299 
300  if (ifs)
301  {
303  {
304  Info<< fName << endl;
305  }
306 
307  // Cache the FoamFile entry if present
308  dictionary foamFileDict;
309  if (contextDict.found(IOobject::foamFile))
310  {
311  foamFileDict = contextDict.subDict(IOobject::foamFile);
312  }
313 
314  // Read and clear the FoamFile entry
315  contextDict.read(ifs);
316 
317  // Reinstate original FoamFile entry
318  if (foamFileDict.size() != 0)
319  {
320  dictionary contextDictTmp(contextDict);
321  contextDict.clear();
322  contextDict.add(IOobject::foamFile, foamFileDict);
323  contextDict += contextDictTmp;
324  }
325  }
326  else
327  {
329  (
330  is
331  ) << "Cannot open include file "
332  << (ifs.name().size() ? ifs.name() : this->fName())
333  << " while reading dictionary " << contextDict.name()
334  << exit(FatalIOError);
335  }
336 
337  // Remove named argument entries from contextDict
338  // renaming any existing entries which had the same name
339  removeInsertNamedArgs(contextDict, namedArgs);
340 
341  return true;
342 }
343 
344 
346 (
347  const dictionary& contextDict,
348  primitiveEntry& contextEntry,
349  Istream& is
350 )
351 {
352  return
353  includeEntry(is.lineNumber(), contextDict, is)
354  .virtualExecute(contextDict, contextEntry, is);
355 }
356 
357 
358 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:449
Macros for easy insertion into member function selection tables.
Macros for easy insertion into run-time selection tables.
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
streamFormat format() const
Return current stream format.
Definition: IOstream.H:377
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
versionNumber version() const
Return the stream version.
Definition: IOstream.H:399
Generic input stream.
Definition: ISstream.H:55
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:121
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:60
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 & first()
Return the first element of the list.
Definition: UListI.H:114
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:111
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool read(Istream &, const bool keepHeader=false)
Read dictionary from Istream, optionally keeping the header.
Definition: dictionaryIO.C:105
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:778
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1019
void clear()
Clear the dictionary.
Definition: dictionary.C:1358
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:468
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:105
A class for handling file names.
Definition: fileName.H:82
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:73
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:284
Specify an include file when reading dictionaries.
Definition: includeEntry.H:78
virtual bool virtualExecute(const dictionary &contextDict, primitiveEntry &contextEntry, Istream &)
Expand the functionEntry into the contextEntry.
Definition: includeEntry.C:159
includeEntry(const functionName &functionType, const label lineNumber, const dictionary &parentDict, Istream &is)
Construct from functionType, dictionary and Istream.
Definition: includeEntry.C:228
virtual fileName includeFileName(const fileName &dir, const fileName &, const dictionary &) const
Expand include fileName and return.
Definition: includeEntry.C:134
Tuple2< string, label > args() const
Return the arguments string for processing by insertNamedArgs.
Definition: includeEntry.H:110
virtual bool execute(dictionary &contextDict, Istream &)
Expand the functionEntry into the contextDict.
Definition: includeEntry.C:260
static bool log
Report which file is included to stdout.
Definition: includeEntry.H:156
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A functionName is a word starting with '#'.
Definition: functionName.H:60
Motion of the mesh specified as a list of pointMeshMovers.
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
Template function which returns the un-mangled name of a given type. Useful for types which do not ha...
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
defineFunctionTypeNameAndDebug(includeFvConstraintEntry, 0)
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, primitiveEntryIstream)
addToRunTimeSelectionTable(functionEntry, includeFvConstraintEntry, dictionary)
string & inplaceExpandEntry(string &s, const dictionary &dict, const bool allowEnvVars, const bool allowEmpty, const char sigil='$')
Inplace expand occurrences of variables according to the dictionary.
Definition: stringOps.C:760
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void dictArgList(const Tuple2< string, label > &argString, word &configName, List< Tuple2< wordRe, label >> &args, List< Tuple3< word, string, label >> &namedArgs)
Parse dictionary substitution argument list.
Definition: dictionary.C:1494
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
bool exists(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist (as directory or file) in the file system?
Definition: POSIX.C:520
messageStream Info
labelList second(const UList< labelPair > &p)
Definition: patchToPatch.C:49
labelList first(const UList< labelPair > &p)
Definition: patchToPatch.C:39
Pair< word > dictAndKeyword(const word &scopedName)
Extracts dict name and keyword.
Definition: dictionary.C:1683
IOerror FatalIOError
string expandArg(const string &arg, dictionary &dict, const label lineNumber)
Expand arg within the dict context and return.
Definition: dictionaryIO.C:436
labelList f(nPoints)
dictionary dict
Foam::argList args(argc, argv)