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-2022 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"
30 #include "stringOps.H"
31 #include "IFstream.H"
32 #include "IOstreams.H"
33 #include "UPstream.H"
34 #include "fileOperation.H"
35 #include "regIOobject.H"
37 
38 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
39 
41 
42 
43 namespace Foam
44 {
45 namespace functionEntries
46 {
48  (
49  functionEntry,
50  includeEntry,
51  execute,
52  dictionaryIstream,
53  include
54  );
55 
57  (
58  functionEntry,
59  includeEntry,
60  execute,
61  primitiveEntryIstream,
62  include
63  );
64 
66  (
67  functionEntry,
68  sincludeEntry,
69  execute,
70  dictionaryIstream,
71  sinclude
72  );
73 
75  (
76  functionEntry,
77  sincludeEntry,
78  execute,
79  primitiveEntryIstream,
80  sinclude
81  );
82 
83  // Compat 1712 and earlier
85  (
86  functionEntry,
87  sincludeEntry,
88  execute,
89  dictionaryIstream,
90  includeIfPresent
91  );
92 
94  (
95  functionEntry,
96  sincludeEntry,
97  execute,
98  primitiveEntryIstream,
99  includeIfPresent
100  );
101 } // End namespace functionEntries
102 } // End namespace Foam
103 
104 
105 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
106 
108 (
109  const fileName& dir,
110  const fileName& f,
111  const dictionary& dict
112 )
113 {
114  fileName fName(f);
115 
116  // Substitute dictionary and environment variables.
117  // Allow empty substitutions.
118  stringOps::inplaceExpand(fName, dict, true, true);
119 
120  if (fName.empty() || fName.isAbsolute())
121  {
122  return fName;
123  }
125  // Relative name
126  return dir/fName;
127 }
128 
129 
131 (
132  const bool mandatory,
133  dictionary& parentDict,
134  Istream& is
135 )
136 {
137  const auto* rioPtr = isA<regIOobject>(parentDict.topDict());
138 
139  const label oldComm
140  (
141  rioPtr && rioPtr->global()
143  : fileHandler().comm()
144  );
145 
146  const fileName rawName(is);
147  const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
148 
149  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
150  auto& ifs = *ifsPtr;
151 
152  if (ifs)
153  {
155  {
156  // Report to stdout which file is included
157  Info<< fName << nl;
158  }
159 
160  // Add watch on included file
161  if (rioPtr)
162  {
163  const_cast<regIOobject&>(*rioPtr).addWatch(fName);
164  }
165 
166  parentDict.read(ifs);
167 
168  fileHandler().comm(oldComm);
169  return true;
170  }
171 
172  fileHandler().comm(oldComm);
173 
174  if (!mandatory)
175  {
176  return true; // Never fails if optional
177  }
178 
180  << "Cannot open include file "
181  << (ifs.name().size() ? ifs.name() : rawName)
182  << " while reading dictionary " << parentDict.relativeName()
184 
185  return false;
186 }
187 
188 
190 (
191  const bool mandatory,
192  const dictionary& parentDict,
194  Istream& is
195 )
196 {
197  const auto* rioPtr = isA<regIOobject>(parentDict.topDict());
198 
199  const label oldComm
200  (
201  rioPtr && rioPtr->global()
203  : fileHandler().comm()
204  );
205 
206  const fileName rawName(is);
207  const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
208 
209  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
210  auto& ifs = *ifsPtr;
211 
212  if (ifs)
213  {
215  {
216  // Report to stdout which file is included
217  Info<< fName << nl;
218  }
219 
220  // Add watch on included file
221  if (rioPtr)
222  {
223  const_cast<regIOobject&>(*rioPtr).addWatch(fName);
224  }
225 
226  entry.read(parentDict, ifs);
227 
228  fileHandler().comm(oldComm);
229  return true;
230  }
231 
232  fileHandler().comm(oldComm);
233 
234  if (!mandatory)
235  {
236  return true; // Never fails if optional
237  }
238 
240  << "Cannot open include file "
241  << (ifs.name().size() ? ifs.name() : rawName)
242  << " while reading dictionary " << parentDict.relativeName()
243  << exit(FatalIOError);
244 
245  return false;
246 }
247 
248 
249 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
250 
252 (
253  dictionary& parentDict,
254  Istream& is
255 )
256 {
257  return includeEntry::execute(true, parentDict, is);
258 }
259 
260 
262 (
263  const dictionary& parentDict,
265  Istream& is
266 )
267 {
268  return includeEntry::execute(true, parentDict, entry, is);
269 }
270 
271 
273 (
274  dictionary& parentDict,
275  Istream& is
276 )
277 {
278  return includeEntry::execute(false, parentDict, is);
279 }
280 
281 
283 (
284  const dictionary& parentDict,
286  Istream& is
287 )
288 {
289  return includeEntry::execute(false, parentDict, entry, is);
290 }
291 
292 
293 // ************************************************************************* //
dictionary dict
A class for handling file names.
Definition: fileName.H:72
virtual const fileName & name() const
The name of the stream.
Definition: IOstream.C:33
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). Reads entries until EOF or when the first token i...
Definition: dictionaryIO.C:134
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
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:50
refPtr< fileOperation > fileHandler(std::nullptr_t)
Delete current file handler - forwards to fileOperation::handler()
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:169
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 label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:409
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A keyword and a list of tokens comprise a primitiveEntry. A primitiveEntry can be read...
static bool execute(dictionary &parentDict, Istream &is)
Include file (if it exists) in a sub-dict context.
Definition: includeEntry.C:266
const dictionary & topDict() const
Return the top of the tree.
Definition: dictionary.C:185
static fileName resolveFile(const fileName &dir, const fileName &f, const dictionary &dict)
Expand include fileName and return.
Definition: includeEntry.C:101
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:718
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:277
labelList f(nPoints)
virtual const dictionary & dict() const
This entry is not a dictionary, calling this function generates a FatalError.
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:179
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:627
static bool execute(const bool mandatory, dictionary &parentDict, Istream &is)
Include file in a sub-dict context.
Definition: includeEntry.C:124
Macros for easy insertion into member function selection tables.
regIOobject is an abstract class derived from IOobject to handle automatic object registration with t...
Definition: regIOobject.H:66
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
static bool log
Report to stdout which file is included.
Definition: includeEntry.H:114
virtual bool read(const dictionary &dict, Istream &is)
Read tokens from the given stream.
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 ...