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-2018 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 "IOstreams.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  parentDict.read(ifs);
120  return true;
121  }
122  else
123  {
125  (
126  is
127  ) << "Cannot open etc file "
128  << (ifs.name().size() ? ifs.name() : rawFName)
129  << " while reading dictionary " << parentDict.name()
130  << exit(FatalIOError);
131 
132  return false;
133  }
134 }
135 
136 
138 (
139  const dictionary& parentDict,
140  primitiveEntry& entry,
141  Istream& is
142 )
143 {
144  const fileName rawFName(is);
145  const fileName fName
146  (
147  includeEtcFileName(rawFName, parentDict)
148  );
149 
150  // IFstream ifs(fName);
151  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
152  ISstream& ifs = ifsPtr();
153 
154  if (ifs)
155  {
157  {
158  Info<< fName << endl;
159  }
160  entry.read(parentDict, ifs);
161  return true;
162  }
163  else
164  {
166  (
167  is
168  ) << "Cannot open etc file "
169  << (ifs.name().size() ? ifs.name() : rawFName)
170  << " while reading dictionary " << parentDict.name()
171  << exit(FatalIOError);
172 
173  return false;
174  }
175 }
176 
177 
178 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:125
A class for handling file names.
Definition: fileName.H:69
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:106
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:137
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:256
static bool log
Report which file is included to stdout.
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
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 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:103
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
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:87
#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.
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Namespace for OpenFOAM.
IOerror FatalIOError