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

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