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-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 "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,
159  primitiveEntry& entry,
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 // ************************************************************************* //
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
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:120
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
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
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
static fileName includeFileName(Istream &, const dictionary &)
Read the include fileName from Istream, expand and return.
Definition: includeEntry.C:71
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 log
Report which file is included to stdout.
Definition: includeEntry.H:94
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
Namespace for OpenFOAM.
IOerror FatalIOError