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-2019 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(fileHandler().NewIFstream(fName));
134  ISstream& ifs = ifsPtr();
135 
136  if (ifs)
137  {
139  {
140  Info<< fName << endl;
141  }
142 
143  // Cache the FoamFile entry if present
144  dictionary foamFileDict;
145  if (parentDict.found(IOobject::foamFile))
146  {
147  foamFileDict = parentDict.subDict(IOobject::foamFile);
148  }
149 
150  // Read and clear the FoamFile entry
151  parentDict.read(ifs);
152 
153  // Reinstate original FoamFile entry
154  if (foamFileDict.size() != 0)
155  {
156  dictionary parentDictTmp(parentDict);
157  parentDict.clear();
158  parentDict.add(IOobject::foamFile, foamFileDict);
159  parentDict += parentDictTmp;
160  }
161 
162  return true;
163  }
164  else
165  {
167  (
168  is
169  ) << "Cannot open include file "
170  << (ifs.name().size() ? ifs.name() : rawFName)
171  << " while reading dictionary " << parentDict.name()
172  << exit(FatalIOError);
173 
174  return false;
175  }
176 }
177 
178 
180 (
181  const dictionary& parentDict,
182  primitiveEntry& entry,
183  Istream& is
184 )
185 {
186  const fileName rawFName(is);
187  const fileName fName
188  (
189  includeFileName(is.name().path(), rawFName, parentDict)
190  );
191 
192  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
193  ISstream& ifs = ifsPtr();
194 
195  if (ifs)
196  {
198  {
199  Info<< fName << endl;
200  }
201  entry.read(parentDict, ifs);
202  return true;
203  }
204  else
205  {
207  (
208  is
209  ) << "Cannot open include file "
210  << (ifs.name().size() ? ifs.name() : rawFName)
211  << " while reading dictionary " << parentDict.name()
212  << exit(FatalIOError);
213 
214  return false;
215  }
216 }
217 
218 
219 // ************************************************************************* //
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:667
A class for handling file names.
Definition: fileName.H:79
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:104
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:158
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
bool read(Istream &, const bool keepHeader=false)
Read dictionary from Istream, optionally keeping the header.
Definition: dictionaryIO.C:77
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1056
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:934
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:111
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:61
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:297
const fileOperation & fileHandler()
Get current file handler.
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:98
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:51
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:331
Macros for easy insertion into member function selection tables.
messageStream Info
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:253
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:1388
Namespace for OpenFOAM.
fileName path(UMean.rootPath()/UMean.caseName()/functionObjects::writeFile::outputPrefix/"graphs"/UMean.instance())
IOerror FatalIOError