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 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  DetailInfo << fName << endl;
127  }
128  parentDict.read(ifs);
129  return true;
130  }
131 
132  if (!mandatory)
133  {
134  return true; // Never fails if optional
135  }
136 
138  << "Cannot open etc file "
139  << (ifs.name().size() ? ifs.name() : rawName)
140  << " while reading dictionary " << parentDict.name()
141  << exit(FatalIOError);
142 
143  return false;
144 }
145 
146 
148 (
149  const bool mandatory,
150  const dictionary& parentDict,
152  Istream& is
153 )
154 {
155  const fileName rawName(is);
156  const fileName fName(resolveEtcFile(rawName, parentDict));
157 
158  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
159  auto& ifs = *ifsPtr;
160 
161  if (ifs)
162  {
164  {
165  DetailInfo << fName << endl;
166  }
167  entry.read(parentDict, ifs);
168  return true;
169  }
170 
171  if (!mandatory)
172  {
173  return true; // Never fails if optional
174  }
175 
177  << "Cannot open etc file "
178  << (ifs.name().size() ? ifs.name() : rawName)
179  << " while reading dictionary " << parentDict.name()
180  << exit(FatalIOError);
181 
182  return false;
183 }
184 
185 
186 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
187 
189 (
190  dictionary& parentDict,
191  Istream& is
192 )
193 {
194  return includeEtcEntry::execute(true, parentDict, is);
195 }
196 
197 
199 (
200  const dictionary& parentDict,
202  Istream& is
203 )
204 {
205  return includeEtcEntry::execute(true, parentDict, entry, is);
206 }
207 
208 
210 (
211  dictionary& parentDict,
212  Istream& is
213 )
214 {
215  return includeEtcEntry::execute(false, parentDict, is);
216 }
217 
218 
220 (
221  const dictionary& parentDict,
223  Istream& is
224 )
225 {
226  return includeEtcEntry::execute(false, parentDict, entry, is);
227 }
228 
229 
230 // ************************************************************************* //
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:62
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
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:210
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1181
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
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::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H:446
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::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Definition: stringOps.C:752
addToMemberFunctionSelectionTable.H
Macros for easy insertion into member function selection tables.
DetailInfo
#define DetailInfo
Definition: evalEntry.C:36
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:121
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.
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
f
labelList f(nPoints)
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
stringOps.H
Foam::fileName::isAbsolute
static bool isAbsolute(const std::string &str)
Return true if string starts with a '/'.
Definition: fileNameI.H:136