includeEtcEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2015-2017 OpenFOAM Foundation
9  Copyright (C) 2019-2022 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "includeEtcEntry.H"
30 #include "etcFiles.H"
31 #include "stringOps.H"
32 #include "IFstream.H"
33 #include "IOstreams.H"
34 #include "UPstream.H"
35 #include "fileOperation.H"
36 #include "regIOobject.H"
38 
39 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
40 
42 
43 
44 namespace Foam
45 {
46 namespace functionEntries
47 {
49  (
50  functionEntry,
51  includeEtcEntry,
52  execute,
53  dictionaryIstream,
54  includeEtc
55  );
56 
58  (
59  functionEntry,
60  includeEtcEntry,
61  execute,
62  primitiveEntryIstream,
63  includeEtc
64  );
65 
67  (
68  functionEntry,
69  sincludeEtcEntry,
70  execute,
71  dictionaryIstream,
72  sincludeEtc
73  );
74 
76  (
77  functionEntry,
78  sincludeEtcEntry,
79  execute,
80  primitiveEntryIstream,
81  sincludeEtc
82  );
83 } // End namespace functionEntries
84 } // End namespace Foam
85 
86 
87 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
88 
90 (
91  const fileName& f,
92  const dictionary& dict
93 )
94 {
95  fileName fName(f);
96 
97  // Substitute dictionary and environment variables.
98  // Allow empty substitutions.
99  stringOps::inplaceExpand(fName, dict, true, true);
100 
101  if (fName.empty() || fName.isAbsolute())
102  {
103  return fName;
104  }
106  // Search etc directories for the file
107  return Foam::findEtcFile(fName);
108 }
109 
110 
112 (
113  const bool mandatory,
114  dictionary& parentDict,
115  Istream& is
116 )
117 {
118  const regIOobject* rioPtr = isA<regIOobject>(parentDict.topDict());
119 
120  const label oldComm
121  (
122  rioPtr && rioPtr->global()
124  : fileHandler().comm()
125  );
126 
127  const fileName rawName(is);
128  const fileName fName(resolveEtcFile(rawName, parentDict));
129 
130  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
131  auto& ifs = *ifsPtr;
132 
133  if (ifs)
134  {
136  {
137  // Report to stdout which file is included
138  Info<< fName << nl;
139  }
140  parentDict.read(ifs);
141 
142  fileHandler().comm(oldComm);
143  return true;
144  }
145 
146  fileHandler().comm(oldComm);
147 
148  if (!mandatory)
149  {
150  return true; // Never fails if optional
151  }
152 
154  << "Cannot open etc file "
155  << (ifs.name().size() ? ifs.name() : rawName)
156  << " while reading dictionary " << parentDict.relativeName()
158 
159  return false;
160 }
161 
162 
164 (
165  const bool mandatory,
166  const dictionary& parentDict,
168  Istream& is
169 )
170 {
171  const regIOobject* rioPtr = isA<regIOobject>(parentDict.topDict());
172 
173  const label oldComm
174  (
175  rioPtr && rioPtr->global()
177  : fileHandler().comm()
178  );
179 
180  const fileName rawName(is);
181  const fileName fName(resolveEtcFile(rawName, parentDict));
182 
183  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
184  auto& ifs = *ifsPtr;
185 
186  if (ifs)
187  {
189  {
190  // Report to stdout which file is included
191  Info<< fName << nl;
192  }
193  entry.read(parentDict, ifs);
194 
195  fileHandler().comm(oldComm);
196  return true;
197  }
198 
199  fileHandler().comm(oldComm);
200 
201  if (!mandatory)
202  {
203  return true; // Never fails if optional
204  }
205 
207  << "Cannot open etc file "
208  << (ifs.name().size() ? ifs.name() : rawName)
209  << " while reading dictionary " << parentDict.relativeName()
210  << exit(FatalIOError);
211 
212  return false;
213 }
214 
215 
216 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217 
219 (
220  dictionary& parentDict,
221  Istream& is
222 )
223 {
224  return includeEtcEntry::execute(true, parentDict, is);
225 }
226 
227 
229 (
230  const dictionary& parentDict,
232  Istream& is
233 )
234 {
235  return includeEtcEntry::execute(true, parentDict, entry, is);
236 }
237 
238 
240 (
241  dictionary& parentDict,
242  Istream& is
243 )
244 {
245  return includeEtcEntry::execute(false, parentDict, is);
246 }
247 
248 
250 (
251  const dictionary& parentDict,
253  Istream& is
254 )
255 {
256  return includeEtcEntry::execute(false, parentDict, entry, is);
257 }
258 
259 
260 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
Definition: dictionaryIO.C:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
static bool isAbsolute(const std::string &str)
Return true if filename starts with a &#39;/&#39; or &#39;\&#39; or (windows-only) with a filesystem-root.
Definition: fileNameI.H:129
static bool log
Report to stdout which file is included.
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:409
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
static bool execute(dictionary &parentDict, Istream &is)
Include etc file (if it exists) in a sub-dict context.
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
Functions to search &#39;etc&#39; directories for configuration files etc.
const dictionary & topDict() const
Return the top of the tree.
Definition: dictionary.C:185
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping. Does not use environment values...
Definition: stringOps.C:718
labelList f(nPoints)
virtual const dictionary & dict() const
This entry is not a dictionary, calling this function generates a FatalError.
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:179
static fileName resolveEtcFile(const fileName &f, const dictionary &dict)
Expand include fileName and search etc directories for the file.
virtual bool global() const
Is object global.
Definition: regIOobject.H:493
fileName findEtcFile(const fileName &name, const bool mandatory=false, unsigned short location=0777)
Search for a single FILE within the etc directories.
Definition: etcFiles.C:439
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
Macros for easy insertion into member function selection tables.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
messageStream Info
Information stream (stdout output on master, null elsewhere)
addNamedToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream, calc)
Pointer management similar to std::unique_ptr, with some additional methods and type checking...
Definition: HashPtrTable.H:48
Namespace for OpenFOAM.
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:63
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...