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-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 "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  DetailInfo << fName << endl;
147  }
148 
149  // Add watch on included file
150  const dictionary& top = parentDict.topDict();
151  if (isA<regIOobject>(top))
152  {
153  regIOobject& rio = const_cast<regIOobject&>
154  (
155  dynamic_cast<const regIOobject&>(top)
156  );
157  rio.addWatch(fName);
158  }
159 
160  parentDict.read(ifs);
161  return true;
162  }
163 
164  if (!mandatory)
165  {
166  return true; // Never fails if optional
167  }
168 
170  << "Cannot open include file "
171  << (ifs.name().size() ? ifs.name() : rawName)
172  << " while reading dictionary " << parentDict.name()
173  << exit(FatalIOError);
174 
175  return false;
176 }
177 
178 
180 (
181  const bool mandatory,
182  const dictionary& parentDict,
184  Istream& is
185 )
186 {
187  const fileName rawName(is);
188  const fileName fName(resolveFile(is.name().path(), rawName, parentDict));
189 
190  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
191  auto& ifs = *ifsPtr;
192 
193  if (ifs)
194  {
196  {
197  DetailInfo << fName << endl;
198  }
199 
200  // Add watch on included file
201  const dictionary& top = parentDict.topDict();
202  if (isA<regIOobject>(top))
203  {
204  regIOobject& rio = const_cast<regIOobject&>
205  (
206  dynamic_cast<const regIOobject&>(top)
207  );
208  rio.addWatch(fName);
209  }
210 
211  entry.read(parentDict, ifs);
212  return true;
213  }
214 
215  if (!mandatory)
216  {
217  return true; // Never fails if optional
218  }
219 
221  << "Cannot open include file "
222  << (ifs.name().size() ? ifs.name() : rawName)
223  << " while reading dictionary " << parentDict.name()
224  << exit(FatalIOError);
225 
226  return false;
227 }
228 
229 
230 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
231 
233 (
234  dictionary& parentDict,
235  Istream& is
236 )
237 {
238  return includeEntry::execute(true, parentDict, is);
239 }
240 
241 
243 (
244  const dictionary& parentDict,
246  Istream& is
247 )
248 {
249  return includeEntry::execute(true, parentDict, entry, is);
250 }
251 
252 
254 (
255  dictionary& parentDict,
256  Istream& is
257 )
258 {
259  return includeEntry::execute(false, parentDict, is);
260 }
261 
262 
264 (
265  const dictionary& parentDict,
267  Istream& is
268 )
269 {
270  return includeEntry::execute(false, parentDict, entry, is);
271 }
272 
273 
274 // ************************************************************************* //
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::fileName::path
static std::string path(const std::string &str)
Return directory path name (part before last /)
Definition: fileNameI.H:186
Foam::regIOobject::addWatch
virtual void addWatch()
Add file watch on object (if registered and READ_IF_MODIFIED)
Definition: regIOobject.C:280
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::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H: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.
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:39
Foam::functionEntries::includeEntry::log
static bool log
Report to stdout which file is included.
Definition: includeEntry.H:110
DetailInfo
#define DetailInfo
Definition: evalEntry.C:36
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:121
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.
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:67
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:254
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
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)
Return true if string starts with a '/'.
Definition: fileNameI.H:136