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 '')
-rw-r--r-- | contrib/iostream/test.cpp | 24 | ||||
-rw-r--r-- | contrib/iostream/zfstream.cpp | 329 | ||||
-rw-r--r-- | contrib/iostream/zfstream.h | 142 | ||||
-rw-r--r-- | contrib/iostream2/zstream.h | 307 | ||||
-rw-r--r-- | contrib/iostream2/zstream_test.cpp | 25 |
5 files changed, 827 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 | |||
diff --git a/contrib/iostream2/zstream.h b/contrib/iostream2/zstream.h new file mode 100644 index 0000000..43d2332 --- /dev/null +++ b/contrib/iostream2/zstream.h | |||
@@ -0,0 +1,307 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright (c) 1997 | ||
4 | * Christian Michelsen Research AS | ||
5 | * Advanced Computing | ||
6 | * Fantoftvegen 38, 5036 BERGEN, Norway | ||
7 | * http://www.cmr.no | ||
8 | * | ||
9 | * Permission to use, copy, modify, distribute and sell this software | ||
10 | * and its documentation for any purpose is hereby granted without fee, | ||
11 | * provided that the above copyright notice appear in all copies and | ||
12 | * that both that copyright notice and this permission notice appear | ||
13 | * in supporting documentation. Christian Michelsen Research AS makes no | ||
14 | * representations about the suitability of this software for any | ||
15 | * purpose. It is provided "as is" without express or implied warranty. | ||
16 | * | ||
17 | */ | ||
18 | |||
19 | #ifndef ZSTREAM__H | ||
20 | #define ZSTREAM__H | ||
21 | |||
22 | /* | ||
23 | * zstream.h - C++ interface to the 'zlib' general purpose compression library | ||
24 | * $Id: zstream.h 1.1 1997-06-25 12:00:56+02 tyge Exp tyge $ | ||
25 | */ | ||
26 | |||
27 | #include <strstream.h> | ||
28 | #include <string.h> | ||
29 | #include <stdio.h> | ||
30 | #include "zlib.h" | ||
31 | |||
32 | #if defined(_WIN32) | ||
33 | # include <fcntl.h> | ||
34 | # include <io.h> | ||
35 | # define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY) | ||
36 | #else | ||
37 | # define SET_BINARY_MODE(file) | ||
38 | #endif | ||
39 | |||
40 | class zstringlen { | ||
41 | public: | ||
42 | zstringlen(class izstream&); | ||
43 | zstringlen(class ozstream&, const char*); | ||
44 | size_t value() const { return val.word; } | ||
45 | private: | ||
46 | struct Val { unsigned char byte; size_t word; } val; | ||
47 | }; | ||
48 | |||
49 | // ----------------------------- izstream ----------------------------- | ||
50 | |||
51 | class izstream | ||
52 | { | ||
53 | public: | ||
54 | izstream() : m_fp(0) {} | ||
55 | izstream(FILE* fp) : m_fp(0) { open(fp); } | ||
56 | izstream(const char* name) : m_fp(0) { open(name); } | ||
57 | ~izstream() { close(); } | ||
58 | |||
59 | /* Opens a gzip (.gz) file for reading. | ||
60 | * open() can be used to read a file which is not in gzip format; | ||
61 | * in this case read() will directly read from the file without | ||
62 | * decompression. errno can be checked to distinguish two error | ||
63 | * cases (if errno is zero, the zlib error is Z_MEM_ERROR). | ||
64 | */ | ||
65 | void open(const char* name) { | ||
66 | if (m_fp) close(); | ||
67 | m_fp = ::gzopen(name, "rb"); | ||
68 | } | ||
69 | |||
70 | void open(FILE* fp) { | ||
71 | SET_BINARY_MODE(fp); | ||
72 | if (m_fp) close(); | ||
73 | m_fp = ::gzdopen(fileno(fp), "rb"); | ||
74 | } | ||
75 | |||
76 | /* Flushes all pending input if necessary, closes the compressed file | ||
77 | * and deallocates all the (de)compression state. The return value is | ||
78 | * the zlib error number (see function error() below). | ||
79 | */ | ||
80 | int close() { | ||
81 | int r = ::gzclose(m_fp); | ||
82 | m_fp = 0; return r; | ||
83 | } | ||
84 | |||
85 | /* Binary read the given number of bytes from the compressed file. | ||
86 | */ | ||
87 | int read(void* buf, size_t len) { | ||
88 | return ::gzread(m_fp, buf, len); | ||
89 | } | ||
90 | |||
91 | /* Returns the error message for the last error which occurred on the | ||
92 | * given compressed file. errnum is set to zlib error number. If an | ||
93 | * error occurred in the file system and not in the compression library, | ||
94 | * errnum is set to Z_ERRNO and the application may consult errno | ||
95 | * to get the exact error code. | ||
96 | */ | ||
97 | const char* error(int* errnum) { | ||
98 | return ::gzerror(m_fp, errnum); | ||
99 | } | ||
100 | |||
101 | gzFile fp() { return m_fp; } | ||
102 | |||
103 | private: | ||
104 | gzFile m_fp; | ||
105 | }; | ||
106 | |||
107 | /* | ||
108 | * Binary read the given (array of) object(s) from the compressed file. | ||
109 | * If the input file was not in gzip format, read() copies the objects number | ||
110 | * of bytes into the buffer. | ||
111 | * returns the number of uncompressed bytes actually read | ||
112 | * (0 for end of file, -1 for error). | ||
113 | */ | ||
114 | template <class T, class Items> | ||
115 | inline int read(izstream& zs, T* x, Items items) { | ||
116 | return ::gzread(zs.fp(), x, items*sizeof(T)); | ||
117 | } | ||
118 | |||
119 | /* | ||
120 | * Binary input with the '>' operator. | ||
121 | */ | ||
122 | template <class T> | ||
123 | inline izstream& operator>(izstream& zs, T& x) { | ||
124 | ::gzread(zs.fp(), &x, sizeof(T)); | ||
125 | return zs; | ||
126 | } | ||
127 | |||
128 | |||
129 | inline zstringlen::zstringlen(izstream& zs) { | ||
130 | zs > val.byte; | ||
131 | if (val.byte == 255) zs > val.word; | ||
132 | else val.word = val.byte; | ||
133 | } | ||
134 | |||
135 | /* | ||
136 | * Read length of string + the string with the '>' operator. | ||
137 | */ | ||
138 | inline izstream& operator>(izstream& zs, char* x) { | ||
139 | zstringlen len(zs); | ||
140 | ::gzread(zs.fp(), x, len.value()); | ||
141 | x[len.value()] = '\0'; | ||
142 | return zs; | ||
143 | } | ||
144 | |||
145 | inline char* read_string(izstream& zs) { | ||
146 | zstringlen len(zs); | ||
147 | char* x = new char[len.value()+1]; | ||
148 | ::gzread(zs.fp(), x, len.value()); | ||
149 | x[len.value()] = '\0'; | ||
150 | return x; | ||
151 | } | ||
152 | |||
153 | // ----------------------------- ozstream ----------------------------- | ||
154 | |||
155 | class ozstream | ||
156 | { | ||
157 | public: | ||
158 | ozstream() : m_fp(0), m_os(0) { | ||
159 | } | ||
160 | ozstream(FILE* fp, int level = Z_DEFAULT_COMPRESSION) | ||
161 | : m_fp(0), m_os(0) { | ||
162 | open(fp, level); | ||
163 | } | ||
164 | ozstream(const char* name, int level = Z_DEFAULT_COMPRESSION) | ||
165 | : m_fp(0), m_os(0) { | ||
166 | open(name, level); | ||
167 | } | ||
168 | ~ozstream() { | ||
169 | close(); | ||
170 | } | ||
171 | |||
172 | /* Opens a gzip (.gz) file for writing. | ||
173 | * The compression level parameter should be in 0..9 | ||
174 | * errno can be checked to distinguish two error cases | ||
175 | * (if errno is zero, the zlib error is Z_MEM_ERROR). | ||
176 | */ | ||
177 | void open(const char* name, int level = Z_DEFAULT_COMPRESSION) { | ||
178 | char mode[4] = "wb\0"; | ||
179 | if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level; | ||
180 | if (m_fp) close(); | ||
181 | m_fp = ::gzopen(name, mode); | ||
182 | } | ||
183 | |||
184 | /* open from a FILE pointer. | ||
185 | */ | ||
186 | void open(FILE* fp, int level = Z_DEFAULT_COMPRESSION) { | ||
187 | SET_BINARY_MODE(fp); | ||
188 | char mode[4] = "wb\0"; | ||
189 | if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level; | ||
190 | if (m_fp) close(); | ||
191 | m_fp = ::gzdopen(fileno(fp), mode); | ||
192 | } | ||
193 | |||
194 | /* Flushes all pending output if necessary, closes the compressed file | ||
195 | * and deallocates all the (de)compression state. The return value is | ||
196 | * the zlib error number (see function error() below). | ||
197 | */ | ||
198 | int close() { | ||
199 | if (m_os) { | ||
200 | ::gzwrite(m_fp, m_os->str(), m_os->pcount()); | ||
201 | delete[] m_os->str(); delete m_os; m_os = 0; | ||
202 | } | ||
203 | int r = ::gzclose(m_fp); m_fp = 0; return r; | ||
204 | } | ||
205 | |||
206 | /* Binary write the given number of bytes into the compressed file. | ||
207 | */ | ||
208 | int write(const void* buf, size_t len) { | ||
209 | return ::gzwrite(m_fp, (voidp) buf, len); | ||
210 | } | ||
211 | |||
212 | /* Flushes all pending output into the compressed file. The parameter | ||
213 | * _flush is as in the deflate() function. The return value is the zlib | ||
214 | * error number (see function gzerror below). flush() returns Z_OK if | ||
215 | * the flush_ parameter is Z_FINISH and all output could be flushed. | ||
216 | * flush() should be called only when strictly necessary because it can | ||
217 | * degrade compression. | ||
218 | */ | ||
219 | int flush(int _flush) { | ||
220 | os_flush(); | ||
221 | return ::gzflush(m_fp, _flush); | ||
222 | } | ||
223 | |||
224 | /* Returns the error message for the last error which occurred on the | ||
225 | * given compressed file. errnum is set to zlib error number. If an | ||
226 | * error occurred in the file system and not in the compression library, | ||
227 | * errnum is set to Z_ERRNO and the application may consult errno | ||
228 | * to get the exact error code. | ||
229 | */ | ||
230 | const char* error(int* errnum) { | ||
231 | return ::gzerror(m_fp, errnum); | ||
232 | } | ||
233 | |||
234 | gzFile fp() { return m_fp; } | ||
235 | |||
236 | ostream& os() { | ||
237 | if (m_os == 0) m_os = new ostrstream; | ||
238 | return *m_os; | ||
239 | } | ||
240 | |||
241 | void os_flush() { | ||
242 | if (m_os && m_os->pcount()>0) { | ||
243 | ostrstream* oss = new ostrstream; | ||
244 | oss->fill(m_os->fill()); | ||
245 | oss->flags(m_os->flags()); | ||
246 | oss->precision(m_os->precision()); | ||
247 | oss->width(m_os->width()); | ||
248 | ::gzwrite(m_fp, m_os->str(), m_os->pcount()); | ||
249 | delete[] m_os->str(); delete m_os; m_os = oss; | ||
250 | } | ||
251 | } | ||
252 | |||
253 | private: | ||
254 | gzFile m_fp; | ||
255 | ostrstream* m_os; | ||
256 | }; | ||
257 | |||
258 | /* | ||
259 | * Binary write the given (array of) object(s) into the compressed file. | ||
260 | * returns the number of uncompressed bytes actually written | ||
261 | * (0 in case of error). | ||
262 | */ | ||
263 | template <class T, class Items> | ||
264 | inline int write(ozstream& zs, const T* x, Items items) { | ||
265 | return ::gzwrite(zs.fp(), (voidp) x, items*sizeof(T)); | ||
266 | } | ||
267 | |||
268 | /* | ||
269 | * Binary output with the '<' operator. | ||
270 | */ | ||
271 | template <class T> | ||
272 | inline ozstream& operator<(ozstream& zs, const T& x) { | ||
273 | ::gzwrite(zs.fp(), (voidp) &x, sizeof(T)); | ||
274 | return zs; | ||
275 | } | ||
276 | |||
277 | inline zstringlen::zstringlen(ozstream& zs, const char* x) { | ||
278 | val.byte = 255; val.word = ::strlen(x); | ||
279 | if (val.word < 255) zs < (val.byte = val.word); | ||
280 | else zs < val; | ||
281 | } | ||
282 | |||
283 | /* | ||
284 | * Write length of string + the string with the '<' operator. | ||
285 | */ | ||
286 | inline ozstream& operator<(ozstream& zs, const char* x) { | ||
287 | zstringlen len(zs, x); | ||
288 | ::gzwrite(zs.fp(), (voidp) x, len.value()); | ||
289 | return zs; | ||
290 | } | ||
291 | |||
292 | #ifdef _MSC_VER | ||
293 | inline ozstream& operator<(ozstream& zs, char* const& x) { | ||
294 | return zs < (const char*) x; | ||
295 | } | ||
296 | #endif | ||
297 | |||
298 | /* | ||
299 | * Ascii write with the << operator; | ||
300 | */ | ||
301 | template <class T> | ||
302 | inline ostream& operator<<(ozstream& zs, const T& x) { | ||
303 | zs.os_flush(); | ||
304 | return zs.os() << x; | ||
305 | } | ||
306 | |||
307 | #endif | ||
diff --git a/contrib/iostream2/zstream_test.cpp b/contrib/iostream2/zstream_test.cpp new file mode 100644 index 0000000..5bbd56c --- /dev/null +++ b/contrib/iostream2/zstream_test.cpp | |||
@@ -0,0 +1,25 @@ | |||
1 | #include "zstream.h" | ||
2 | #include <math.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <iomanip.h> | ||
5 | |||
6 | void main() { | ||
7 | char h[256] = "Hello"; | ||
8 | char* g = "Goodbye"; | ||
9 | ozstream out("temp.gz"); | ||
10 | out < "This works well" < h < g; | ||
11 | out.close(); | ||
12 | |||
13 | izstream in("temp.gz"); // read it back | ||
14 | char *x = read_string(in), *y = new char[256], z[256]; | ||
15 | in > y > z; | ||
16 | in.close(); | ||
17 | cout << x << endl << y << endl << z << endl; | ||
18 | |||
19 | out.open("temp.gz"); // try ascii output; zcat temp.gz to see the results | ||
20 | out << setw(50) << setfill('#') << setprecision(20) << x << endl << y << endl << z << endl; | ||
21 | out << z << endl << y << endl << x << endl; | ||
22 | out << 1.1234567890123456789 << endl; | ||
23 | |||
24 | delete[] x; delete[] y; | ||
25 | } | ||