Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / db / IOstreams / Fstreams / IFstream.C
blob287e0259b547567833b9f155abd4ebdcd573719d
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "IFstream.H"
27 #include "OSspecific.H"
28 #include "gzstream.h"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::IFstream, 0);
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
39     ifPtr_(NULL),
40     compression_(IOstream::UNCOMPRESSED)
42     if (pathname.empty())
43     {
44         if (IFstream::debug)
45         {
46             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
47                     "cannot open null file " << endl;
48         }
49     }
51     ifPtr_ = new ifstream(pathname.c_str());
53     // If the file is compressed, decompress it before reading.
54     if (!ifPtr_->good() && isFile(pathname + ".gz", false))
55     {
56         if (IFstream::debug)
57         {
58             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
59                     "decompressing " << pathname + ".gz" << endl;
60         }
62         delete ifPtr_;
64         ifPtr_ = new igzstream((pathname + ".gz").c_str());
66         if (ifPtr_->good())
67         {
68             compression_ = IOstream::COMPRESSED;
69         }
70     }
74 Foam::IFstreamAllocator::~IFstreamAllocator()
76     delete ifPtr_;
80 std::istream& Foam::IFstreamAllocator::stdStream()
82     if (!ifPtr_)
83     {
84         FatalErrorIn("IFstreamAllocator::stdStream()")
85             << "No stream allocated" << abort(FatalError);
86     }
87     return *ifPtr_;
91 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
93 Foam::IFstream::IFstream
95     const fileName& pathname,
96     streamFormat format,
97     versionNumber version
100     IFstreamAllocator(pathname),
101     ISstream
102     (
103         *ifPtr_,
104         "IFstream.sourceFile_",
105         format,
106         version,
107         IFstreamAllocator::compression_
108     ),
109     pathname_(pathname)
111     setClosed();
113     setState(ifPtr_->rdstate());
115     if (!good())
116     {
117         if (debug)
118         {
119             Info<< "IFstream::IFstream(const fileName&,"
120                    "streamFormat=ASCII,"
121                    "versionNumber=currentVersion) : "
122                    "could not open file for input"
123                 << endl << info() << endl;
124         }
126         setBad();
127     }
128     else
129     {
130         setOpened();
131     }
133     lineNumber_ = 1;
137 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
139 Foam::IFstream::~IFstream()
143 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
145 void Foam::IFstream::print(Ostream& os) const
147     // Print File data
148     os  << "IFstream: ";
149     ISstream::print(os);
153 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
155 Foam::IFstream& Foam::IFstream::operator()() const
157     if (!good())
158     {
159         // also checks .gz file
160         if (isFile(pathname_, true))
161         {
162             check("IFstream::operator()");
163             FatalIOError.exit();
164         }
165         else
166         {
167             FatalIOErrorIn("IFstream::operator()", *this)
168                 << "file " << pathname_ << " does not exist"
169                 << exit(FatalIOError);
170         }
171     }
173     return const_cast<IFstream&>(*this);
177 // ************************************************************************* //