initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / db / IOstreams / Fstreams / OFstream.C
blob2aa7296ab718040520b8ee6fbf379960c0d39907
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software; you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "OFstream.H"
28 #include "OSspecific.H"
29 #include "gzstream.h"
31 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
33 namespace Foam
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 defineTypeNameAndDebug(OFstream, 0);
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 OFstreamAllocator::OFstreamAllocator
44     const fileName& pathname,
45     IOstream::compressionType compression
48     ofPtr_(NULL)
50     if (!pathname.size())
51     {
52         if (OFstream::debug)
53         {
54             Info
55                 << "OFstreamAllocator::OFstreamAllocator"
56                    "(const fileName& pathname) : "
57                    "can't open null file "
58                 << endl;
59         }
60     }
62     if (compression == IOstream::COMPRESSED)
63     {
64         if (file(pathname))
65         {
66             rm(pathname);
67         }
69         ofPtr_ = new ogzstream((pathname + ".gz").c_str());
70     }
71     else
72     {
73         if (file(pathname + ".gz"))
74         {
75             rm(pathname + ".gz");
76         }
78         ofPtr_ = new ofstream(pathname.c_str());
79     }
83 OFstreamAllocator::~OFstreamAllocator()
85     delete ofPtr_;
89 ostream& OFstreamAllocator::stdStream()
91     if (!ofPtr_)
92     {
93         FatalErrorIn("OFstreamAllocator::stdStream()")
94             << "No stream allocated." << abort(FatalError);
95     }
96     return *ofPtr_;
100 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
102 OFstream::OFstream
104     const fileName& pathname,
105     streamFormat format,
106     versionNumber version,
107     compressionType compression
110     OFstreamAllocator(pathname, compression),
111     OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
112     pathname_(pathname)
114     setClosed();
116     setState(ofPtr_->rdstate());
117                 
118     if (!good())
119     {
120         if (debug)
121         {
122             Info<< "IFstream::IFstream(const fileName& pathname,"
123                    "streamFormat format=ASCII,"
124                    "versionNumber version=currentVersion) : "
125                    "couldn't open File for input\n"
126                    "in stream " << info() << Foam::endl;
127         }
129         setBad();
130     }
131     else
132     {
133         setOpened();
134     }
135     
136     lineNumber_ = 1;
140 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
142 OFstream::~OFstream()
146 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
148 void OFstream::print(Ostream& os) const
150     // Print File data
151     os  << "    OFstream: ";
152     OSstream::print(os);
156 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
158 } // End namespace Foam
160 // ************************************************************************* //