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 | 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 "IFstream.H"
27 #include "OSspecific.H"
28 #include "gzstream.h"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 namespace Foam
33 {
34  defineTypeNameAndDebug(IFstream, 0);
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
41 :
42  ifPtr_(NULL),
43  compression_(IOstream::UNCOMPRESSED)
44 {
45  if (pathname.empty())
46  {
47  if (IFstream::debug)
48  {
49  InfoInFunction << "Cannot open null file " << endl;
50  }
51  }
52 
53  ifPtr_ = new ifstream(pathname.c_str());
54 
55  // If the file is compressed, decompress it before reading.
56  if (!ifPtr_->good() && isFile(pathname + ".gz", false))
57  {
58  if (IFstream::debug)
59  {
60  InfoInFunction << "Decompressing " << pathname + ".gz" << endl;
61  }
62 
63  delete ifPtr_;
64 
65  ifPtr_ = new igzstream((pathname + ".gz").c_str());
66 
67  if (ifPtr_->good())
68  {
69  compression_ = IOstream::COMPRESSED;
70  }
71  }
72 }
73 
74 
75 Foam::IFstreamAllocator::~IFstreamAllocator()
76 {
77  delete ifPtr_;
78 }
79 
80 
81 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
82 
84 (
85  const fileName& pathname,
86  streamFormat format,
87  versionNumber version
88 )
89 :
90  IFstreamAllocator(pathname),
91  ISstream
92  (
93  *ifPtr_,
94  "IFstream.sourceFile_",
95  format,
96  version,
97  IFstreamAllocator::compression_
98  ),
99  pathname_(pathname)
100 {
101  setClosed();
102 
103  setState(ifPtr_->rdstate());
104 
105  if (!good())
106  {
107  if (debug)
108  {
110  << "Could not open file for input" << endl << info() << endl;
111  }
112 
113  setBad();
114  }
115  else
116  {
117  setOpened();
118  }
119 
120  lineNumber_ = 1;
121 }
122 
123 
124 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
125 
127 {}
128 
129 
130 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
131 
133 {
134  if (!ifPtr_)
135  {
137  << "No stream allocated" << abort(FatalError);
138  }
139  return *ifPtr_;
140 }
141 
142 
143 const std::istream& Foam::IFstream::stdStream() const
144 {
145  if (!ifPtr_)
146  {
148  << "No stream allocated" << abort(FatalError);
149  }
150  return *ifPtr_;
151 }
152 
153 
155 {
156  // Print File data
157  os << "IFstream: ";
158  ISstream::print(os);
159 }
160 
161 
162 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
163 
165 {
166  if (!good())
167  {
168  // also checks .gz file
169  if (isFile(pathname_, true))
170  {
171  check("IFstream::operator()");
172  FatalIOError.exit();
173  }
174  else
175  {
177  << "file " << pathname_ << " does not exist"
178  << exit(FatalIOError);
179  }
180  }
181 
182  return const_cast<IFstream&>(*this);
183 }
184 
185 
186 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:231
A class for handling file names.
Definition: fileName.H:69
void setClosed()
Set stream closed.
Definition: IOstream.H:245
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:531
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
ISstream(istream &is, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct as wrapper around istream.
Definition: ISstreamI.H:31
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
bool isFile(const fileName &, const bool checkGzip=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:492
virtual istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:132
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:34
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setBad()
Set stream to be bad.
Definition: IOstream.H:487
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:86
errorManip< error > abort(error &err)
Definition: errorManip.H:131
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:171
defineTypeNameAndDebug(combustionModel, 0)
Input from file stream.
Definition: IFstream.H:81
~IFstream()
Destructor.
Definition: IFstream.C:126
void setOpened()
Set stream opened.
Definition: IOstream.H:239
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:331
Version number type.
Definition: IOstream.H:96
A std::istream with ability to handle compressed files.
Definition: IFstream.H:56
IFstream(const fileName &pathname, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from pathname.
Definition: IFstream.C:84
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:251
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IFstream.C:154
Namespace for OpenFOAM.
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.