Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / db / IOstreams / Fstreams / OFstream.C
blobb56f35005c2bc31a48a43bac8e5e673ea645d4b4
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend 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 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "OFstream.H"
27 #include "OSspecific.H"
28 #include "gzstream.h"
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 defineTypeNameAndDebug(Foam::OFstream, 0);
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 Foam::OFstreamAllocator::OFstreamAllocator
39     const fileName& pathname,
40     ios_base::openmode mode,
41     IOstream::compressionType compression
44     ofPtr_(NULL)
46     if (pathname.empty())
47     {
48         if (OFstream::debug)
49         {
50             Info<< "OFstreamAllocator::OFstreamAllocator(const fileName&) : "
51                    "cannot open null file " << endl;
52         }
53     }
55     if (compression == IOstream::COMPRESSED)
56     {
57         // get identically named uncompressed version out of the way
58         if (isFile(pathname, false))
59         {
60             rm(pathname);
61         }
63         ofPtr_ = new ogzstream((pathname + ".gz").c_str(), mode);
64     }
65     else
66     {
67         // get identically named compressed version out of the way
68         if (isFile(pathname + ".gz", false))
69         {
70             rm(pathname + ".gz");
71         }
73         ofPtr_ = new ofstream(pathname.c_str(), mode);
74     }
78 Foam::OFstreamAllocator::~OFstreamAllocator()
80     delete ofPtr_;
84 std::ostream& Foam::OFstreamAllocator::stdStream()
86     if (!ofPtr_)
87     {
88         FatalErrorIn("OFstreamAllocator::stdStream()")
89             << "No stream allocated." << abort(FatalError);
90     }
91     return *ofPtr_;
95 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
97 Foam::OFstream::OFstream
99     const fileName& pathname,
100     ios_base::openmode mode,
101     streamFormat format,
102     versionNumber version,
103     compressionType compression
106     OFstreamAllocator(pathname, mode, compression),
107     OSstream(*ofPtr_, "OFstream.sinkFile_", format, version, compression),
108     pathname_(pathname)
110     setClosed();
111     setState(ofPtr_->rdstate());
113     if (!good())
114     {
115         if (debug)
116         {
117             Info<< "IFstream::IFstream(const fileName&,"
118                    "streamFormat format=ASCII,"
119                    "versionNumber version=currentVersion) : "
120                    "could not open file for input\n"
121                    "in stream " << info() << Foam::endl;
122         }
124         setBad();
125     }
126     else
127     {
128         setOpened();
129     }
131     lineNumber_ = 1;
135 // * * * * * * * * * * * * * * * * Destructors * * * * * * * * * * * * * * * //
137 Foam::OFstream::~OFstream()
141 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
143 void Foam::OFstream::print(Ostream& os) const
145     os  << "    OFstream: ";
146     OSstream::print(os);
150 // ************************************************************************* //