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  }
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 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
IOstreams.H
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Foam::primitiveEntry
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read,...
Definition: primitiveEntry.H:63
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::functionEntries::sincludeEtcEntry::execute
static bool execute(dictionary &parentDict, Istream &is)
Include etc file (if it exists) in a sub-dict context.
Definition: includeEtcEntry.C:212
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
Foam::functionEntries::includeEtcEntry::resolveEtcFile
static fileName resolveEtcFile(const fileName &f, const dictionary &dict)
Expand include fileName and search etc directories for the file.
Definition: includeEtcEntry.C:88
includeEtcEntry.H
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
Foam::findEtcFile
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
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::dictionary::relativeName
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
addToMemberFunctionSelectionTable.H
Macros for easy insertion into member function selection tables.
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
fileOperation.H
Foam::functionEntries::addNamedToMemberFunctionSelectionTable
addNamedToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream, calc)
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::functionEntries::includeEtcEntry::log
static bool log
Report to stdout which file is included.
Definition: includeEtcEntry.H:105
Foam::dictionary::read
bool read(Istream &is)
Read dictionary from Istream. Discards the header.
Definition: dictionaryIO.C:141
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::functionEntries::includeEtcEntry::execute
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
Definition: includeEtcEntry.C:110
etcFiles.H
Functions to search 'etc' directories for configuration files etc.
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
stringOps.H
Foam::fileName::isAbsolute
static bool isAbsolute(const std::string &str)
Definition: fileNameI.H:136