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-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 "includeEtcEntry.H"
31#include "etcFiles.H"
32#include "stringOps.H"
33#include "IFstream.H"
34#include "IOstreams.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 includeEtc
53 );
54
56 (
59 execute,
60 primitiveEntryIstream,
61 includeEtc
62 );
63
65 (
68 execute,
69 dictionaryIstream,
70 sincludeEtc
71 );
72
74 (
77 execute,
78 primitiveEntryIstream,
79 sincludeEtc
80 );
81} // End namespace functionEntries
82} // End namespace Foam
83
84
85// * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
86
88(
89 const fileName& f,
90 const dictionary& dict
91)
92{
93 fileName fName(f);
94
95 // Substitute dictionary and environment variables.
96 // Allow empty substitutions.
97 stringOps::inplaceExpand(fName, dict, true, true);
98
99 if (fName.empty() || fName.isAbsolute())
100 {
101 return fName;
102 }
103
104 // Search etc directories for the file
105 return Foam::findEtcFile(fName);
106}
107
108
110(
111 const bool mandatory,
112 dictionary& parentDict,
113 Istream& is
114)
115{
116 const fileName rawName(is);
117 const fileName fName(resolveEtcFile(rawName, parentDict));
118
119 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
120 auto& ifs = *ifsPtr;
121
122 if (ifs)
123 {
125 {
126 // Report to stdout which file is included
127 Info<< fName << nl;
128 }
129 parentDict.read(ifs);
130 return true;
131 }
132
133 if (!mandatory)
134 {
135 return true; // Never fails if optional
136 }
137
139 << "Cannot open etc file "
140 << (ifs.name().size() ? ifs.name() : rawName)
141 << " while reading dictionary " << parentDict.relativeName()
142 << exit(FatalIOError);
143
144 return false;
145}
146
147
149(
150 const bool mandatory,
151 const dictionary& parentDict,
153 Istream& is
154)
155{
156 const fileName rawName(is);
157 const fileName fName(resolveEtcFile(rawName, parentDict));
158
159 autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
160 auto& ifs = *ifsPtr;
161
162 if (ifs)
163 {
165 {
166 // Report to stdout which file is included
167 Info<< fName << nl;
168 }
169 entry.read(parentDict, ifs);
170 return true;
171 }
172
173 if (!mandatory)
174 {
175 return true; // Never fails if optional
176 }
177
179 << "Cannot open etc file "
180 << (ifs.name().size() ? ifs.name() : rawName)
181 << " while reading dictionary " << parentDict.relativeName()
182 << exit(FatalIOError);
183
184 return false;
185}
186
187
188// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
189
191(
192 dictionary& parentDict,
193 Istream& is
194)
195{
196 return includeEtcEntry::execute(true, parentDict, is);
197}
198
199
201(
202 const dictionary& parentDict,
204 Istream& is
205)
206{
207 return includeEtcEntry::execute(true, parentDict, entry, is);
208}
209
210
212(
213 dictionary& parentDict,
214 Istream& is
215)
216{
217 return includeEtcEntry::execute(false, parentDict, is);
218}
219
220
222(
223 const dictionary& parentDict,
225 Istream& is
226)
227{
228 return includeEtcEntry::execute(false, parentDict, entry, is);
229}
230
231
232// ************************************************************************* //
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: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
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 bool isAbsolute(const std::string &str)
Definition: fileNameI.H:136
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.
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,.
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
Functions to search 'etc' directories for configuration files etc.
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)
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:446
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