diff options
author | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:17:33 -0700 |
---|---|---|
committer | Mark Adler <madler@alumni.caltech.edu> | 2011-09-09 23:17:33 -0700 |
commit | 7850e4e406dce1f7a819297eeb151d1ca18e7cd9 (patch) | |
tree | d4befddacae46b06c4924193904de533099610b4 /contrib/iostream | |
parent | ebd3c2c0e734fc99a1360014ea52ed04fe6aade4 (diff) | |
download | zlib-1.0.7.tar.gz zlib-1.0.7.tar.bz2 zlib-1.0.7.zip |
zlib 1.0.7v1.0.7
Diffstat (limited to 'contrib/iostream')
-rw-r--r-- | contrib/iostream/test.cpp | 24 | ||||
-rw-r--r-- | contrib/iostream/zfstream.cpp | 329 | ||||
-rw-r--r-- | contrib/iostream/zfstream.h | 142 |
3 files changed, 495 insertions, 0 deletions
diff --git a/contrib/iostream/test.cpp b/contrib/iostream/test.cpp new file mode 100644 index 0000000..7d265b3 --- /dev/null +++ b/contrib/iostream/test.cpp | |||
@@ -0,0 +1,24 @@ | |||
1 | |||
2 | #include "zfstream.h" | ||
3 | |||
4 | int main() { | ||
5 | |||
6 | // Construct a stream object with this filebuffer. Anything sent | ||
7 | // to this stream will go to standard out. | ||
8 | gzofstream os( 1, ios::out ); | ||
9 | |||
10 | // This text is getting compressed and sent to stdout. | ||
11 | // To prove this, run 'test | zcat'. | ||
12 | os << "Hello, Mommy" << endl; | ||
13 | |||
14 | os << setcompressionlevel( Z_NO_COMPRESSION ); | ||
15 | os << "hello, hello, hi, ho!" << endl; | ||
16 | |||
17 | setcompressionlevel( os, Z_DEFAULT_COMPRESSION ) | ||
18 | << "I'm compressing again" << endl; | ||
19 | |||
20 | os.close(); | ||
21 | |||
22 | return 0; | ||
23 | |||
24 | } | ||
diff --git a/contrib/iostream/zfstream.cpp b/contrib/iostream/zfstream.cpp new file mode 100644 index 0000000..a690bbe --- /dev/null +++ b/contrib/iostream/zfstream.cpp | |||
@@ -0,0 +1,329 @@ | |||
1 | |||
2 | #include <memory.h> | ||
3 | #include "zfstream.h" | ||
4 | |||
5 | gzfilebuf::gzfilebuf() : | ||
6 | file(NULL), | ||
7 | mode(0), | ||
8 | own_file_descriptor(0) | ||
9 | { } | ||
10 | |||
11 | gzfilebuf::~gzfilebuf() { | ||
12 | |||
13 | sync(); | ||
14 | if ( own_file_descriptor ) | ||
15 | close(); | ||
16 | |||
17 | } | ||
18 | |||
19 | gzfilebuf *gzfilebuf::open( const char *name, | ||
20 | int io_mode ) { | ||
21 | |||
22 | if ( is_open() ) | ||
23 | return NULL; | ||
24 | |||
25 | char char_mode[10]; | ||
26 | char *p; | ||
27 | memset(char_mode,'\0',10); | ||
28 | p = char_mode; | ||
29 | |||
30 | if ( io_mode & ios::in ) { | ||
31 | mode = ios::in; | ||
32 | *p++ = 'r'; | ||
33 | } else if ( io_mode & ios::app ) { | ||
34 | mode = ios::app; | ||
35 | *p++ = 'a'; | ||
36 | } else { | ||
37 | mode = ios::out; | ||
38 | *p++ = 'w'; | ||
39 | } | ||
40 | |||
41 | if ( io_mode & ios::binary ) { | ||
42 | mode |= ios::binary; | ||
43 | *p++ = 'b'; | ||
44 | } | ||
45 | |||
46 | // Hard code the compression level | ||
47 | if ( io_mode & (ios::out|ios::app )) { | ||
48 | *p++ = '9'; | ||
49 | } | ||
50 | |||
51 | if ( (file = gzopen(name, char_mode)) == NULL ) | ||
52 | return NULL; | ||
53 | |||
54 | own_file_descriptor = 1; | ||
55 | |||
56 | return this; | ||
57 | |||
58 | } | ||
59 | |||
60 | gzfilebuf *gzfilebuf::attach( int file_descriptor, | ||
61 | int io_mode ) { | ||
62 | |||
63 | if ( is_open() ) | ||
64 | return NULL; | ||
65 | |||
66 | char char_mode[10]; | ||
67 | char *p; | ||
68 | memset(char_mode,'\0',10); | ||
69 | p = char_mode; | ||
70 | |||
71 | if ( io_mode & ios::in ) { | ||
72 | mode = ios::in; | ||
73 | *p++ = 'r'; | ||
74 | } else if ( io_mode & ios::app ) { | ||
75 | mode = ios::app; | ||
76 | *p++ = 'a'; | ||
77 | } else { | ||
78 | mode = ios::out; | ||
79 | *p++ = 'w'; | ||
80 | } | ||
81 | |||
82 | if ( io_mode & ios::binary ) { | ||
83 | mode |= ios::binary; | ||
84 | *p++ = 'b'; | ||
85 | } | ||
86 | |||
87 | // Hard code the compression level | ||
88 | if ( io_mode & (ios::out|ios::app )) { | ||
89 | *p++ = '9'; | ||
90 | } | ||
91 | |||
92 | if ( (file = gzdopen(file_descriptor, char_mode)) == NULL ) | ||
93 | return NULL; | ||
94 | |||
95 | own_file_descriptor = 0; | ||
96 | |||
97 | return this; | ||
98 | |||
99 | } | ||
100 | |||
101 | gzfilebuf *gzfilebuf::close() { | ||
102 | |||
103 | if ( is_open() ) { | ||
104 | |||
105 | sync(); | ||
106 | gzclose( file ); | ||
107 | file = NULL; | ||
108 | |||
109 | } | ||
110 | |||
111 | return this; | ||
112 | |||
113 | } | ||
114 | |||
115 | int gzfilebuf::setcompressionlevel( short comp_level ) { | ||
116 | |||
117 | return gzsetparams(file, comp_level, -2); | ||
118 | |||
119 | } | ||
120 | |||
121 | int gzfilebuf::setcompressionstrategy( short comp_strategy ) { | ||
122 | |||
123 | return gzsetparams(file, -2, comp_strategy); | ||
124 | |||
125 | } | ||
126 | |||
127 | |||
128 | streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) { | ||
129 | |||
130 | return streampos(EOF); | ||
131 | |||
132 | } | ||
133 | |||
134 | int gzfilebuf::underflow() { | ||
135 | |||
136 | // If the file hasn't been opened for reading, error. | ||
137 | if ( !is_open() || !(mode & ios::in) ) | ||
138 | return EOF; | ||
139 | |||
140 | // if a buffer doesn't exists, allocate one. | ||
141 | if ( !base() ) { | ||
142 | |||
143 | if ( (allocate()) == EOF ) | ||
144 | return EOF; | ||
145 | setp(0,0); | ||
146 | |||
147 | } else { | ||
148 | |||
149 | if ( in_avail() ) | ||
150 | return (unsigned char) *gptr(); | ||
151 | |||
152 | if ( out_waiting() ) { | ||
153 | if ( flushbuf() == EOF ) | ||
154 | return EOF; | ||
155 | } | ||
156 | |||
157 | } | ||
158 | |||
159 | // Attempt to fill the buffer. | ||
160 | |||
161 | int result = fillbuf(); | ||
162 | if ( result == EOF ) { | ||
163 | // disable get area | ||
164 | setg(0,0,0); | ||
165 | return EOF; | ||
166 | } | ||
167 | |||
168 | return (unsigned char) *gptr(); | ||
169 | |||
170 | } | ||
171 | |||
172 | int gzfilebuf::overflow( int c ) { | ||
173 | |||
174 | if ( !is_open() || !(mode & ios::out) ) | ||
175 | return EOF; | ||
176 | |||
177 | if ( !base() ) { | ||
178 | if ( allocate() == EOF ) | ||
179 | return EOF; | ||
180 | setg(0,0,0); | ||
181 | } else { | ||
182 | if (in_avail()) { | ||
183 | return EOF; | ||
184 | } | ||
185 | if (out_waiting()) { | ||
186 | if (flushbuf() == EOF) | ||
187 | return EOF; | ||
188 | } | ||
189 | } | ||
190 | |||
191 | int bl = blen(); | ||
192 | setp( base(), base() + bl); | ||
193 | |||
194 | if ( c != EOF ) { | ||
195 | |||
196 | *pptr() = c; | ||
197 | pbump(1); | ||
198 | |||
199 | } | ||
200 | |||
201 | return 0; | ||
202 | |||
203 | } | ||
204 | |||
205 | int gzfilebuf::sync() { | ||
206 | |||
207 | if ( !is_open() ) | ||
208 | return EOF; | ||
209 | |||
210 | if ( out_waiting() ) | ||
211 | return flushbuf(); | ||
212 | |||
213 | return 0; | ||
214 | |||
215 | } | ||
216 | |||
217 | int gzfilebuf::flushbuf() { | ||
218 | |||
219 | int n; | ||
220 | char *q; | ||
221 | |||
222 | q = pbase(); | ||
223 | n = pptr() - q; | ||
224 | |||
225 | if ( gzwrite( file, q, n) < n ) | ||
226 | return EOF; | ||
227 | |||
228 | setp(0,0); | ||
229 | |||
230 | return 0; | ||
231 | |||
232 | } | ||
233 | |||
234 | int gzfilebuf::fillbuf() { | ||
235 | |||
236 | int required; | ||
237 | char *p; | ||
238 | |||
239 | p = base(); | ||
240 | |||
241 | required = blen(); | ||
242 | |||
243 | int t = gzread( file, p, required ); | ||
244 | |||
245 | if ( t <= 0) return EOF; | ||
246 | |||
247 | setg( base(), base(), base()+t); | ||
248 | |||
249 | return t; | ||
250 | |||
251 | } | ||
252 | |||
253 | gzfilestream_common::gzfilestream_common() : | ||
254 | ios( gzfilestream_common::rdbuf() ) | ||
255 | { } | ||
256 | |||
257 | gzfilestream_common::~gzfilestream_common() | ||
258 | { } | ||
259 | |||
260 | void gzfilestream_common::attach( int fd, int io_mode ) { | ||
261 | |||
262 | if ( !buffer.attach( fd, io_mode) ) | ||
263 | clear( ios::failbit | ios::badbit ); | ||
264 | else | ||
265 | clear(); | ||
266 | |||
267 | } | ||
268 | |||
269 | void gzfilestream_common::open( const char *name, int io_mode ) { | ||
270 | |||
271 | if ( !buffer.open( name, io_mode ) ) | ||
272 | clear( ios::failbit | ios::badbit ); | ||
273 | else | ||
274 | clear(); | ||
275 | |||
276 | } | ||
277 | |||
278 | void gzfilestream_common::close() { | ||
279 | |||
280 | if ( !buffer.close() ) | ||
281 | clear( ios::failbit | ios::badbit ); | ||
282 | |||
283 | } | ||
284 | |||
285 | gzfilebuf *gzfilestream_common::rdbuf() { | ||
286 | |||
287 | return &buffer; | ||
288 | |||
289 | } | ||
290 | |||
291 | gzifstream::gzifstream() : | ||
292 | ios( gzfilestream_common::rdbuf() ) | ||
293 | { | ||
294 | clear( ios::badbit ); | ||
295 | } | ||
296 | |||
297 | gzifstream::gzifstream( const char *name, int io_mode ) : | ||
298 | ios( gzfilestream_common::rdbuf() ) | ||
299 | { | ||
300 | gzfilestream_common::open( name, io_mode ); | ||
301 | } | ||
302 | |||
303 | gzifstream::gzifstream( int fd, int io_mode ) : | ||
304 | ios( gzfilestream_common::rdbuf() ) | ||
305 | { | ||
306 | gzfilestream_common::attach( fd, io_mode ); | ||
307 | } | ||
308 | |||
309 | gzifstream::~gzifstream() { } | ||
310 | |||
311 | gzofstream::gzofstream() : | ||
312 | ios( gzfilestream_common::rdbuf() ) | ||
313 | { | ||
314 | clear( ios::badbit ); | ||
315 | } | ||
316 | |||
317 | gzofstream::gzofstream( const char *name, int io_mode ) : | ||
318 | ios( gzfilestream_common::rdbuf() ) | ||
319 | { | ||
320 | gzfilestream_common::open( name, io_mode ); | ||
321 | } | ||
322 | |||
323 | gzofstream::gzofstream( int fd, int io_mode ) : | ||
324 | ios( gzfilestream_common::rdbuf() ) | ||
325 | { | ||
326 | gzfilestream_common::attach( fd, io_mode ); | ||
327 | } | ||
328 | |||
329 | gzofstream::~gzofstream() { } | ||
diff --git a/contrib/iostream/zfstream.h b/contrib/iostream/zfstream.h new file mode 100644 index 0000000..c87fa08 --- /dev/null +++ b/contrib/iostream/zfstream.h | |||
@@ -0,0 +1,142 @@ | |||
1 | |||
2 | #ifndef _zfstream_h | ||
3 | #define _zfstream_h | ||
4 | |||
5 | #include <fstream.h> | ||
6 | #include "zlib.h" | ||
7 | |||
8 | class gzfilebuf : public streambuf { | ||
9 | |||
10 | public: | ||
11 | |||
12 | gzfilebuf( ); | ||
13 | virtual ~gzfilebuf(); | ||
14 | |||
15 | gzfilebuf *open( const char *name, int io_mode ); | ||
16 | gzfilebuf *attach( int file_descriptor, int io_mode ); | ||
17 | gzfilebuf *close(); | ||
18 | |||
19 | int setcompressionlevel( short comp_level ); | ||
20 | int setcompressionstrategy( short comp_strategy ); | ||
21 | |||
22 | inline int is_open() const { return (file !=NULL); } | ||
23 | |||
24 | virtual streampos seekoff( streamoff, ios::seek_dir, int ); | ||
25 | |||
26 | virtual int sync(); | ||
27 | |||
28 | protected: | ||
29 | |||
30 | virtual int underflow(); | ||
31 | virtual int overflow( int = EOF ); | ||
32 | |||
33 | private: | ||
34 | |||
35 | gzFile file; | ||
36 | short mode; | ||
37 | short own_file_descriptor; | ||
38 | |||
39 | int flushbuf(); | ||
40 | int fillbuf(); | ||
41 | |||
42 | }; | ||
43 | |||
44 | class gzfilestream_common : virtual public ios { | ||
45 | |||
46 | friend class gzifstream; | ||
47 | friend class gzofstream; | ||
48 | friend gzofstream &setcompressionlevel( gzofstream &, int ); | ||
49 | friend gzofstream &setcompressionstrategy( gzofstream &, int ); | ||
50 | |||
51 | public: | ||
52 | virtual ~gzfilestream_common(); | ||
53 | |||
54 | void attach( int fd, int io_mode ); | ||
55 | void open( const char *name, int io_mode ); | ||
56 | void close(); | ||
57 | |||
58 | protected: | ||
59 | gzfilestream_common(); | ||
60 | |||
61 | private: | ||
62 | gzfilebuf *rdbuf(); | ||
63 | |||
64 | gzfilebuf buffer; | ||
65 | |||
66 | }; | ||
67 | |||
68 | class gzifstream : public gzfilestream_common, public istream { | ||
69 | |||
70 | public: | ||
71 | |||
72 | gzifstream(); | ||
73 | gzifstream( const char *name, int io_mode = ios::in ); | ||
74 | gzifstream( int fd, int io_mode = ios::in ); | ||
75 | |||
76 | virtual ~gzifstream(); | ||
77 | |||
78 | }; | ||
79 | |||
80 | class gzofstream : public gzfilestream_common, public ostream { | ||
81 | |||
82 | public: | ||
83 | |||
84 | gzofstream(); | ||
85 | gzofstream( const char *name, int io_mode = ios::out ); | ||
86 | gzofstream( int fd, int io_mode = ios::out ); | ||
87 | |||
88 | virtual ~gzofstream(); | ||
89 | |||
90 | }; | ||
91 | |||
92 | template<class T> class gzomanip { | ||
93 | friend gzofstream &operator<<(gzofstream &, const gzomanip<T> &); | ||
94 | public: | ||
95 | gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { } | ||
96 | private: | ||
97 | gzofstream &(*func)(gzofstream &, T); | ||
98 | T val; | ||
99 | }; | ||
100 | |||
101 | template<class T> gzofstream &operator<<(gzofstream &s, | ||
102 | const gzomanip<T> &m) { | ||
103 | return (*m.func)(s, m.val); | ||
104 | |||
105 | } | ||
106 | |||
107 | inline gzofstream &setcompressionlevel( gzofstream &s, int l ) { | ||
108 | (s.rdbuf())->setcompressionlevel(l); | ||
109 | return s; | ||
110 | } | ||
111 | |||
112 | inline gzofstream &setcompressionstrategy( gzofstream &s, int l ) { | ||
113 | (s.rdbuf())->setcompressionstrategy(l); | ||
114 | return s; | ||
115 | } | ||
116 | |||
117 | inline gzomanip<int> setcompressionlevel(int l) | ||
118 | { | ||
119 | return gzomanip<int>(&setcompressionlevel,l); | ||
120 | } | ||
121 | |||
122 | inline gzomanip<int> setcompressionstrategy(int l) | ||
123 | { | ||
124 | return gzomanip<int>(&setcompressionstrategy,l); | ||
125 | } | ||
126 | |||
127 | #endif | ||
128 | |||
129 | |||
130 | |||
131 | |||
132 | |||
133 | |||
134 | |||
135 | |||
136 | |||
137 | |||
138 | |||
139 | |||
140 | |||
141 | |||
142 | |||