Merge branch 'upstream/OpenFOAM' into master
[freefoam.git] / src / OpenFOAM / db / IOstreams / Fstreams / IFstream.C
blob14b58b1ad2594f1dfa0e4ea98f1cb7615b334660
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "IFstream.H"
28 #include <OpenFOAM/OSspecific.H>
29 #include <OpenFOAM/gzstream.h>
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 defineTypeNameAndDebug(Foam::IFstream, 0);
36 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
40     ifPtr_(NULL),
41     compression_(IOstream::UNCOMPRESSED)
43     if (pathname.empty())
44     {
45         if (IFstream::debug)
46         {
47             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
48                     "cannot open null file " << endl;
49         }
50     }
52     ifPtr_ = new ifstream(pathname.c_str());
54     // If the file is compressed, decompress it before reading.
55     if (!ifPtr_->good() && isFile(pathname + ".gz", false))
56     {
57         if (IFstream::debug)
58         {
59             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
60                     "decompressing " << pathname + ".gz" << endl;
61         }
63         delete ifPtr_;
65         ifPtr_ = new igzstream((pathname + ".gz").c_str());
67         if (ifPtr_->good())
68         {
69             compression_ = IOstream::COMPRESSED;
70         }
71     }
75 Foam::IFstreamAllocator::~IFstreamAllocator()
77     delete ifPtr_;
81 std::istream& Foam::IFstreamAllocator::stdStream()
83     if (!ifPtr_)
84     {
85         FatalErrorIn("IFstreamAllocator::stdStream()")
86             << "No stream allocated" << abort(FatalError);
87     }
88     return *ifPtr_;
92 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
94 Foam::IFstream::IFstream
96     const fileName& pathname,
97     streamFormat format,
98     versionNumber version
101     IFstreamAllocator(pathname),
102     ISstream
103     (
104         *ifPtr_,
105         "IFstream.sourceFile_",
106         format,
107         version,
108         IFstreamAllocator::compression_
109     ),
110     pathname_(pathname)
112     setClosed();
114     setState(ifPtr_->rdstate());
116     if (!good())
117     {
118         if (debug)
119         {
120             Info<< "IFstream::IFstream(const fileName&,"
121                    "streamFormat=ASCII,"
122                    "versionNumber=currentVersion) : "
123                    "could not open file for input"
124                 << endl << info() << endl;
125         }
127         setBad();
128     }
129     else
130     {
131         setOpened();
132     }
134     lineNumber_ = 1;
138 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
140 Foam::IFstream::~IFstream()
144 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
146 void Foam::IFstream::print(Ostream& os) const
148     // Print File data
149     os  << "IFstream: ";
150     ISstream::print(os);
154 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
156 Foam::IFstream& Foam::IFstream::operator()() const
158     if (!good())
159     {
160         // also checks .gz file
161         if (isFile(pathname_, true))
162         {
163             check("IFstream::operator()");
164             FatalIOError.exit();
165         }
166         else
167         {
168             FatalIOErrorIn("IFstream::operator()", *this)
169                 << "file " << pathname_ << " does not exist"
170                 << exit(FatalIOError);
171         }
172     }
174     return const_cast<IFstream&>(*this);
178 // ************************ vim: set sw=4 sts=4 et: ************************ //