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

OFstream.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 "OFstream.H"
00027 #include <OpenFOAM/OSspecific.H>
00028 #include <OpenFOAM/gzstream.h>
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 defineTypeNameAndDebug(Foam::OFstream, 0);
00033 
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 Foam::OFstreamAllocator::OFstreamAllocator
00038 (
00039     const fileName& pathname,
00040     IOstream::compressionType compression
00041 )
00042 :
00043     ofPtr_(NULL)
00044 {
00045     if (pathname.empty())
00046     {
00047         if (OFstream::debug)
00048         {
00049             Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
00050                    "cannot open null file " << endl;
00051         }
00052     }
00053 
00054     if (compression == IOstream::COMPRESSED)
00055     {
00056         // get identically named uncompressed version out of the way
00057         if (isFile(pathname, false))
00058         {
00059             rm(pathname);
00060         }
00061 
00062         ofPtr_ = new ogzstream((pathname + ".gz").c_str());
00063     }
00064     else
00065     {
00066         // get identically named compressed version out of the way
00067         if (isFile(pathname + ".gz", false))
00068         {
00069             rm(pathname + ".gz");
00070         }
00071 
00072         ofPtr_ = new ofstream(pathname.c_str());
00073     }
00074 }
00075 
00076 
00077 Foam::OFstreamAllocator::~OFstreamAllocator()
00078 {
00079     delete ofPtr_;
00080 }
00081 
00082 
00083 std::ostream& Foam::OFstreamAllocator::stdStream()
00084 {
00085     if (!ofPtr_)
00086     {
00087         FatalErrorIn("OFstreamAllocator::stdStream()")
00088             << "No stream allocated." << abort(FatalError);
00089     }
00090     return *ofPtr_;
00091 }
00092 
00093 
00094 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00095 
00096 Foam::OFstream::OFstream
00097 (
00098     const fileName& pathname,
00099     streamFormat format,
00100     versionNumber version,
00101     compressionType compression
00102 )
00103 :
00104     OFstreamAllocator(pathname, compression),
00105     OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
00106     pathname_(pathname)
00107 {
00108     setClosed();
00109     setState(ofPtr_->rdstate());
00110 
00111     if (!good())
00112     {
00113         if (debug)
00114         {
00115             Info<< "IFstream::IFstream(const fileName&,"
00116                    "streamFormat format=ASCII,"
00117                    "versionNumber version=currentVersion) : "
00118                    "could not open file for input\n"
00119                    "in stream " << info() << Foam::endl;
00120         }
00121 
00122         setBad();
00123     }
00124     else
00125     {
00126         setOpened();
00127     }
00128 
00129     lineNumber_ = 1;
00130 }
00131 
00132 
00133 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
00134 
00135 Foam::OFstream::~OFstream()
00136 {}
00137 
00138 
00139 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00140 
00141 void Foam::OFstream::print(Ostream& os) const
00142 {
00143     os  << "    OFstream: ";
00144     OSstream::print(os);
00145 }
00146 
00147 
00148 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines