includeEntry.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) 2011-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 "includeEntry.H"
27 #include "dictionary.H"
28 #include "IFstream.H"
30 #include "stringOps.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 const Foam::word Foam::functionEntries::includeEntry::typeName
35 (
36  Foam::functionEntries::includeEntry::typeName_()
37 );
38 
39 // Don't lookup the debug switch here as the debug switch dictionary
40 // might include includeEntry
41 int Foam::functionEntries::includeEntry::debug(0);
42 
44 
45 
46 namespace Foam
47 {
48 namespace functionEntries
49 {
51  (
52  functionEntry,
53  includeEntry,
54  execute,
55  dictionaryIstream
56  );
57 
59  (
60  functionEntry,
61  includeEntry,
62  execute,
63  primitiveEntryIstream
64  );
65 }
66 }
67 
68 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
69 
71 (
72  Istream& is,
73  const dictionary& dict
74 )
75 {
76  fileName fName(is);
77  // Substitute dictionary and environment variables. Allow empty
78  // substitutions.
79  stringOps::inplaceExpand(fName, dict, true, true);
80 
81  if (fName.empty() || fName.isAbsolute())
82  {
83  return fName;
84  }
85  else
86  {
87  // relative name
88  return fileName(is.name()).path()/fName;
89  }
90 }
91 
92 
94 (
95  const fileName& dir,
96  const fileName& f,
97  const dictionary& dict
98 )
99 {
100  fileName fName(f);
101  // Substitute dictionary and environment variables. Allow empty
102  // substitutions.
103  stringOps::inplaceExpand(fName, dict, true, true);
104 
105  if (fName.empty() || fName.isAbsolute())
106  {
107  return fName;
108  }
109  else
110  {
111  // relative name
112  return dir/fName;
113  }
114 }
115 
116 
117 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
118 
120 (
121  dictionary& parentDict,
122  Istream& is
123 )
124 {
125  const fileName rawFName(is);
126  const fileName fName
127  (
128  includeFileName(is.name().path(), rawFName, parentDict)
129  );
130  IFstream ifs(fName);
131 
132  if (ifs)
133  {
135  {
136  Info<< fName << endl;
137  }
138  parentDict.read(ifs);
139  return true;
140  }
141  else
142  {
144  (
145  is
146  ) << "Cannot open include 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,
160  Istream& is
161 )
162 {
163  const fileName rawFName(is);
164  const fileName fName
165  (
166  includeFileName(is.name().path(), rawFName, parentDict)
167  );
168  IFstream ifs(fName);
169 
170  if (ifs)
171  {
173  {
174  Info<< fName << endl;
175  }
176  entry.read(parentDict, ifs);
177  return true;
178  }
179  else
180  {
182  (
183  is
184  ) << "Cannot open include file "
185  << (ifs.name().size() ? ifs.name() : rawFName)
186  << " while reading dictionary " << parentDict.name()
187  << exit(FatalIOError);
188 
189  return false;
190  }
191 }
192 
193 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
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::path
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:293
Foam::fileName::isAbsolute
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
Foam::functionEntries::includeEntry::execute
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:120
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::functionEntries::includeEntry::report
static bool report
Report which file is included to stdout.
Definition: includeEntry.H:94
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
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)
includeEntry.H
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
Foam::functionEntries::includeEntry::includeFileName
static fileName includeFileName(Istream &, const dictionary &)
Read the include fileName from Istream, expand and return.
Definition: includeEntry.C:71
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::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297