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 {
34  defineTypeNameAndDebug(IFstream, 0);
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,
95  streamFormat format,
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 // ************************************************************************* //
label lineNumber_
Definition: IOstream.H:228
A class for handling file names.
Definition: fileName.H:79
void setClosed()
Set stream closed.
Definition: IOstream.H:242
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:323
virtual bool check(const char *operation) const
Check IOstream status for given operation.
Definition: IOstream.C:92
ISstream(istream &is, const string &name, streamFormat format=ASCII, versionNumber version=currentVersion, compressionType compression=UNCOMPRESSED)
Construct as wrapper around istream.
Definition: ISstreamI.H:31
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
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: SstreamsPrint.C:34
IFstream(const fileName &filePath, streamFormat format=ASCII, versionNumber version=currentVersion)
Construct from filePath.
Definition: IFstream.C:93
virtual istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:141
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
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:484
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:54
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:170
defineTypeNameAndDebug(combustionModel, 0)
Input from file stream.
Definition: IFstream.H:81
~IFstream()
Destructor.
Definition: IFstream.C:135
void setOpened()
Set stream opened.
Definition: IOstream.H:236
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:335
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:173
Version number type.
Definition: IOstream.H:96
A std::istream with ability to handle compressed files.
Definition: IFstream.H:56
void setState(ios_base::iostate state)
Set stream state.
Definition: IOstream.H:248
virtual void print(Ostream &) const
Print description of IOstream to Ostream.
Definition: IFstream.C:163
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:528
Namespace for OpenFOAM.
IOerror FatalIOError
#define InfoInFunction
Report an information message using Foam::Info.