ENH: autoLayerDriver: better layering information message
[OpenFOAM-2.0.x.git] / src / OpenFOAM / db / IOstreams / Fstreams / IFstream.C
blobfb2480f41385ec0c152ea6663c2b922a66e072bb
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
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
13     the Free Software Foundation, either version 3 of the License, or
14     (at your 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, 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 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
82 Foam::IFstream::IFstream
84     const fileName& pathname,
85     streamFormat format,
86     versionNumber version
89     IFstreamAllocator(pathname),
90     ISstream
91     (
92         *ifPtr_,
93         "IFstream.sourceFile_",
94         format,
95         version,
96         IFstreamAllocator::compression_
97     ),
98     pathname_(pathname)
100     setClosed();
102     setState(ifPtr_->rdstate());
104     if (!good())
105     {
106         if (debug)
107         {
108             Info<< "IFstream::IFstream(const fileName&,"
109                    "streamFormat=ASCII,"
110                    "versionNumber=currentVersion) : "
111                    "could not open file for input"
112                 << endl << info() << endl;
113         }
115         setBad();
116     }
117     else
118     {
119         setOpened();
120     }
122     lineNumber_ = 1;
126 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
128 Foam::IFstream::~IFstream()
132 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
134 std::istream& Foam::IFstream::stdStream()
136     if (!ifPtr_)
137     {
138         FatalErrorIn("IFstream::stdStream()")
139             << "No stream allocated" << abort(FatalError);
140     }
141     return *ifPtr_;
145 const std::istream& Foam::IFstream::stdStream() const
147     if (!ifPtr_)
148     {
149         FatalErrorIn("IFstream::stdStream() const")
150             << "No stream allocated" << abort(FatalError);
151     }
152     return *ifPtr_;
156 void Foam::IFstream::print(Ostream& os) const
158     // Print File data
159     os  << "IFstream: ";
160     ISstream::print(os);
164 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
166 Foam::IFstream& Foam::IFstream::operator()() const
168     if (!good())
169     {
170         // also checks .gz file
171         if (isFile(pathname_, true))
172         {
173             check("IFstream::operator()");
174             FatalIOError.exit();
175         }
176         else
177         {
178             FatalIOErrorIn("IFstream::operator()", *this)
179                 << "file " << pathname_ << " does not exist"
180                 << exit(FatalIOError);
181         }
182     }
184     return const_cast<IFstream&>(*this);
188 // ************************************************************************* //