includeEtcEntry.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) 2015-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 "includeEtcEntry.H"
27 #include "etcFiles.H"
28 #include "stringOps.H"
30 #include "IOobject.H"
31 #include "fileOperation.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const Foam::word Foam::functionEntries::includeEtcEntry::typeName
36 (
37  Foam::functionEntries::includeEtcEntry::typeName_()
38 );
39 
40 // Don't lookup the debug switch here as the debug switch dictionary
41 // might include includeEtcEntry
42 int Foam::functionEntries::includeEtcEntry::debug(0);
43 
45 
46 
47 namespace Foam
48 {
49 namespace functionEntries
50 {
52  (
55  execute,
56  dictionaryIstream
57  );
58 
60  (
63  execute,
64  primitiveEntryIstream
65  );
66 }
67 }
68 
69 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
70 
71 Foam::fileName Foam::functionEntries::includeEtcEntry::includeEtcFileName
72 (
73  const fileName& f,
74  const dictionary& dict
75 )
76 {
77  fileName fName(f);
78 
79  // Substitute dictionary and environment variables.
80  // Allow empty substitutions.
81  stringOps::inplaceExpandEntry(fName, dict, true, true);
82 
83  if (fName.empty() || fName.isAbsolute())
84  {
85  return fName;
86  }
87  else
88  {
89  // Search the etc directories for the file
90  return findEtcFile(fName);
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
98 (
99  dictionary& parentDict,
100  Istream& is
101 )
102 {
103  const fileName rawFName(is);
104 
105  const fileName fName
106  (
107  includeEtcFileName(rawFName, parentDict)
108  );
109 
110  // IFstream ifs(fName);
111  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
112  ISstream& ifs = ifsPtr();
113 
114  if (ifs)
115  {
117  {
118  Info<< fName << endl;
119  }
120 
121  // Cache the FoamFile entry if present
122  dictionary foamFileDict;
123  if (parentDict.found(IOobject::foamFile))
124  {
125  foamFileDict = parentDict.subDict(IOobject::foamFile);
126  }
127 
128  // Read and clear the FoamFile entry
129  parentDict.read(ifs);
130 
131  // Reinstate original FoamFile entry
132  if (foamFileDict.size() != 0)
133  {
134  dictionary parentDictTmp(parentDict);
135  parentDict.clear();
136  parentDict.add(IOobject::foamFile, foamFileDict);
137  parentDict += parentDictTmp;
138  }
139 
140  return true;
141  }
142  else
143  {
145  (
146  is
147  ) << "Cannot open etc file "
148  << (ifs.name().size() ? ifs.name() : rawFName)
149  << " while reading dictionary " << parentDict.name()
150  << exit(FatalIOError);
151 
152  return false;
153  }
154 }
155 
156 
158 (
159  const dictionary& parentDict,
161  Istream& is
162 )
163 {
164  const fileName rawFName(is);
165 
166  const fileName fName
167  (
168  includeEtcFileName(rawFName, parentDict)
169  );
170 
171  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
172  ISstream& ifs = ifsPtr();
173 
174  if (ifs)
175  {
177  {
178  Info<< fName << endl;
179  }
180  entry.read(parentDict, ifs);
181  return true;
182  }
183  else
184  {
186  (
187  is
188  ) << "Cannot open etc file "
189  << (ifs.name().size() ? ifs.name() : rawFName)
190  << " while reading dictionary " << parentDict.name()
191  << exit(FatalIOError);
192 
193  return false;
194  }
195 }
196 
197 
198 // ************************************************************************* //
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
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
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
A class for handling file names.
Definition: fileName.H:82
Specify an etc file to include when reading dictionaries, expects a single string to follow.
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
static bool log
Report which file is included to stdout.
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,...
const dictionary & dict() const
This entry is not a dictionary,.
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
Functions to search 'etc' directories for configuration files etc.
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: etcFiles.C:252
IOerror FatalIOError
labelList f(nPoints)
dictionary dict