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 | using namespace std;
|
---|
38 |
|
---|
39 | #ifdef GZSTREAM_NAMESPACE
|
---|
40 | namespace GZSTREAM_NAMESPACE {
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | // ----------------------------------------------------------------------------
|
---|
44 | // Internal classes to implement gzstream. See below for user classes.
|
---|
45 | // ----------------------------------------------------------------------------
|
---|
46 |
|
---|
47 | // size of data buff
|
---|
48 | #define gzbufferSize 303
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | // #define STREAMBUF std::streambuf
|
---|
53 | // #define ISTREAM std::istream
|
---|
54 | // #define OSTREAM std::ostream
|
---|
55 |
|
---|
56 | #define STREAMBUF streambuf
|
---|
57 | #define ISTREAM istream
|
---|
58 | #define OSTREAM ostream
|
---|
59 |
|
---|
60 | class gzstreambuf : public STREAMBUF {
|
---|
61 |
|
---|
62 | private:
|
---|
63 | // totals 512 bytes under g++ for igzstream at the end.
|
---|
64 |
|
---|
65 | gzFile file; // file handle for compressed file
|
---|
66 | char buffer[gzbufferSize]; // data buffer
|
---|
67 | char opened; // open/close state of stream
|
---|
68 | int mode; // I/O mode
|
---|
69 |
|
---|
70 | int flush_buffer();
|
---|
71 | public:
|
---|
72 | gzstreambuf() : opened(0) {
|
---|
73 | setp( buffer, buffer + (gzbufferSize-1));
|
---|
74 | setg( buffer + 4, // beginning of putback area
|
---|
75 | buffer + 4, // read position
|
---|
76 | buffer + 4); // end position
|
---|
77 | // ASSERT: both input & output capabilities will not be used together
|
---|
78 | }
|
---|
79 | int is_open() { return opened; }
|
---|
80 | gzstreambuf* open( const char* name, int open_mode);
|
---|
81 | gzstreambuf* close();
|
---|
82 | ~gzstreambuf() { close(); }
|
---|
83 |
|
---|
84 | virtual int overflow( int c = EOF);
|
---|
85 | virtual int underflow();
|
---|
86 | virtual int sync();
|
---|
87 |
|
---|
88 | };
|
---|
89 |
|
---|
90 | class gzstreambase : virtual public ios {
|
---|
91 |
|
---|
92 | protected:
|
---|
93 | gzstreambuf buf;
|
---|
94 | public:
|
---|
95 | gzstreambase() { init(&buf); }
|
---|
96 | gzstreambase( const char* name, int open_mode);
|
---|
97 | ~gzstreambase();
|
---|
98 | void open( const char* name, int open_mode);
|
---|
99 | void close();
|
---|
100 | gzstreambuf* rdbuf() { return &buf; }
|
---|
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 |
|
---|