includeEntry.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 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 #include "IOstreams.H"
32 #include "fileOperation.H"
33 
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
35 
36 const Foam::word Foam::functionEntries::includeEntry::typeName
37 (
38  Foam::functionEntries::includeEntry::typeName_()
39 );
40 
41 // Don't lookup the debug switch here as the debug switch dictionary
42 // might include includeEntry
43 int Foam::functionEntries::includeEntry::debug(0);
44 
46 
47 
48 namespace Foam
49 {
50 namespace functionEntries
51 {
53  (
54  functionEntry,
55  includeEntry,
56  execute,
57  dictionaryIstream
58  );
59 
61  (
62  functionEntry,
63  includeEntry,
64  execute,
65  primitiveEntryIstream
66  );
67 }
68 }
69 
70 // * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * * //
71 
73 (
74  Istream& is,
75  const dictionary& dict
76 )
77 {
78  fileName fName(is);
79  // Substitute dictionary and environment variables. Allow empty
80  // substitutions.
81  stringOps::inplaceExpand(fName, dict, true, true);
82 
83  if (fName.empty() || fName.isAbsolute())
84  {
85  return fName;
86  }
87  else
88  {
89  // relative name
90  return fileName(is.name()).path()/fName;
91  }
92 }
93 
94 
96 (
97  const fileName& dir,
98  const fileName& f,
99  const dictionary& dict
100 )
101 {
102  fileName fName(f);
103  // Substitute dictionary and environment variables. Allow empty
104  // substitutions.
105  stringOps::inplaceExpand(fName, dict, true, true);
106 
107  if (fName.empty() || fName.isAbsolute())
108  {
109  return fName;
110  }
111  else
112  {
113  // relative name
114  return dir/fName;
115  }
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
122 (
123  dictionary& parentDict,
124  Istream& is
125 )
126 {
127  const fileName rawFName(is);
128  const fileName fName
129  (
130  includeFileName(is.name().path(), rawFName, parentDict)
131  );
132 
133  // IFstream ifs(fName);
134  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
135  ISstream& ifs = ifsPtr();
136 
137  if (ifs)
138  {
140  {
141  Info<< fName << endl;
142  }
143  parentDict.read(ifs);
144  return true;
145  }
146  else
147  {
149  (
150  is
151  ) << "Cannot open include file "
152  << (ifs.name().size() ? ifs.name() : rawFName)
153  << " while reading dictionary " << parentDict.name()
154  << exit(FatalIOError);
155 
156  return false;
157  }
158 }
159 
160 
162 (
163  const dictionary& parentDict,
164  primitiveEntry& entry,
165  Istream& is
166 )
167 {
168  const fileName rawFName(is);
169  const fileName fName
170  (
171  includeFileName(is.name().path(), rawFName, parentDict)
172  );
173 
174  // IFstream ifs(fName);
175  autoPtr<ISstream> ifsPtr(fileHandler().NewIFstream(fName));
176  ISstream& ifs = ifsPtr();
177 
178  if (ifs)
179  {
181  {
182  Info<< fName << endl;
183  }
184  entry.read(parentDict, ifs);
185  return true;
186  }
187  else
188  {
190  (
191  is
192  ) << "Cannot open include file "
193  << (ifs.name().size() ? ifs.name() : rawFName)
194  << " while reading dictionary " << parentDict.name()
195  << exit(FatalIOError);
196 
197  return false;
198  }
199 }
200 
201 // ************************************************************************* //
bool read(Istream &)
Read dictionary from Istream.
Definition: dictionaryIO.C:125
A class for handling file names.
Definition: fileName.H:69
virtual const fileName & name() const
Return the name of the stream.
Definition: ISstream.H:106
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:256
static bool execute(dictionary &parentDict, Istream &)
Execute the functionEntry in a sub-dict context.
Definition: includeEntry.C:122
addToMemberFunctionSelectionTable(functionEntry, calcEntry, execute, dictionaryIstream)
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
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.
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
bool isAbsolute() const
Return true if file name is absolute.
Definition: fileName.C:57
A class for handling words, derived from string.
Definition: word.H:59
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
const fileOperation & fileHandler()
Get current file handler.
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:73
Generic input stream.
Definition: ISstream.H:51
string & inplaceExpand(string &, const HashTable< string, word, string::hash > &mapping, const char sigil='$')
Inplace expand occurrences of variables according to the mapping.
Definition: stringOps.C:87
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Macros for easy insertion into member function selection tables.
messageStream Info
fileName path() const
Return directory path name (part before last /)
Definition: fileName.C:249
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: PtrList.H:52
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
Namespace for OpenFOAM.
IOerror FatalIOError