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 -------------------------------------------------------------------------------
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"
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 
42 namespace Foam
43 {
44 namespace functionEntries
45 {
47  (
48  functionEntry,
49  includeEtcEntry,
50  execute,
51  dictionaryIstream,
52  includeEtc
53  );
54 
56  (
57  functionEntry,
58  includeEtcEntry,
59  execute,
60  primitiveEntryIstream,
61  includeEtc
62  );
63 
65  (
66  functionEntry,
67  sincludeEtcEntry,
68  execute,
69  dictionaryIstream,
70  sincludeEtc
71  );
72 
74  (
75  functionEntry,
76  sincludeEtcEntry,
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  }
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()
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 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:71
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.
Definition: dictionaryIO.C:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
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:49
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.
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.
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:717
labelList f(nPoints)
virtual const dictionary & dict() const
This entry is not a dictionary,.
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.
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:607
Macros for easy insertion into member function selection tables.
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 ...