Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 #include "IFstream.H"
00027 #include <OpenFOAM/OSspecific.H>
00028 #include <OpenFOAM/gzstream.h>
00029 
00030 
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     
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 
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 
00138 
00139 Foam::IFstream::~IFstream()
00140 {}
00141 
00142 
00143 
00144 
00145 void Foam::IFstream::print(Ostream& os) const
00146 {
00147     
00148     os  << "IFstream: ";
00149     ISstream::print(os);
00150 }
00151 
00152 
00153 
00154 
00155 Foam::IFstream& Foam::IFstream::operator()() const
00156 {
00157     if (!good())
00158     {
00159         
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