IFstream.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IFstream.H"
30 #include "OSspecific.H"
31 #include "gzstream.h"
32 
33 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34 
35 namespace Foam
36 {
37  defineTypeNameAndDebug(IFstream, 0);
38 }
39 
40 
41 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
42 
44 :
45  allocatedPtr_(nullptr),
46  compression_(IOstream::UNCOMPRESSED)
47 {
48  if (pathname.empty())
49  {
50  if (IFstream::debug)
51  {
52  InfoInFunction << "Cannot open null file " << endl;
53  }
54  }
55 
56  const std::ios_base::openmode mode(std::ios_base::in|std::ios_base::binary);
57 
58  allocatedPtr_ = new std::ifstream(pathname, mode);
59 
60  // If the file is compressed, decompress it before reading.
61  if (!allocatedPtr_->good() && isFile(pathname + ".gz", false))
62  {
63  if (IFstream::debug)
64  {
65  InfoInFunction << "Decompressing " << pathname + ".gz" << endl;
66  }
67 
68  delete allocatedPtr_;
69  allocatedPtr_ = new igzstream((pathname + ".gz").c_str(), mode);
70 
71  if (allocatedPtr_->good())
72  {
74  }
75  }
76 }
77 
78 
79 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
80 
82 {
83  deallocate();
84 }
85 
86 
87 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
88 
90 {
91  if (allocatedPtr_)
92  {
93  delete allocatedPtr_;
94  allocatedPtr_ = nullptr;
95  }
96 }
97 
98 
99 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
100 
102 (
103  const fileName& pathname,
106 )
107 :
108  Detail::IFstreamAllocator(pathname),
109  ISstream
110  (
111  *allocatedPtr_,
112  pathname,
113  format,
114  version,
116  )
117 {
118  setClosed();
119 
120  setState(allocatedPtr_->rdstate());
121 
122  if (!good())
123  {
124  if (debug)
125  {
127  << "Could not open file " << pathname
128  << " for input" << nl << info() << Foam::endl;
129  }
130 
131  setBad();
132  }
133  else
134  {
135  setOpened();
136  }
137 
138  lineNumber_ = 1;
139 }
140 
141 
142 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
143 
145 {
146  if (!allocatedPtr_)
147  {
149  << "No stream allocated"
150  << abort(FatalError);
151  }
152  return *allocatedPtr_;
153 }
154 
155 
156 const std::istream& Foam::IFstream::stdStream() const
157 {
158  if (!allocatedPtr_)
159  {
161  << "No stream allocated"
162  << abort(FatalError);
163  }
164  return *allocatedPtr_;
165 }
166 
167 
169 {
170  lineNumber_ = 1; // Reset line number
171 
172  igzstream* gzPtr = nullptr;
173 
174  try
175  {
176  gzPtr = dynamic_cast<igzstream*>(allocatedPtr_);
177  }
178  catch (const std::bad_cast&)
179  {
180  gzPtr = nullptr;
181  }
182 
183  if (gzPtr)
184  {
185  // Need special treatment for gzstream.
186  gzPtr->close();
187  gzPtr->clear();
188  gzPtr->open((this->name() + ".gz").c_str());
189 
190  setState(gzPtr->rdstate());
191  }
192  else
193  {
195  }
196 }
197 
198 
200 {
201  os << "IFstream: ";
202  ISstream::print(os);
203 }
204 
205 
206 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
207 
209 {
210  if (!good())
211  {
212  // Also checks .gz file
213  if (isFile(this->name(), true))
214  {
215  check(FUNCTION_NAME);
216  FatalIOError.exit();
217  }
218  else
219  {
221  << "file " << this->name() << " does not exist"
222  << exit(FatalIOError);
223  }
224  }
225 
226  return const_cast<IFstream&>(*this);
227 }
228 
229 
230 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::ISstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:36
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:316
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::IFstream
Input from file stream, using an ISstream.
Definition: IFstream.H:97
Foam::Detail::IFstreamAllocator::~IFstreamAllocator
~IFstreamAllocator()
Destructor.
Definition: IFstream.C:81
Foam::IFstream::print
virtual void print(Ostream &os) const
Print description of IOstream to Ostream.
Definition: IFstream.C:199
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::ISstream
Generic input stream using standard (STL) streams.
Definition: ISstream.H:54
Foam::IFstream::operator()
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:208
Foam::isFile
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: MSwindows.C:658
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:852
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
format
word format(conversionProperties.get< word >("format"))
Foam::Detail::IFstreamAllocator::allocatedPtr_
std::istream * allocatedPtr_
The allocated stream pointer (ifstream or igzstream).
Definition: IFstream.H:67
Foam::IFstream::IFstream
IFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from pathname.
Definition: IFstream.C:102
Foam::IOstreamOption::versionNumber
Representation of a major/minor version number.
Definition: IOstreamOption.H:79
Foam::Detail::IFstreamAllocator
A std::istream with the ability to handle compressed files.
Definition: IFstream.H:60
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
IFstream.H
Foam::Detail::IFstreamAllocator::deallocate
void deallocate()
Delete the stream pointer.
Definition: IFstream.C:89
Foam::IOstreamOption::streamFormat
streamFormat
Data format (ascii | binary)
Definition: IOstreamOption.H:64
Foam::FatalError
error FatalError
Foam::Detail::IFstreamAllocator::IFstreamAllocator
IFstreamAllocator(const fileName &pathname)
Construct from pathname.
Definition: IFstream.C:43
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
Foam::IFstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:144
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:175
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:74
Foam::Detail::IFstreamAllocator::compression_
IOstream::compressionType compression_
The requested compression type.
Definition: IFstream.H:70
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::IFstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: IFstream.C:168
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:261
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:375
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)