[372] | 1 | // ============================================================================
|
---|
| 2 | // gzstream, C++ iostream classes wrapping the zlib compression library.
|
---|
| 3 | // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
|
---|
| 4 | //
|
---|
| 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.
|
---|
| 9 | //
|
---|
| 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.
|
---|
| 14 | //
|
---|
| 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 | // ============================================================================
|
---|
| 19 | //
|
---|
| 20 | // File : gzstream.h
|
---|
| 21 | // Revision : $Revision: 1.1 $
|
---|
| 22 | // Revision_date : $Date: 2004/02/16 14:46:00 $
|
---|
| 23 | // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
|
---|
| 24 | //
|
---|
| 25 | // Standard streambuf implementation following Nicolai Josuttis, "The
|
---|
| 26 | // Standard C++ Library".
|
---|
| 27 | // ============================================================================
|
---|
| 28 |
|
---|
| 29 | #ifndef GZSTREAM_H
|
---|
| 30 | #define GZSTREAM_H 1
|
---|
| 31 |
|
---|
| 32 | #include <zlib.h>
|
---|
| 33 |
|
---|
| 34 | // standard C++ with new header file names and std:: namespace
|
---|
| 35 | #include <iostream>
|
---|
| 36 | #include <fstream>
|
---|
| 37 |
|
---|
| 38 | #ifdef GZSTREAM_NAMESPACE
|
---|
| 39 | namespace GZSTREAM_NAMESPACE {
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
| 42 | // ----------------------------------------------------------------------------
|
---|
| 43 | // Internal classes to implement gzstream. See below for user classes.
|
---|
| 44 | // ----------------------------------------------------------------------------
|
---|
| 45 |
|
---|
| 46 | // size of data buff
|
---|
| 47 | #define gzbufferSize 303
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 |
|
---|
[2176] | 51 | #define STREAMBUF std::streambuf
|
---|
| 52 | #define ISTREAM std::istream
|
---|
| 53 | #define OSTREAM std::ostream
|
---|
[372] | 54 |
|
---|
[2176] | 55 | //#define STREAMBUF streambuf
|
---|
| 56 | //#define ISTREAM istream
|
---|
| 57 | //#define OSTREAM ostream
|
---|
[372] | 58 |
|
---|
| 59 | class gzstreambuf : public STREAMBUF {
|
---|
| 60 |
|
---|
| 61 | private:
|
---|
| 62 | // totals 512 bytes under g++ for igzstream at the end.
|
---|
| 63 |
|
---|
| 64 | gzFile file; // file handle for compressed file
|
---|
| 65 | char buffer[gzbufferSize]; // data buffer
|
---|
| 66 | char opened; // open/close state of stream
|
---|
| 67 | int mode; // I/O mode
|
---|
| 68 |
|
---|
| 69 | int flush_buffer();
|
---|
| 70 | public:
|
---|
| 71 | gzstreambuf() : opened(0) {
|
---|
| 72 | setp( buffer, buffer + (gzbufferSize-1));
|
---|
| 73 | setg( buffer + 4, // beginning of putback area
|
---|
| 74 | buffer + 4, // read position
|
---|
| 75 | buffer + 4); // end position
|
---|
| 76 | // ASSERT: both input & output capabilities will not be used together
|
---|
| 77 | }
|
---|
[1264] | 78 | int is_open() const { return opened; }
|
---|
[372] | 79 | gzstreambuf* open( const char* name, int open_mode);
|
---|
| 80 | gzstreambuf* close();
|
---|
| 81 | ~gzstreambuf() { close(); }
|
---|
| 82 |
|
---|
| 83 | virtual int overflow( int c = EOF);
|
---|
| 84 | virtual int underflow();
|
---|
| 85 | virtual int sync();
|
---|
| 86 |
|
---|
| 87 | };
|
---|
| 88 |
|
---|
[2176] | 89 | class gzstreambase : virtual public std::ios {
|
---|
[372] | 90 |
|
---|
| 91 | protected:
|
---|
| 92 | gzstreambuf buf;
|
---|
| 93 | public:
|
---|
| 94 | gzstreambase() { init(&buf); }
|
---|
| 95 | gzstreambase( const char* name, int open_mode);
|
---|
| 96 | ~gzstreambase();
|
---|
| 97 | void open( const char* name, int open_mode);
|
---|
| 98 | void close();
|
---|
| 99 | gzstreambuf* rdbuf() { return &buf; }
|
---|
[1264] | 100 | int is_open() const { return buf.is_open(); }
|
---|
[372] | 101 | };
|
---|
| 102 |
|
---|
| 103 | // ----------------------------------------------------------------------------
|
---|
| 104 | // User classes. Use igzstream and ogzstream analogously to ifstream and
|
---|
| 105 | // ofstream respectively. They read and write files based on the gz*
|
---|
| 106 | // function interface of the zlib. Files are compatible with gzip compression.
|
---|
| 107 | // ----------------------------------------------------------------------------
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | class igzstream : public gzstreambase, public ISTREAM {
|
---|
| 112 |
|
---|
| 113 | public:
|
---|
| 114 | igzstream() : gzstreambase(), ISTREAM( &buf ) {}
|
---|
| 115 |
|
---|
| 116 | igzstream( const char* name, int open_mode = std::ios::in)
|
---|
| 117 | : gzstreambase( name, open_mode), ISTREAM( &buf) {}
|
---|
| 118 |
|
---|
| 119 | gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
|
---|
| 120 |
|
---|
| 121 | void open( const char* name, int open_mode = std::ios::in) {
|
---|
| 122 | gzstreambase::open( name, open_mode);
|
---|
| 123 | }
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | class ogzstream : public gzstreambase, public OSTREAM {
|
---|
| 127 |
|
---|
| 128 | public:
|
---|
| 129 | ogzstream() : OSTREAM( &buf) {}
|
---|
| 130 | ogzstream( const char* name, int mode = std::ios::out)
|
---|
| 131 | : gzstreambase( name, mode), OSTREAM( &buf) {}
|
---|
| 132 | gzstreambuf* rdbuf() { return gzstreambase::rdbuf(); }
|
---|
| 133 | void open( const char* name, int open_mode = std::ios::out) {
|
---|
| 134 | gzstreambase::open( name, open_mode);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | };
|
---|
| 138 |
|
---|
| 139 | #ifdef GZSTREAM_NAMESPACE
|
---|
| 140 | } // namespace GZSTREAM_NAMESPACE
|
---|
| 141 | #endif
|
---|
| 142 |
|
---|
| 143 | #endif // GZSTREAM_H
|
---|
| 144 | // ============================================================================
|
---|
| 145 | // EOF //
|
---|
| 146 |
|
---|