includeEtcEntry.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) 2015-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 "includeEtcEntry.H"
27 #include "etcFiles.H"
28 #include "stringOps.H"
30 #include "IOobject.H"
31 #include "fileOperation.H"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 const Foam::word Foam::functionEntries::includeEtcEntry::typeName
36 (
37  Foam::functionEntries::includeEtcEntry::typeName_()
38 );
39 
40 // Don't lookup the debug switch here as the debug switch dictionary
41 // might include includeEtcEntry
42 int Foam::functionEntries::includeEtcEntry::debug(0);
43 
45 
46 
47 namespace Foam
48 {
49 namespace functionEntries
50 {
52  (
53  functionEntry,
54  includeEtcEntry,
55  execute,
56  dictionaryIstream
57  );
58 
60  (
61  functionEntry,
62  includeEtcEntry,
63  execute,
64  primitiveEntryIstream
65  );
66 }
67 }
68 
69 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
70 
71 Foam::fileName Foam::functionEntries::includeEtcEntry::includeEtcFileName
72 (
73  const fileName& f,
74  const dictionary& dict
75 )
76 {
77  fileName fName(f);
78 
79  // Substitute dictionary and environment variables.
80  // Allow empty substitutions.
81  stringOps::inplaceExpand(fName, dict, true, true);
82 
83  if (fName.empty() || fName.isAbsolute())
84  {
85  return fName;
86  }
87  else
88  {
89  // Search the etc directories for the file
90  return findEtcFile(fName);
91  }
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
98 (
99  dictionary& parentDict,
100  Istream& is
101 )
102 {
103  const fileName rawFName(is);
104  const fileName fName
105  (
106  includeEtcFileName(rawFName, parentDict)
107  );
108 
109  // IFstream ifs(fName);
110  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
111  ISstream& ifs = ifsPtr();
112 
113  if (ifs)
114  {
116  {
117  Info<< fName << endl;
118  }
119 
120  // Cache the FoamFile entry if present
121  dictionary foamFileDict;
122  if (parentDict.found(IOobject::foamFile))
123  {
124  foamFileDict = parentDict.subDict(IOobject::foamFile);
125  }
126 
127  // Read and clear the FoamFile entry
128  parentDict.read(ifs);
129 
130  // Reinstate original FoamFile entry
131  if (foamFileDict.size() != 0)
132  {
133  dictionary parentDictTmp(parentDict);
134  parentDict.clear();
135  parentDict.add(IOobject::foamFile, foamFileDict);
136  parentDict += parentDictTmp;
137  }
138 
139  return true;
140  }
141  else
142  {
144  (
145  is
146  ) << "Cannot open etc file "
147  << (ifs.name().size() ? ifs.name() : rawFName)
148  << " while reading dictionary " << parentDict.name()
149  << exit(FatalIOError);
150 
151  return false;
152  }
153 }
154 
155 
157 (
158  const dictionary& parentDict,
159  primitiveEntry& entry,
160  Istream& is
161 )
162 {
163  const fileName rawFName(is);
164  const fileName fName
165  (
166  includeEtcFileName(rawFName, parentDict)
167  );
168 
169  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
170  ISstream& ifs = ifsPtr();
171 
172  if (ifs)
173  {
175  {
176  Info<< fName << endl;
177  }
178  entry.read(parentDict, ifs);
179  return true;
180  }
181  else
182  {
184  (
185  is
186  ) << "Cannot open etc file "
187  << (ifs.name().size() ? ifs.name() : rawFName)
188  << " while reading dictionary " << parentDict.name()
189  << exit(FatalIOError);
190 
191  return false;
192  }
193 }
194 
195 
196 // ************************************************************************* //
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 log
Report which file is included to stdout.
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
A class for handling words, derived from string.
Definition: word.H:59
Functions to search &#39;etc&#39; directories for configuration files etc.
const fileOperation & fileHandler()
Get current file handler.
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: etcFiles.C:252
static constexpr const char * foamFile
Keyword for the FoamFile header sub-dictionary.
Definition: IOobject.H:98
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
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.
void clear()
Clear the dictionary.
Definition: dictionary.C:1388
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Namespace for OpenFOAM.
IOerror FatalIOError