Loading...
Searching...
No Matches
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-------------------------------------------------------------------------------
11License
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
44namespace Foam
45{
46namespace functionEntries
47{
49 (
52 execute,
53 dictionaryIstream,
54 includeEtc
55 );
56
58 (
61 execute,
62 primitiveEntryIstream,
63 includeEtc
64 );
65
67 (
70 execute,
71 dictionaryIstream,
72 sincludeEtc
73 );
74
76 (
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;
105
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()
157 << exit(FatalIOError);
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);
212 return false;
213}
214
215
216// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
217
219(
220 dictionary& parentDict,
222)
223{
224 return includeEtcEntry::execute(true, parentDict, is);
225}
226
227
229(
230 const dictionary& parentDict,
233)
234{
235 return includeEtcEntry::execute(true, parentDict, entry, is);
236}
237
238
240(
241 dictionary& parentDict,
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// ************************************************************************* //
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Macros for easy insertion into member function selection tables.
#define addNamedToMemberFunctionSelectionTable(baseType, thisType, funcName, argNames, lookupName)
Add to hash-table of functions with 'lookupName' as the key.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition Istream.H:60
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition UPstream.H:1069
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition dictionary.H:133
const dictionary & topDict() const
Return the top of the tree.
Definition dictionary.C:185
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition dictionary.C:179
bool read(Istream &is)
Read dictionary from Istream (discards the header). Reads entries until EOF or when the first token i...
A keyword and a list of tokens is an 'entry'.
Definition entry.H:66
entry(const keyType &keyword)
Construct from keyword.
Definition entry.C:62
A class for handling file names.
Definition fileName.H:75
static bool isAbsolute(const std::string &str)
Return true if filename starts with a '/' or '\' or (windows-only) with a filesystem-root.
Definition fileNameI.H:129
A dictionary directive for including a file found using the Foam::findEtcFile() mechanism.
static bool log
Report to stdout which file is included.
static fileName resolveEtcFile(const fileName &f, const dictionary &dict)
Expand include fileName and search etc directories for the file.
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
A dictionary directive for conditionally including an etc file, expects a single string to follow.
static bool execute(dictionary &parentDict, Istream &is)
Include etc file (if it exists) in a sub-dict context.
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
primitiveEntry(const keyType &key)
Construct from keyword and no tokens.
virtual const dictionary & dict() const
This entry is not a dictionary, calling this function generates a FatalError.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition regIOobject.H:71
virtual bool global() const
Is object global.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
Functions to search 'etc' directories for configuration files etc.
Namespace for containing a functionEntry.
Definition calcEntry.C:33
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.
Namespace for OpenFOAM.
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler().
messageStream Info
Information stream (stdout output on master, null elsewhere).
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
const Type * isA(const U &obj)
Attempt dynamic_cast to Type.
Definition typeInfo.H:87
IOerror FatalIOError
Error stream (stdout output on all processes), with additional 'FOAM FATAL IO ERROR' header text and ...
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition errorManip.H:125
constexpr char nl
The newline '\n' character (0x0a).
Definition Ostream.H:50
labelList f(nPoints)
dictionary dict