includeEtcEntry.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 | Copyright (C) 2015-2016 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 "IFstream.H"
29 #include "stringOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const Foam::word Foam::functionEntries::includeEtcEntry::typeName
35 (
36  Foam::functionEntries::includeEtcEntry::typeName_()
37 );
38 
39 // Don't lookup the debug switch here as the debug switch dictionary
40 // might include includeEtcEntry
41 int Foam::functionEntries::includeEtcEntry::debug(0);
42 
44 
45 
46 namespace Foam
47 {
48 namespace functionEntries
49 {
51  (
52  functionEntry,
53  includeEtcEntry,
54  execute,
55  dictionaryIstream
56  );
57 
59  (
60  functionEntry,
61  includeEtcEntry,
62  execute,
63  primitiveEntryIstream
64  );
65 }
66 }
67 
68 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
69 
70 Foam::fileName Foam::functionEntries::includeEtcEntry::includeEtcFileName
71 (
72  const fileName& f,
73  const dictionary& dict
74 )
75 {
76  fileName fName(f);
77 
78  // Substitute dictionary and environment variables.
79  // Allow empty substitutions.
80  stringOps::inplaceExpand(fName, dict, true, true);
81 
82  if (fName.empty() || fName.isAbsolute())
83  {
84  return fName;
85  }
86  else
87  {
88  // Search the etc directories for the file
89  return findEtcFile(fName);
90  }
91 }
92 
93 
94 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
95 
97 (
98  dictionary& parentDict,
99  Istream& is
100 )
101 {
102  const fileName rawFName(is);
103  const fileName fName
104  (
105  includeEtcFileName(rawFName, parentDict)
106  );
107  IFstream ifs(fName);
108 
109  if (ifs)
110  {
112  {
113  Info<< fName << endl;
114  }
115  parentDict.read(ifs);
116  return true;
117  }
118  else
119  {
121  (
122  is
123  ) << "Cannot open etc file "
124  << (ifs.name().size() ? ifs.name() : rawFName)
125  << " while reading dictionary " << parentDict.name()
126  << exit(FatalIOError);
127 
128  return false;
129  }
130 }
131 
132 
134 (
135  const dictionary& parentDict,
136  primitiveEntry& entry,
137  Istream& is
138 )
139 {
140  const fileName rawFName(is);
141  const fileName fName
142  (
143  includeEtcFileName(rawFName, parentDict)
144  );
145  IFstream ifs(fName);
146 
147  if (ifs)
148  {
150  {
151  Info<< fName << endl;
152  }
153  entry.read(parentDict, ifs);
154  return true;
155  }
156  else
157  {
159  (
160  is
161  ) << "Cannot open etc file "
162  << (ifs.name().size() ? ifs.name() : rawFName)
163  << " while reading dictionary " << parentDict.name()
164  << exit(FatalIOError);
165 
166  return false;
167  }
168 }
169 
170 
171 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
A class for handling file names.
Definition: fileName.H:69
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:253
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
static bool log
Report which file is included to stdout.
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
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.
A class for handling words, derived from string.
Definition: word.H:59
Functions to search &#39;etc&#39; directories for configuration files etc.
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: etcFiles.C:252
Input from file stream.
Definition: IFstream.H:81
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Macros for easy insertion into member function selection tables.
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil= '$')
Inplace expand occurences of variables according to the mapping.
Definition: stringOps.C:87
messageStream Info
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