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-2020 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  detectedCompression_(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_.reset(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  allocatedPtr_.reset(new igzstream((pathname + ".gz").c_str(), mode));
69 
70  if (allocatedPtr_->good())
71  {
73  }
74  }
75 }
76 
77 
78 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
79 
81 (
82  const fileName& pathname,
83  IOstreamOption streamOpt
84 )
85 :
86  Detail::IFstreamAllocator(pathname),
87  ISstream(*allocatedPtr_, pathname, streamOpt)
88 {
90 
91  setClosed();
92 
93  setState(allocatedPtr_->rdstate());
94 
95  if (!good())
96  {
97  if (debug)
98  {
100  << "Could not open file " << pathname
101  << " for input" << nl << info() << Foam::endl;
102  }
103 
104  setBad();
105  }
106  else
107  {
108  setOpened();
109  }
110 
111  lineNumber_ = 1;
112 }
113 
114 
115 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
116 
118 {
119  if (!allocatedPtr_)
120  {
122  << "No stream allocated"
123  << abort(FatalError);
124  }
125  return *allocatedPtr_;
126 }
127 
128 
129 const std::istream& Foam::IFstream::stdStream() const
130 {
131  if (!allocatedPtr_)
132  {
134  << "No stream allocated"
135  << abort(FatalError);
136  }
137  return *allocatedPtr_;
138 }
139 
140 
142 {
143  lineNumber_ = 1; // Reset line number
144 
145  igzstream* gzPtr = nullptr;
146 
147  try
148  {
149  gzPtr = dynamic_cast<igzstream*>(allocatedPtr_.get());
150  }
151  catch (const std::bad_cast&)
152  {
153  gzPtr = nullptr;
154  }
155 
156  if (gzPtr)
157  {
158  // Need special treatment for gzstream.
159  gzPtr->close();
160  gzPtr->clear();
161  gzPtr->open((this->name() + ".gz").c_str());
162 
163  setState(gzPtr->rdstate());
164  }
165  else
166  {
168  }
169 }
170 
171 
173 {
174  os << "IFstream: ";
175  ISstream::print(os);
176 }
177 
178 
179 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
180 
182 {
183  if (!good())
184  {
185  // Also checks .gz file
186  if (isFile(this->name(), true))
187  {
188  check(FUNCTION_NAME);
189  FatalIOError.exit();
190  }
191  else
192  {
194  << "file " << this->name() << " does not exist"
195  << exit(FatalIOError);
196  }
197  }
198 
199  return const_cast<IFstream&>(*this);
200 }
201 
202 
203 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::Detail::IFstreamAllocator::detectedCompression_
IOstream::compressionType detectedCompression_
The detected compression type.
Definition: IFstream.H:69
Foam::ISstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:36
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:320
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:85
Foam::IFstream::print
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: IFstream.C:172
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 a standard (STL) stream.
Definition: ISstream.H:55
Foam::IFstream::operator()
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:181
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:350
Foam::ISstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:874
Foam::mode
mode_t mode(const fileName &name, const bool followLink=true)
Return the file mode, normally following symbolic links.
Definition: MSwindows.C:564
Foam::Detail::IFstreamAllocator
A std::istream with the ability to handle compressed files.
Definition: IFstream.H:59
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
Foam::IOstreamOption
The IOstreamOption is a simple container for options an IOstream can normally have.
Definition: IOstreamOption.H:63
IFstream.H
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::IFstream::IFstream
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname.
Definition: IFstream.C:81
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::IFstream::stdStream
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:117
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:247
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::IOstreamOption::COMPRESSED
compression = true
Definition: IOstreamOption.H:80
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::IFstream::rewind
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: IFstream.C:141
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:265
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:392
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstreamOption::compression
compressionType compression() const noexcept
Get the stream compression.
Definition: IOstreamOption.H:315
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(combustionModel, 0)
Foam::Detail::IFstreamAllocator::allocatedPtr_
std::unique_ptr< std::istream > allocatedPtr_
The allocated stream pointer (ifstream or igzstream).
Definition: IFstream.H:66