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