initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOstreams / Fstreams / IFstream.C
blob3401aab11c07245fd5ea2ab69c7ff464cf6945ee
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 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 "OSspecific.H"
29 #include "gzstream.h"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 defineTypeNameAndDebug(IFstream, 0);
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
44     ifPtr_(NULL),
45     compression_(IOstream::UNCOMPRESSED)
47     if (!pathname.size())
48     {
49         if (IFstream::debug)
50         {
51             Info<< "IFstreamAllocator::IFstreamAllocator"
52                    "(const fileName& pathname) : "
53                    "can't open null file "
54                 << endl;
55         }
56     }
58     ifPtr_ = new ifstream(pathname.c_str());
60     // If the file is compressed, decompress it before reading.
61     if (!ifPtr_->good() && file(pathname + ".gz"))
62     {
63         if (IFstream::debug)
64         {
65             Info<< "IFstreamAllocator::IFstreamAllocator"
66                    "(const fileName& pathname) : "
67                    "decompressing " << pathname + ".gz"
68                 << endl;
69         }
71         delete ifPtr_;
73         ifPtr_ = new igzstream((pathname + ".gz").c_str());
75         if (ifPtr_->good())
76         {
77             compression_ = IOstream::COMPRESSED;
78         }
79     }
83 IFstreamAllocator::~IFstreamAllocator()
85     delete ifPtr_;
89 istream& IFstreamAllocator::stdStream()
91     if (!ifPtr_)
92     {
93         FatalErrorIn("IFstreamAllocator::stdStream()")
94             << "No stream allocated." << abort(FatalError);
95     }
96     return *ifPtr_;
100 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
102 IFstream::IFstream
104     const fileName& pathname,
105     streamFormat format,
106     versionNumber version
109     IFstreamAllocator(pathname),
110     ISstream
111     (
112         *ifPtr_,
113         "IFstream.sourceFile_",
114         format,
115         version,
116         IFstreamAllocator::compression_
117     ),
118     pathname_(pathname)
120     setClosed();
122     setState(ifPtr_->rdstate());
123                 
124     if (!good())
125     {
126         if (debug)
127         {
128             Info<< "IFstream::IFstream(const fileName& pathname,"
129                    "streamFormat format=ASCII,"
130                    "versionNumber version=currentVersion) : "
131                    "couldn't open File for input"
132                 << endl << info() << endl;
133         }
135         setBad();
136     }
137     else
138     {
139         setOpened();
140     }
141     
142     lineNumber_ = 1;
146 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
148 IFstream::~IFstream()
152 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
154 void IFstream::print(Ostream& os) const
156     // Print File data
157     os  << "IFstream: ";
158     ISstream::print(os);
162 //- Return a non-const reference to const Istream
163 //  Needed for read-constructors where the stream argument is temporary:
164 //  e.g. thing thisThing(IFstream("thingFileName")());
165 IFstream& IFstream::operator()() const
167     if (!good())
168     {
169         if (!file(pathname_) && !file(pathname_ + ".gz"))
170         {
171             FatalIOErrorIn("IFstream::operator()", *this)
172                 << "file " << pathname_ << " does not exist"
173                 << exit(FatalIOError);
174         }
175         else
176         {
177             check("IFstream::operator()");
178             FatalIOError.exit();
179         }
180     }
182     return const_cast<IFstream&>(*this);
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
188 } // End namespace Foam
190 // ************************************************************************* //