IFstream.C
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
00027 #include "IFstream.H"
00028 #include "OSspecific.H"
00029 #include "gzstream.h"
00030
00031
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
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
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
00139
00140 Foam::IFstream::~IFstream()
00141 {}
00142
00143
00144
00145
00146 void Foam::IFstream::print(Ostream& os) const
00147 {
00148
00149 os << "IFstream: ";
00150 ISstream::print(os);
00151 }
00152
00153
00154
00155
00156 Foam::IFstream& Foam::IFstream::operator()() const
00157 {
00158 if (!good())
00159 {
00160
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