Loading...
Searching...
No Matches
includeEntry.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) 2011-2017 OpenFOAM Foundation
9 Copyright (C) 2018-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 "includeEntry.H"
30#include "stringOps.H"
31#include "IFstream.H"
32#include "IOstreams.H"
33#include "UPstream.H"
34#include "fileOperation.H"
35#include "regIOobject.H"
37
38// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39
41
42
43namespace Foam
44{
45namespace functionEntries
46{
48 (
51 execute,
52 dictionaryIstream,
53 include
54 );
55
57 (
60 execute,
61 primitiveEntryIstream,
62 include
63 );
64
66 (
69 execute,
70 dictionaryIstream,
71 sinclude
72 );
73
75 (
78 execute,
79 primitiveEntryIstream,
80 sinclude
81 );
82
83 // Compat 1712 and earlier
85 (
88 execute,
89 dictionaryIstream,
90 includeIfPresent
91 );
92
94 (
97 execute,
98 primitiveEntryIstream,
99 includeIfPresent
100 );
101} // End namespace functionEntries
102} // End namespace Foam
103
104
105// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
106
108(
109 const fileName& dir,
110 const fileName& f,
111 const dictionary& dict
112)
113{
114 fileName fName(f);
115
116 // Substitute dictionary and environment variables.
117 // Allow empty substitutions.
118 stringOps::inplaceExpand(fName, dict, true, true);
119
120 if (fName.empty() || fName.isAbsolute())
121 {
122 return fName;
124
125 // Relative name
126 return dir/fName;
127}
128
129
131(
132 const bool mandatory,
133 dictionary& parentDict,
134 Istream& is
135)
136{
137 const auto* rioPtr = isA<regIOobject>(parentDict.topDict());
138
139 const label oldComm
140 (
141 rioPtr && rioPtr->global()
143 : fileHandler().comm()
144 );
145
146 const fileName rawName(is);
147 const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
148
149 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
150 auto& ifs = *ifsPtr;
151
152 if (ifs)
153 {
155 {
156 // Report to stdout which file is included
157 Info<< fName << nl;
158 }
159
160 // Add watch on included file
161 if (rioPtr)
162 {
163 const_cast<regIOobject&>(*rioPtr).addWatch(fName);
164 }
165
166 parentDict.read(ifs);
167
168 fileHandler().comm(oldComm);
169 return true;
170 }
171
172 fileHandler().comm(oldComm);
173
174 if (!mandatory)
175 {
176 return true; // Never fails if optional
177 }
178
180 << "Cannot open include file "
181 << (ifs.name().size() ? ifs.name() : rawName)
182 << " while reading dictionary " << parentDict.relativeName()
183 << exit(FatalIOError);
184
185 return false;
186}
187
188
190(
191 const bool mandatory,
192 const dictionary& parentDict,
194 Istream& is
195)
196{
197 const auto* rioPtr = isA<regIOobject>(parentDict.topDict());
198
199 const label oldComm
200 (
201 rioPtr && rioPtr->global()
203 : fileHandler().comm()
204 );
205
206 const fileName rawName(is);
207 const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
208
209 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
210 auto& ifs = *ifsPtr;
211
212 if (ifs)
213 {
215 {
216 // Report to stdout which file is included
217 Info<< fName << nl;
218 }
219
220 // Add watch on included file
221 if (rioPtr)
222 {
223 const_cast<regIOobject&>(*rioPtr).addWatch(fName);
224 }
225
226 entry.read(parentDict, ifs);
227
228 fileHandler().comm(oldComm);
229 return true;
230 }
231
232 fileHandler().comm(oldComm);
233
234 if (!mandatory)
235 {
236 return true; // Never fails if optional
237 }
238
240 << "Cannot open include file "
241 << (ifs.name().size() ? ifs.name() : rawName)
242 << " while reading dictionary " << parentDict.relativeName()
243 << exit(FatalIOError);
245 return false;
246}
247
248
249// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
250
252(
253 dictionary& parentDict,
255)
256{
257 return includeEntry::execute(true, parentDict, is);
258}
259
260
262(
263 const dictionary& parentDict,
266)
267{
268 return includeEntry::execute(true, parentDict, entry, is);
269}
270
271
273(
274 dictionary& parentDict,
276)
277{
278 return includeEntry::execute(false, parentDict, is);
279}
280
281
283(
284 const dictionary& parentDict,
286 Istream& is
287)
288{
289 return includeEntry::execute(false, parentDict, entry, is);
290}
291
292
293// ************************************************************************* //
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.
virtual const fileName & name() const
The name of the stream.
Definition IOstream.C:33
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 std::string path(const std::string &str)
Return directory path name (part before last /).
Definition fileNameI.H:169
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, expects a single string to follow.
static fileName resolveFile(const fileName &dir, const fileName &f, const dictionary &dict)
Expand include fileName and return.
static bool log
Report to stdout which file is included.
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
A dictionary directive for conditionally including a file, expects a single string to follow.
static bool execute(dictionary &parentDict, Istream &is)
Include 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 bool read(const dictionary &dict, Istream &is)
Read tokens from the given stream.
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 void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED).
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition error.H:629
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).
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