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