OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

IFstream.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "IFstream.H"
00028 #include "OSspecific.H"
00029 #include "gzstream.h"
00030 
00031 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00032 
00033 defineTypeNameAndDebug(Foam::IFstream, 0);
00034 
00035 
00036 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00037 
00038 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
00039 :
00040     ifPtr_(NULL),
00041     compression_(IOstream::UNCOMPRESSED)
00042 {
00043     if (pathname.empty())
00044     {
00045         if (IFstream::debug)
00046         {
00047             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
00048                     "cannot open null file " << endl;
00049         }
00050     }
00051 
00052     ifPtr_ = new ifstream(pathname.c_str());
00053 
00054     // If the file is compressed, decompress it before reading.
00055     if (!ifPtr_->good() && isFile(pathname + ".gz", false))
00056     {
00057         if (IFstream::debug)
00058         {
00059             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
00060                     "decompressing " << pathname + ".gz" << endl;
00061         }
00062 
00063         delete ifPtr_;
00064 
00065         ifPtr_ = new igzstream((pathname + ".gz").c_str());
00066 
00067         if (ifPtr_->good())
00068         {
00069             compression_ = IOstream::COMPRESSED;
00070         }
00071     }
00072 }
00073 
00074 
00075 Foam::IFstreamAllocator::~IFstreamAllocator()
00076 {
00077     delete ifPtr_;
00078 }
00079 
00080 
00081 std::istream& Foam::IFstreamAllocator::stdStream()
00082 {
00083     if (!ifPtr_)
00084     {
00085         FatalErrorIn("IFstreamAllocator::stdStream()")
00086             << "No stream allocated" << abort(FatalError);
00087     }
00088     return *ifPtr_;
00089 }
00090 
00091 
00092 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00093 
00094 Foam::IFstream::IFstream
00095 (
00096     const fileName& pathname,
00097     streamFormat format,
00098     versionNumber version
00099 )
00100 :
00101     IFstreamAllocator(pathname),
00102     ISstream
00103     (
00104         *ifPtr_,
00105         "IFstream.sourceFile_",
00106         format,
00107         version,
00108         IFstreamAllocator::compression_
00109     ),
00110     pathname_(pathname)
00111 {
00112     setClosed();
00113 
00114     setState(ifPtr_->rdstate());
00115 
00116     if (!good())
00117     {
00118         if (debug)
00119         {
00120             Info<< "IFstream::IFstream(const fileName&,"
00121                    "streamFormat=ASCII,"
00122                    "versionNumber=currentVersion) : "
00123                    "could not open file for input"
00124                 << endl << info() << endl;
00125         }
00126 
00127         setBad();
00128     }
00129     else
00130     {
00131         setOpened();
00132     }
00133 
00134     lineNumber_ = 1;
00135 }
00136 
00137 
00138 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00139 
00140 Foam::IFstream::~IFstream()
00141 {}
00142 
00143 
00144 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00145 
00146 void Foam::IFstream::print(Ostream& os) const
00147 {
00148     // Print File data
00149     os  << "IFstream: ";
00150     ISstream::print(os);
00151 }
00152 
00153 
00154 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
00155 
00156 Foam::IFstream& Foam::IFstream::operator()() const
00157 {
00158     if (!good())
00159     {
00160         // also checks .gz file
00161         if (isFile(pathname_, true))
00162         {
00163             check("IFstream::operator()");
00164             FatalIOError.exit();
00165         }
00166         else
00167         {
00168             FatalIOErrorIn("IFstream::operator()", *this)
00169                 << "file " << pathname_ << " does not exist"
00170                 << exit(FatalIOError);
00171         }
00172     }
00173 
00174     return const_cast<IFstream&>(*this);
00175 }
00176 
00177 
00178 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd