IFstream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 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 {
35 }
36 
37 
38 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
39 
40 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& filePath)
41 :
42  ifPtr_(nullptr),
43  compression_(IOstream::UNCOMPRESSED)
44 {
45  if (filePath.empty())
46  {
47  if (IFstream::debug)
48  {
49  InfoInFunction << "Cannot open null file " << endl;
50  }
51  }
52 
53  ifPtr_ = new ifstream(filePath.c_str());
54 
55  // If the file is compressed, decompress it before reading.
56  if (!ifPtr_->good())
57  {
58  if (isFile(filePath + ".gz", false, false))
59  {
60  delete ifPtr_;
61 
62  if (IFstream::debug)
63  {
64  InfoInFunction << "Decompressing " << filePath + ".gz" << endl;
65  }
66 
67  ifPtr_ = new igzstream((filePath + ".gz").c_str());
68 
69  if (ifPtr_->good())
70  {
71  compression_ = IOstream::COMPRESSED;
72  }
73  }
74  else if (isFile(filePath + ".orig", false, false))
75  {
76  delete ifPtr_;
77 
78  ifPtr_ = new ifstream((filePath + ".orig").c_str());
79  }
80  }
81 }
82 
83 
84 Foam::IFstreamAllocator::~IFstreamAllocator()
85 {
86  delete ifPtr_;
87 }
88 
89 
90 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
91 
93 (
94  const fileName& filePath,
96  versionNumber version
97 )
98 :
99  IFstreamAllocator(filePath),
100  ISstream
101  (
102  *ifPtr_,
103  "IFstream.sourceFile_",
104  format,
105  version,
106  IFstreamAllocator::compression_
107  ),
108  filePath_(filePath)
109 {
110  setClosed();
111 
112  setState(ifPtr_->rdstate());
113 
114  if (!good())
115  {
116  if (debug)
117  {
119  << "Could not open file for input" << endl << info() << endl;
120  }
121 
122  setBad();
123  }
124  else
125  {
126  setOpened();
127  }
128 
129  lineNumber_ = 1;
130 }
131 
132 
133 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
134 
136 {}
137 
138 
139 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
140 
142 {
143  if (!ifPtr_)
144  {
146  << "No stream allocated" << abort(FatalError);
147  }
148  return *ifPtr_;
149 }
150 
151 
152 const std::istream& Foam::IFstream::stdStream() const
153 {
154  if (!ifPtr_)
155  {
157  << "No stream allocated" << abort(FatalError);
158  }
159  return *ifPtr_;
160 }
161 
162 
164 {
165  // Print File data
166  os << "IFstream: ";
167  ISstream::print(os);
168 }
169 
170 
171 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
172 
174 {
175  if (!good())
176  {
177  // also checks variants
178  if (isFile(filePath_, true, true))
179  {
180  check("IFstream::operator()");
181  FatalIOError.exit();
182  }
183  else
184  {
186  << "file " << filePath_ << " does not exist"
187  << exit(FatalIOError);
188  }
189  }
190 
191  return const_cast<IFstream&>(*this);
192 }
193 
194 
195 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
A std::istream with ability to handle compressed files.
Definition: IFstream.H:57
friend class IFstream
Definition: IFstream.H:58
Input from file stream.
Definition: IFstream.H:85
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:173
virtual istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:141
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IFstream.C:163
~IFstream()
Destructor.
Definition: IFstream.C:135
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:158
Version number type.
Definition: IOstream.H:97
streamFormat
Enumeration for the format of data in the stream.
Definition: IOstream.H:87
void setBad()
Set stream to be bad.
Definition: IOstream.H:484
label lineNumber_
Definition: IOstream.H:228
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
void setClosed()
Set stream closed.
Definition: IOstream.H:242
void setOpened()
Set stream opened.
Definition: IOstream.H:236
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:528
Generic input stream.
Definition: ISstream.H:55
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:34
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A class for handling file names.
Definition: fileName.H:82
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:318
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
#define InfoInFunction
Report an information message using Foam::Info.
Namespace for OpenFOAM.
bool isFile(const fileName &, const bool checkVariants=true, const bool followLink=true)
Does the name exist as a file in the file system?
Definition: POSIX.C:555
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
errorManip< error > abort(error &err)
Definition: errorManip.H:131
defineTypeNameAndDebug(combustionModel, 0)
IOerror FatalIOError
error FatalError
word format(conversionProperties.lookup("format"))