All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
includeEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "includeEntry.H"
27 #include "dictionary.H"
28 #include "IFstream.H"
30 #include "stringOps.H"
31 #include "IOobject.H"
32 #include "fileOperation.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const Foam::word Foam::functionEntries::includeEntry::typeName
37 (
38  Foam::functionEntries::includeEntry::typeName_()
39 );
40 
41 // Don't lookup the debug switch here as the debug switch dictionary
42 // might include includeEntry
43 int Foam::functionEntries::includeEntry::debug(0);
44 
46 
47 
48 namespace Foam
49 {
50 namespace functionEntries
51 {
53  (
54  functionEntry,
55  includeEntry,
56  execute,
57  dictionaryIstream
58  );
59 
61  (
62  functionEntry,
63  includeEntry,
64  execute,
65  primitiveEntryIstream
66  );
67 }
68 }
69 
70 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
71 
73 (
74  Istream& is,
75  const dictionary& dict
76 )
77 {
78  fileName fName(is);
79  // Substitute dictionary and environment variables. Allow empty
80  // substitutions.
81  stringOps::inplaceExpand(fName, dict, true, true);
82 
83  if (fName.empty() || fName.isAbsolute())
84  {
85  return fName;
86  }
87  else
88  {
89  // relative name
90  return fileName(is.name()).path()/fName;
91  }
92 }
93 
94 
96 (
97  const fileName& dir,
98  const fileName& f,
99  const dictionary& dict
100 )
101 {
102  fileName fName(f);
103  // Substitute dictionary and environment variables. Allow empty
104  // substitutions.
105  stringOps::inplaceExpand(fName, dict, true, true);
106 
107  if (fName.empty() || fName.isAbsolute())
108  {
109  return fName;
110  }
111  else
112  {
113  // relative name
114  return dir/fName;
115  }
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
122 (
123  dictionary& parentDict,
124  Istream& is
125 )
126 {
127  const fileName rawFName(is);
128  const fileName fName
129  (
130  includeFileName(is.name().path(), rawFName, parentDict)
131  );
132 
133  autoPtr<ISstream> ifsPtr
134  (
135  fileHandler().NewIFstream(fName, is.format(), is.version())
136  );
137  ISstream& ifs = ifsPtr();
138 
139  if (ifs)
140  {
142  {
143  Info<< fName << endl;
144  }
145 
146  // Cache the FoamFile entry if present
147  dictionary foamFileDict;
148  if (parentDict.found(IOobject::foamFile))
149  {
150  foamFileDict = parentDict.subDict(IOobject::foamFile);
151  }
152 
153  // Read and clear the FoamFile entry
154  parentDict.read(ifs);
155 
156  // Reinstate original FoamFile entry
157  if (foamFileDict.size() != 0)
158  {
159  dictionary parentDictTmp(parentDict);
160  parentDict.clear();
161  parentDict.add(IOobject::foamFile, foamFileDict);
162  parentDict += parentDictTmp;
163  }
164 
165  return true;
166  }
167  else
168  {
170  (
171  is
172  ) << "Cannot open include file "
173  << (ifs.name().size() ? ifs.name() : rawFName)
174  << " while reading dictionary " << parentDict.name()
175  << exit(FatalIOError);
176 
177  return false;
178  }
179 }
180 
181 
183 (
184  const dictionary& parentDict,
185  primitiveEntry& entry,
186  Istream& is
187 )
188 {
189  const fileName rawFName(is);
190  const fileName fName
191  (
192  includeFileName(is.name().path(), rawFName, parentDict)
193  );
194 
195  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
196  ISstream& ifs = ifsPtr();
197 
198  if (ifs)
199  {
201  {
202  Info<< fName << endl;
203  }
204  entry.read(parentDict, ifs);
205  return true;
206  }
207  else
208  {
210  (
211  is
212  ) << "Cannot open include file "
213  << (ifs.name().size() ? ifs.name() : rawFName)
214  << " while reading dictionary " << parentDict.name()
215  << exit(FatalIOError);
216 
217  return false;
218  }
219 }
220 
221 
222 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
A class for handling file names.
Definition: fileName.H:79
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:121
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:122
versionNumber version() const
Return the stream version.
Definition: IOstream.H:396
bool read(Istream &, const bool keepHeader=false)
Read dictionary from Istream, optionally keeping the header.
Definition: dictionaryIO.C:104
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1153
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:1002
A keyword and a list of tokens is a &#39;primitiveEntry&#39;. An primitiveEntry can be read, written and printed, and the types and values of its tokens analysed.
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:73
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:294
const fileOperation & fileHandler()
Get current file handler.
streamFormat format() const
Return current stream format.
Definition: IOstream.H:374
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:104
static fileName includeFileName(Istream &, const dictionary &)
Read the include fileName from Istream, expand and return.
Definition: includeEntry.C:73
Generic input stream.
Definition: ISstream.H:52
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:81
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
Macros for easy insertion into member function selection tables.
messageStream Info
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:265
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
static bool log
Report which file is included to stdout.
Definition: includeEntry.H:88
void clear()
Clear the dictionary.
Definition: dictionary.C:1483
Namespace for OpenFOAM.
IOerror FatalIOError