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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "IFstream.H"
30 #include "OSspecific.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
34 namespace Foam
35 {
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 (
44  const fileName& pathname,
45  IOstreamOption streamOpt
46 )
47 :
48  Foam::ifstreamPointer(pathname),
49  ISstream(*(ifstreamPointer::get()), pathname, streamOpt)
50 {
52 
53  setClosed();
54 
55  setState(ifstreamPointer::get()->rdstate());
56 
57  if (good())
58  {
59  setOpened();
60  }
61  else
62  {
63  setBad();
64  }
65 
66  lineNumber_ = 1;
67 
68  if (debug)
69  {
70  if (pathname.empty())
71  {
73  << "Cannot open empty file name"
74  << Foam::endl;
75  }
77  {
79  << "Decompressing " << (this->name() + ".gz") << Foam::endl;
80  }
81 
82  if (!opened())
83  {
85  << "Could not open file " << pathname
86  << " for input\n" << info() << Foam::endl;
87  }
88  }
89 }
90 
91 
92 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
93 
94 std::istream& Foam::IFstream::stdStream()
95 {
96  std::istream* ptr = ifstreamPointer::get();
97 
98  if (!ptr)
99  {
101  << "No stream allocated\n"
103  }
104 
105  return *ptr;
106 }
107 
108 
109 const std::istream& Foam::IFstream::stdStream() const
110 {
111  const std::istream* ptr = ifstreamPointer::get();
112 
113  if (!ptr)
114  {
116  << "No stream allocated\n"
118  }
119 
120  return *ptr;
121 }
122 
123 
125 {
127  {
128  lineNumber_ = 1; // Reset line number
130  setState(ifstreamPointer::get()->rdstate());
131  }
132  else
133  {
135  }
136 }
137 
138 
139 void Foam::IFstream::print(Ostream& os) const
140 {
141  os << "IFstream: ";
143 }
144 
145 
146 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
147 
149 {
150  if (!good())
151  {
152  // Also checks .gz file
153  if (Foam::isFile(this->name(), true))
154  {
156  FatalIOError.exit();
157  }
158  else
159  {
161  << "file " << this->name() << " does not exist"
162  << exit(FatalIOError);
163  }
164  }
165 
166  return const_cast<IFstream&>(*this);
167 }
168 
169 
170 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
label lineNumber_
The file line.
Definition: IOstream.H:140
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:265
IFstream(const fileName &pathname, IOstreamOption streamOpt=IOstreamOption())
Construct from pathname, default or specified stream options.
Definition: IFstream.C:36
A class for handling file names.
Definition: fileName.H:71
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:158
virtual const fileName & name() const
The name of the input serial stream. (eg, the name of the Fstream file name)
Definition: ISstream.H:136
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:578
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:166
IOstreamOption::compressionType whichCompression() const
Which compression type?
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:87
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:487
InfoProxy< IOstream > info() const noexcept
Return info proxy, used to print IOstream information to a stream.
Definition: IOstream.H:517
void reopen_gz(const std::string &pathname)
Special &#39;rewind&#39; method for compressed stream.
A simple container for options an IOstream can normally have.
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: IFstream.C:117
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setBad()
Set stream state to be &#39;bad&#39;.
Definition: IOstream.H:459
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:52
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
virtual void print(Ostream &os) const
Print stream description to Ostream.
Definition: SstreamsPrint.C:30
errorManip< error > abort(error &err)
Definition: errorManip.H:139
virtual void print(Ostream &os) const
Print stream description.
Definition: IFstream.C:132
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:239
int debug
Static debugging option.
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
defineTypeNameAndDebug(combustionModel, 0)
compressionType compression() const noexcept
Get the stream compression.
Input from file stream, using an ISstream.
Definition: IFstream.H:49
static void check(const int retVal, const char *what)
bool isFile(const fileName &name, const bool checkGzip=true, const bool followLink=true)
Does the name exist as a FILE in the file system?
Definition: POSIX.C:877
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:51
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:607
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:281
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:141
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1078
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:150
Namespace for OpenFOAM.
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...
#define InfoInFunction
Report an information message using Foam::Info.