1 // ============================================================================
2 // gzstream, C++ iostream classes wrapping the zlib compression library.
3 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ============================================================================
21 // Revision : $Revision: 1.5 $
22 // Revision_date : $Date: 2002/04/26 23:30:15 $
23 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
32 // standard C++ with new header file names and std:: namespace
37 #ifdef GZSTREAM_NAMESPACE
38 namespace GZSTREAM_NAMESPACE {
41 // ----------------------------------------------------------------------------
42 // Internal classes to implement gzstream. See below for user classes.
43 // ----------------------------------------------------------------------------
45 class gzstreambuf : public std::streambuf {
47 static const int bufferSize = 47+256; // size of data buff
48 // totals 512 bytes under g++ for igzstream at the end.
50 gzFile file; // file handle for compressed file
51 char buffer[bufferSize]; // data buffer
52 char opened; // open/close state of stream
57 gzstreambuf() : opened(0) {
58 setp( buffer, buffer + (bufferSize-1));
59 setg( buffer + 4, // beginning of putback area
60 buffer + 4, // read position
61 buffer + 4); // end position
62 // ASSERT: both input & output capabilities will not be used together
64 int is_open() { return opened; }
65 gzstreambuf* open( const char* name, int open_mode);
67 ~gzstreambuf() { close(); }
69 virtual int overflow( int c = EOF);
70 virtual int underflow();
74 class gzstreambase : virtual public std::ios {
78 gzstreambase() { init(&buf); }
79 gzstreambase( const char* name, int open_mode);
81 int is_open() { return buf.is_open(); }
82 void open( const char* name, int open_mode);
84 gzstreambuf* rdbuf() { return &buf; }
87 // ----------------------------------------------------------------------------
88 // User classes. Use igzstream and ogzstream analogously to ifstream and
89 // ofstream respectively. They read and write files based on the gz*
90 // function interface of the zlib. Files are compatible with gzip compression.
91 // ----------------------------------------------------------------------------
93 class igzstream : public gzstreambase, public std::istream {
95 igzstream() : std::istream( &buf) {}
96 igzstream( const char* name, int open_mode = std::ios::in)
97 : gzstreambase( name, open_mode | std::ios::in), std::istream( &buf) {}
98 gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
99 void open( const char* name, int open_mode = std::ios::in) {
100 gzstreambase::open( name, open_mode);
104 class ogzstream : public gzstreambase, public std::ostream {
106 ogzstream() : std::ostream( &buf) {}
107 ogzstream( const char* name, int mode = std::ios::out)
108 : gzstreambase( name, mode | std::ios::out), std::ostream( &buf) {}
109 gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
110 void open( const char* name, int open_mode = std::ios::out) {
111 gzstreambase::open( name, open_mode);
115 #ifdef GZSTREAM_NAMESPACE
116 } // namespace GZSTREAM_NAMESPACE
120 // ============================================================================