FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 #include "IFstream.H"
00027 #include <OpenFOAM/OSspecific.H>
00028 #include <OpenFOAM/gzstream.h>
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::IFstream, 0);
00033 
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 Foam::IFstreamAllocator::IFstreamAllocator(const fileName& pathname)
00038 :
00039     ifPtr_(NULL),
00040     compression_(IOstream::UNCOMPRESSED)
00041 {
00042     if (pathname.empty())
00043     {
00044         if (IFstream::debug)
00045         {
00046             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
00047                     "cannot open null file " << endl;
00048         }
00049     }
00050 
00051     ifPtr_ = new ifstream(pathname.c_str());
00052 
00053     // If the file is compressed, decompress it before reading.
00054     if (!ifPtr_->good() && isFile(pathname + ".gz", false))
00055     {
00056         if (IFstream::debug)
00057         {
00058             Info<< "IFstreamAllocator::IFstreamAllocator(const fileName&) : "
00059                     "decompressing " << pathname + ".gz" << endl;
00060         }
00061 
00062         delete ifPtr_;
00063 
00064         ifPtr_ = new igzstream((pathname + ".gz").c_str());
00065 
00066         if (ifPtr_->good())
00067         {
00068             compression_ = IOstream::COMPRESSED;
00069         }
00070     }
00071 }
00072 
00073 
00074 Foam::IFstreamAllocator::~IFstreamAllocator()
00075 {
00076     delete ifPtr_;
00077 }
00078 
00079 
00080 std::istream& Foam::IFstreamAllocator::stdStream()
00081 {
00082     if (!ifPtr_)
00083     {
00084         FatalErrorIn("IFstreamAllocator::stdStream()")
00085             << "No stream allocated" << abort(FatalError);
00086     }
00087     return *ifPtr_;
00088 }
00089 
00090 
00091 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00092 
00093 Foam::IFstream::IFstream
00094 (
00095     const fileName& pathname,
00096     streamFormat format,
00097     versionNumber version
00098 )
00099 :
00100     IFstreamAllocator(pathname),
00101     ISstream
00102     (
00103         *ifPtr_,
00104         "IFstream.sourceFile_",
00105         format,
00106         version,
00107         IFstreamAllocator::compression_
00108     ),
00109     pathname_(pathname)
00110 {
00111     setClosed();
00112 
00113     setState(ifPtr_->rdstate());
00114 
00115     if (!good())
00116     {
00117         if (debug)
00118         {
00119             Info<< "IFstream::IFstream(const fileName&,"
00120                    "streamFormat=ASCII,"
00121                    "versionNumber=currentVersion) : "
00122                    "could not open file for input"
00123                 << endl << info() << endl;
00124         }
00125 
00126         setBad();
00127     }
00128     else
00129     {
00130         setOpened();
00131     }
00132 
00133     lineNumber_ = 1;
00134 }
00135 
00136 
00137 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00138 
00139 Foam::IFstream::~IFstream()
00140 {}
00141 
00142 
00143 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00144 
00145 void Foam::IFstream::print(Ostream& os) const
00146 {
00147     // Print File data
00148     os  << "IFstream: ";
00149     ISstream::print(os);
00150 }
00151 
00152 
00153 // * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * * //
00154 
00155 Foam::IFstream& Foam::IFstream::operator()() const
00156 {
00157     if (!good())
00158     {
00159         // also checks .gz file
00160         if (isFile(pathname_, true))
00161         {
00162             check("IFstream::operator()");
00163             FatalIOError.exit();
00164         }
00165         else
00166         {
00167             FatalIOErrorIn("IFstream::operator()", *this)
00168                 << "file " << pathname_ << " does not exist"
00169                 << exit(FatalIOError);
00170         }
00171     }
00172 
00173     return const_cast<IFstream&>(*this);
00174 }
00175 
00176 
00177 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines