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-2013 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  "functionEntries::includeEntry::includeEntry"
146  "(dictionary& parentDict, Istream&)",
147  is
148  ) << "Cannot open include file "
149  << (ifs.name().size() ? ifs.name() : rawFName)
150  << " while reading dictionary " << parentDict.name()
151  << exit(FatalIOError);
152 
153  return false;
154  }
155 }
156 
157 
159 (
160  const dictionary& parentDict,
161  primitiveEntry& entry,
162  Istream& is
163 )
164 {
165  const fileName rawFName(is);
166  const fileName fName
167  (
168  includeFileName(is.name().path(), rawFName, parentDict)
169  );
170  IFstream ifs(fName);
171 
172  if (ifs)
173  {
175  {
176  Info<< fName << endl;
177  }
178  entry.read(parentDict, ifs);
179  return true;
180  }
181  else
182  {
184  (
185  "functionEntries::includeEntry::includeEntry"
186  "(dictionary& parentDict, primitiveEntry&, Istream&)",
187  is
188  ) << "Cannot open include file "
189  << (ifs.name().size() ? ifs.name() : rawFName)
190  << " while reading dictionary " << parentDict.name()
191  << exit(FatalIOError);
192 
193  return false;
194  }
195 }
196 
197 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:126
const fileName & name() const
Return the name of the stream.
Definition: IFstream.H:116
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:120
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
A class for handling words, derived from string.
Definition: word.H:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
messageStream Info
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:287
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Input from file stream.
Definition: IFstream.H:81
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
Namespace for OpenFOAM.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
IOerror FatalIOError
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
static fileName includeFileName(Istream &, const dictionary &)
Read the include fileName from Istream, expand and return.
Definition: includeEntry.C:71
static bool report
Report which file is included to stdout.
Definition: includeEntry.H:94
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
fileName path(UMean.rootPath()/UMean.caseName()/"graphs"/UMean.instance())
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
virtual bool read(const dictionary &, Istream &)
Read tokens from the given stream.
A class for handling file names.
Definition: fileName.H:69
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.
Macros for easy insertion into member function selection tables.
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:325