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-2021 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"
31#include "stringOps.H"
32#include "IFstream.H"
33#include "IOstreams.H"
34#include "Time.H"
35#include "fileOperation.H"
36
37// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38
40
41
42namespace Foam
43{
44namespace functionEntries
45{
47 (
50 execute,
51 dictionaryIstream,
52 include
53 );
54
56 (
59 execute,
60 primitiveEntryIstream,
61 include
62 );
63
65 (
68 execute,
69 dictionaryIstream,
70 sinclude
71 );
72
74 (
77 execute,
78 primitiveEntryIstream,
79 sinclude
80 );
81
82 // Compat 1712 and earlier
84 (
87 execute,
88 dictionaryIstream,
89 includeIfPresent
90 );
91
93 (
96 execute,
97 primitiveEntryIstream,
98 includeIfPresent
99 );
100} // End namespace functionEntries
101} // End namespace Foam
102
103
104// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
105
107(
108 const fileName& dir,
109 const fileName& f,
110 const dictionary& dict
111)
112{
113 fileName fName(f);
114
115 // Substitute dictionary and environment variables.
116 // Allow empty substitutions.
117 stringOps::inplaceExpand(fName, dict, true, true);
118
119 if (fName.empty() || fName.isAbsolute())
120 {
121 return fName;
122 }
123
124 // Relative name
125 return dir/fName;
126}
127
128
130(
131 const bool mandatory,
132 dictionary& parentDict,
133 Istream& is
134)
135{
136 const fileName rawName(is);
137 const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
138
139 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
140 auto& ifs = *ifsPtr;
141
142 if (ifs)
143 {
145 {
146 // Report to stdout which file is included
147 Info<< fName << nl;
148 }
149
150 // Add watch on included file
151 const dictionary& top = parentDict.topDict();
152 if (isA<regIOobject>(top))
153 {
154 regIOobject& rio = const_cast<regIOobject&>
155 (
156 dynamic_cast<const regIOobject&>(top)
157 );
158 rio.addWatch(fName);
159 }
160
161 parentDict.read(ifs);
162 return true;
163 }
164
165 if (!mandatory)
166 {
167 return true; // Never fails if optional
168 }
169
171 << "Cannot open include file "
172 << (ifs.name().size() ? ifs.name() : rawName)
173 << " while reading dictionary " << parentDict.relativeName()
174 << exit(FatalIOError);
175
176 return false;
177}
178
179
181(
182 const bool mandatory,
183 const dictionary& parentDict,
185 Istream& is
186)
187{
188 const fileName rawName(is);
189 const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
190
191 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
192 auto& ifs = *ifsPtr;
193
194 if (ifs)
195 {
197 {
198 // Report to stdout which file is included
199 Info<< fName << nl;
200 }
201
202 // Add watch on included file
203 const dictionary& top = parentDict.topDict();
204 if (isA<regIOobject>(top))
205 {
206 regIOobject& rio = const_cast<regIOobject&>
207 (
208 dynamic_cast<const regIOobject&>(top)
209 );
210 rio.addWatch(fName);
211 }
212
213 entry.read(parentDict, ifs);
214 return true;
215 }
216
217 if (!mandatory)
218 {
219 return true; // Never fails if optional
220 }
221
223 << "Cannot open include file "
224 << (ifs.name().size() ? ifs.name() : rawName)
225 << " while reading dictionary " << parentDict.relativeName()
226 << exit(FatalIOError);
227
228 return false;
229}
230
231
232// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
233
235(
236 dictionary& parentDict,
237 Istream& is
238)
239{
240 return includeEntry::execute(true, parentDict, is);
241}
242
243
245(
246 const dictionary& parentDict,
248 Istream& is
249)
250{
251 return includeEntry::execute(true, parentDict, entry, is);
252}
253
254
256(
257 dictionary& parentDict,
258 Istream& is
259)
260{
261 return includeEntry::execute(false, parentDict, is);
262}
263
264
266(
267 const dictionary& parentDict,
269 Istream& is
270)
271{
272 return includeEntry::execute(false, parentDict, entry, is);
273}
274
275
276// ************************************************************************* //
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
Return the name of the stream.
Definition: IOstream.C:40
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: autoPtr.H:66
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const dictionary & topDict() const
Return the top of the tree.
Definition: dictionary.C:192
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:70
A class for handling file names.
Definition: fileName.H:76
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
static bool isAbsolute(const std::string &str)
Definition: fileNameI.H:136
A dictionary directive for including a file, expects a single string to follow.
Definition: includeEntry.H:74
static fileName resolveFile(const fileName &dir, const fileName &f, const dictionary &dict)
Expand include fileName and return.
Definition: includeEntry.C:107
static bool log
Report to stdout which file is included.
Definition: includeEntry.H:110
A functionEntry causes entries to be added/manipulated on the specified dictionary given an input str...
Definition: functionEntry.H:69
virtual bool execute()
Calculate the output fields.
Computes the natural logarithm of an input volScalarField.
Definition: log.H:230
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
virtual const dictionary & dict() const
This entry is not a dictionary,.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:76
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:267
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
Namespace for OpenFOAM.
const fileOperation & fileHandler()
Get current file handler.
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOerror FatalIOError
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
labelList f(nPoints)
dictionary dict