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 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 "dictionary.H"
28 #include "IFstream.H"
30 #include "stringOps.H"
31 #include "OSspecific.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 
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  IFstream ifs(fName);
109 
110  if (ifs)
111  {
113  {
114  Info<< fName << endl;
115  }
116  parentDict.read(ifs);
117  return true;
118  }
119  else
120  {
122  (
123  is
124  ) << "Cannot open etc file "
125  << (ifs.name().size() ? ifs.name() : rawFName)
126  << " while reading dictionary " << parentDict.name()
127  << exit(FatalIOError);
128 
129  return false;
130  }
131 }
132 
133 
135 (
136  const dictionary& parentDict,
138  Istream& is
139 )
140 {
141  const fileName rawFName(is);
142  const fileName fName
143  (
144  includeEtcFileName(rawFName, parentDict)
145  );
146  IFstream ifs(fName);
147 
148  if (ifs)
149  {
151  {
152  Info<< fName << endl;
153  }
154  entry.read(parentDict, ifs);
155  return true;
156  }
157  else
158  {
160  (
161  is
162  ) << "Cannot open etc file "
163  << (ifs.name().size() ? ifs.name() : rawFName)
164  << " while reading dictionary " << parentDict.name()
165  << exit(FatalIOError);
166 
167  return false;
168  }
169 }
170 
171 
172 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
Foam::findEtcFile
fileName findEtcFile(const fileName &, bool mandatory=false)
Search for a file using findEtcFiles.
Definition: POSIX.C:404
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::IFstream::name
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
Foam::primitiveEntry
A keyword and a list of tokens is a 'primitiveEntry'. An primitiveEntry can be read,...
Definition: primitiveEntry.H:62
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::fileName::isAbsolute
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::functionEntries::includeEtcEntry::includeEtcFileName
static fileName includeEtcFileName(const fileName &, const dictionary &)
Expand include fileName and return.
Definition: includeEtcEntry.C:72
includeEtcEntry.H
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:57
Foam::Info
messageStream Info
Foam::functionEntries::includeEtcEntry::report
static bool report
Report which file is included to stdout.
Definition: includeEtcEntry.H:102
addToMemberFunctionSelectionTable.H
Macros for easy insertion into member function selection tables.
IFstream.H
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::functionEntries::addToMemberFunctionSelectionTable
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
f
labelList f(nPoints)
dictionary.H
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::dictionary::read
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
stringOps.H
Foam::stringOps::inplaceExpand
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:86
Foam::functionEntries::includeEtcEntry::execute
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEtcEntry.C:98