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-2024 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 "dictionary.H"
28 #include "IFstream.H"
30 #include "stringOps.H"
31 #include "IOobject.H"
32 #include "fileOperation.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const Foam::word Foam::functionEntries::includeEntry::typeName
37 (
38  Foam::functionEntries::includeEntry::typeName_()
39 );
40 
41 // Don't lookup the debug switch here as the debug switch dictionary
42 // might include includeEntry
43 int Foam::functionEntries::includeEntry::debug(0);
44 
46 
47 
48 namespace Foam
49 {
50 namespace functionEntries
51 {
53  (
56  execute,
57  dictionaryIstream
58  );
59 
61  (
64  execute,
65  primitiveEntryIstream
66  );
67 }
68 }
69 
70 
71 // * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * * //
72 
74 Foam::functionEntries::includeEntry::insertNamedArgs
75 (
76  dictionary& parentDict,
77  Istream& is
78 )
79 {
80  List<Tuple3<word, string, label>> namedArgs;
81 
82  ISstream& iss = dynamic_cast<ISstream&>(is);
83 
84  // If the next character is a '(' process the arguments
85  if (iss.peek() == token::BEGIN_LIST)
86  {
87  // Read line containing the arguments into a string
88  Tuple2<string, label> fNameArgs(string::null, is.lineNumber());
89  iss.readList(fNameArgs.first());
90 
91  // Parse the argument string
92  word funcType;
93  List<Tuple2<wordRe, label>> args;
94  dictArgList(fNameArgs, funcType, args, namedArgs);
95 
96  // Add the named arguments as entries into the parentDict
97  // temporarily renaming any existing entries with the same name
98  forAll(namedArgs, i)
99  {
100  const Pair<word> dAk(dictAndKeyword(namedArgs[i].first()));
101  dictionary& subDict(parentDict.scopedDict(dAk.first()));
102 
103  // Rename the original entry adding a '_'
104  if (subDict.found(dAk.second()))
105  {
106  keyType tmpName(dAk.second());
107  tmpName += '_';
108  subDict.changeKeyword(dAk.second(), tmpName);
109  }
110 
111  // Add the temporary argument entry
112  IStringStream entryStream
113  (
114  dAk.second()
115  + ' '
116  + expandArg
117  (
118  namedArgs[i].second(),
119  parentDict,
120  namedArgs[i].third()
121  )
122  + ';'
123  );
124  subDict.set(entry::New(entryStream).ptr());
125  }
126  }
127 
128  return namedArgs;
129 }
130 
131 
132 void Foam::functionEntries::includeEntry::removeInsertNamedArgs
133 (
134  dictionary& parentDict,
135  const List<Tuple3<word, string, label>>& namedArgs
136 )
137 {
138  forAll(namedArgs, i)
139  {
140  // Remove the temporary argument entry
141  parentDict.remove(namedArgs[i].first());
142 
143  // Reinstate the original entry
144  const Pair<word> dAk(dictAndKeyword(namedArgs[i].first()));
145  dictionary& subDict(parentDict.scopedDict(dAk.first()));
146  keyType tmpName(dAk.second());
147  tmpName += '_';
148  subDict.changeKeyword(tmpName, dAk.second());
149  }
150 }
151 
152 
153 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
154 
156 (
157  Istream& is,
158  const dictionary& dict
159 )
160 {
161  fileName fName(is);
162  // Substitute dictionary and environment variables. Allow empty
163  // substitutions.
164  stringOps::inplaceExpandEntry(fName, dict, true, true);
165 
166  if (fName.empty() || fName.isAbsolute())
167  {
168  return fName;
169  }
170  else
171  {
172  // relative name
173  return fileName(is.name()).path()/fName;
174  }
175 }
176 
177 
179 (
180  const fileName& dir,
181  const fileName& f,
182  const dictionary& dict
183 )
184 {
185  fileName fName(f);
186  // Substitute dictionary and environment variables. Allow empty
187  // substitutions.
188  stringOps::inplaceExpandEntry(fName, dict, true, true);
189 
190  if (fName.empty() || fName.isAbsolute())
191  {
192  return fName;
193  }
194  else
195  {
196  // relative name
197  return dir/fName;
198  }
199 }
200 
201 
202 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
203 
205 (
206  dictionary& parentDict,
207  Istream& is
208 )
209 {
210  const fileName rawFName(is);
211 
212  const fileName fName
213  (
214  includeFileName(is.name().path(), rawFName, parentDict)
215  );
216 
217  // Cache the optional named arguments
218  // temporarily inserted into parentDict
220  (
221  insertNamedArgs(parentDict, is)
222  );
223 
224  autoPtr<ISstream> ifsPtr
225  (
226  fileHandler().NewIFstream(fName, is.format(), is.version())
227  );
228  ISstream& ifs = ifsPtr();
229 
230  if (ifs)
231  {
233  {
234  Info<< fName << endl;
235  }
236 
237  // Cache the FoamFile entry if present
238  dictionary foamFileDict;
239  if (parentDict.found(IOobject::foamFile))
240  {
241  foamFileDict = parentDict.subDict(IOobject::foamFile);
242  }
243 
244  // Read and clear the FoamFile entry
245  parentDict.read(ifs);
246 
247  // Reinstate original FoamFile entry
248  if (foamFileDict.size() != 0)
249  {
250  dictionary parentDictTmp(parentDict);
251  parentDict.clear();
252  parentDict.add(IOobject::foamFile, foamFileDict);
253  parentDict += parentDictTmp;
254  }
255  }
256  else
257  {
259  (
260  is
261  ) << "Cannot open include file "
262  << (ifs.name().size() ? ifs.name() : rawFName)
263  << " while reading dictionary " << parentDict.name()
264  << exit(FatalIOError);
265  }
266 
267  // Remove named argument entries from parentDict
268  // renaming any existing entries which had the same name
269  removeInsertNamedArgs(parentDict, namedArgs);
270 
271  return true;
272 }
273 
274 
276 (
277  const dictionary& parentDict,
279  Istream& is
280 )
281 {
282  const fileName rawFName(is);
283 
284  const fileName fName
285  (
286  includeFileName(is.name().path(), rawFName, parentDict)
287  );
288 
289  // Cache the optional named arguments
290  // temporarily inserted into parentDict
292  (
293  insertNamedArgs(const_cast<dictionary&>(parentDict), is)
294  );
295 
296  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
297  ISstream& ifs = ifsPtr();
298 
299  if (ifs)
300  {
302  {
303  Info<< fName << endl;
304  }
305 
306  entry.read(parentDict, ifs);
307  }
308  else
309  {
311  (
312  is
313  ) << "Cannot open include file "
314  << (ifs.name().size() ? ifs.name() : rawFName)
315  << " while reading dictionary " << parentDict.name()
316  << exit(FatalIOError);
317  }
318 
319  // Remove named argument entries from parentDict
320  // renaming any existing entries which had the same name
321  removeInsertNamedArgs(const_cast<dictionary&>(parentDict), namedArgs);
322 
323  return true;
324 }
325 
326 
327 // ************************************************************************* //
#define forAll(list, i)
Loop across all elements in list.
Definition: UList.H:433
Macros for easy insertion into member function selection tables.
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
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:111
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:849
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1020
void clear()
Clear the dictionary.
Definition: dictionary.C:1359
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:539
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:68
static bool New(dictionary &parentDict, Istream &)
Construct from Istream and insert into dictionary.
Definition: entryIO.C:103
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
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:205
static fileName includeFileName(Istream &, const dictionary &)
Read the include fileName from Istream, expand and return.
Definition: includeEntry.C:156
static bool log
Report which file is included to stdout.
Definition: includeEntry.H:119
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:66
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
static const string null
An empty string.
Definition: string.H:88
@ BEGIN_LIST
Definition: token.H:109
A class for handling words, derived from string.
Definition: word.H:62
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:346
addToMemberFunctionSelectionTable(functionEntry, includeFvConstraintEntry, execute, dictionaryIstream)
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:1495
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
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:1684
IOerror FatalIOError
string expandArg(const string &arg, dictionary &dict, const label lineNumber)
Expand arg within the dict context and return.
Definition: dictionaryIO.C:448
labelList f(nPoints)
dictionary dict
Foam::argList args(argc, argv)