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-------------------------------------------------------------------------------
11License
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
34namespace 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
95{
96 std::istream* ptr = ifstreamPointer::get();
97
98 if (!ptr)
99 {
101 << "No stream allocated\n"
102 << abort(FatalError);
103 }
104
105 return *ptr;
106}
107
108
109const std::istream& Foam::IFstream::stdStream() const
110{
111 const std::istream* ptr = ifstreamPointer::get();
112
113 if (!ptr)
114 {
116 << "No stream allocated\n"
117 << abort(FatalError);
118 }
119
120 return *ptr;
121}
122
123
125{
127 {
128 lineNumber_ = 1; // Reset line number
129 ifstreamPointer::reopen_gz(this->name() + ".gz");
130 setState(ifstreamPointer::get()->rdstate());
131 }
132 else
133 {
135 }
136}
137
138
140{
141 os << "IFstream: ";
143}
144
145
146// * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
147
149{
150 if (!good())
151 {
152 // Also checks .gz file
153 if (isFile(this->name(), true))
154 {
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// ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Input from file stream, using an ISstream.
Definition: IFstream.H:57
IFstream & operator()() const
Return a non-const reference to const IFstream.
Definition: IFstream.C:148
virtual const fileName & name() const
Read/write access to the name of the stream.
Definition: ISstream.H:113
virtual std::istream & stdStream()
Access to underlying std::istream.
Definition: IFstream.C:94
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: IFstream.C:124
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:243
The IOstreamOption is a simple container for options an IOstream can normally have.
compressionType compression() const noexcept
Get the stream compression.
@ COMPRESSED
compression = true
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
bool opened() const noexcept
True if stream has been opened.
Definition: IOstream.H:221
void setBad()
Set stream state to be 'bad'.
Definition: IOstream.H:369
label lineNumber_
The file line.
Definition: IOstream.H:121
InfoProxy< IOstream > info() const
Return info proxy.
Definition: IOstream.H:413
void setClosed() noexcept
Set stream closed.
Definition: IOstream.H:135
void setState(std::ios_base::iostate state) noexcept
Set stream state.
Definition: IOstream.H:141
void setOpened() noexcept
Set stream opened.
Definition: IOstream.H:129
Generic input stream using a standard (STL) stream.
Definition: ISstream.H:58
virtual void rewind()
Rewind the stream so that it may be read again.
Definition: ISstream.C:1064
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling file names.
Definition: fileName.H:76
A wrapped std::ifstream with possible compression handling (igzstream) that behaves much like a std::...
void reopen_gz(const std::string &pathname_gz)
Special 'rewind' method for compressed stream.
std::istream * get() noexcept
The stream pointer (ifstream or igzstream)
IOstreamOption::compressionType whichCompression() const
Which compression type?
scalar print()
Print to screen.
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
Definition: className.H:121
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:473
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
#define FUNCTION_NAME
#define InfoInFunction
Report an information message using Foam::Info.
Namespace for OpenFOAM.
static void check(const int retVal, const char *what)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
errorManip< error > abort(error &err)
Definition: errorManip.H:144
IOerror FatalIOError
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: MSwindows.C:666
error FatalError
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130