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 -------------------------------------------------------------------------------
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 "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 
42 namespace Foam
43 {
44 namespace functionEntries
45 {
47  (
48  functionEntry,
49  includeEntry,
50  execute,
51  dictionaryIstream,
52  include
53  );
54 
56  (
57  functionEntry,
58  includeEntry,
59  execute,
60  primitiveEntryIstream,
61  include
62  );
63 
65  (
66  functionEntry,
67  sincludeEntry,
68  execute,
69  dictionaryIstream,
70  sinclude
71  );
72 
74  (
75  functionEntry,
76  sincludeEntry,
77  execute,
78  primitiveEntryIstream,
79  sinclude
80  );
81 
82  // Compat 1712 and earlier
84  (
85  functionEntry,
86  sincludeEntry,
87  execute,
88  dictionaryIstream,
89  includeIfPresent
90  );
91 
93  (
94  functionEntry,
95  sincludeEntry,
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 // ************************************************************************* //
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::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:176
Foam::regIOobject::addWatch
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:267
Foam::fileHandler
const fileOperation & fileHandler()
Get current file handler.
Definition: fileOperation.C:1485
Foam::FatalIOError
IOerror FatalIOError
Foam::stringOps::inplaceExpand
void inplaceExpand(std::string &s, const HashTable< string > &mapping, const char sigil='$')
Definition: stringOps.C:731
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.
Foam::functionEntries::includeEntry::resolveFile
static fileName resolveFile(const fileName &dir, const fileName &f, const dictionary &dict)
Expand include fileName and return.
Definition: includeEntry.C:107
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:40
Foam::functionEntries::includeEntry::log
static bool log
Report to stdout which file is included.
Definition: includeEntry.H:110
IFstream.H
Foam::dictionary::topDict
const dictionary & topDict() const
Return the top of the tree.
Definition: dictionary.C:192
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::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
Time.H
Foam::autoPtr
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition: HashPtrTable.H:53
Foam::regIOobject
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:73
Foam::nl
constexpr char nl
Definition: Ostream.H:404
f
labelList f(nPoints)
includeEntry.H
Foam::functionEntries::sincludeEntry::execute
static bool execute(dictionary &parentDict, Istream &is)
Include file (if it exists) in a sub-dict context.
Definition: includeEntry.C:256
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
stringOps.H
Foam::functionEntries::includeEntry::execute
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
Definition: includeEntry.C:130
Foam::fileName::isAbsolute
static bool isAbsolute(const std::string &str)
Definition: fileNameI.H:136