4
* Christian Michelsen Research AS
6
* Fantoftvegen 38, 5036 BERGEN, Norway
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.
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 $
27
#include <strstream.h>
35
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
37
# define SET_BINARY_MODE(file)
42
zstringlen(class izstream&);
43
zstringlen(class ozstream&, const char*);
44
size_t value() const { return val.word; }
46
struct Val { unsigned char byte; size_t word; } val;
49
// ----------------------------- izstream -----------------------------
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(); }
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).
65
void open(const char* name) {
67
m_fp = ::gzopen(name, "rb");
73
m_fp = ::gzdopen(fileno(fp), "rb");
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).
81
int r = ::gzclose(m_fp);
85
/* Binary read the given number of bytes from the compressed file.
87
int read(void* buf, size_t len) {
88
return ::gzread(m_fp, buf, len);
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.
97
const char* error(int* errnum) {
98
return ::gzerror(m_fp, errnum);
101
gzFile fp() { return m_fp; }
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).
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));
120
* Binary input with the '>' operator.
123
inline izstream& operator>(izstream& zs, T& x) {
124
::gzread(zs.fp(), &x, sizeof(T));
129
inline zstringlen::zstringlen(izstream& zs) {
131
if (val.byte == 255) zs > val.word;
132
else val.word = val.byte;
136
* Read length of string + the string with the '>' operator.
138
inline izstream& operator>(izstream& zs, char* x) {
140
::gzread(zs.fp(), x, len.value());
141
x[len.value()] = '\0';
145
inline char* read_string(izstream& zs) {
147
char* x = new char[len.value()+1];
148
::gzread(zs.fp(), x, len.value());
149
x[len.value()] = '\0';
153
// ----------------------------- ozstream -----------------------------
158
ozstream() : m_fp(0), m_os(0) {
160
ozstream(FILE* fp, int level = Z_DEFAULT_COMPRESSION)
164
ozstream(const char* name, int level = Z_DEFAULT_COMPRESSION)
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).
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;
181
m_fp = ::gzopen(name, mode);
184
/* open from a FILE pointer.
186
void open(FILE* fp, int level = Z_DEFAULT_COMPRESSION) {
188
char mode[4] = "wb\0";
189
if (level != Z_DEFAULT_COMPRESSION) mode[2] = '0'+level;
191
m_fp = ::gzdopen(fileno(fp), mode);
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).
200
::gzwrite(m_fp, m_os->str(), m_os->pcount());
201
delete[] m_os->str(); delete m_os; m_os = 0;
203
int r = ::gzclose(m_fp); m_fp = 0; return r;
206
/* Binary write the given number of bytes into the compressed file.
208
int write(const void* buf, size_t len) {
209
return ::gzwrite(m_fp, (voidp) buf, len);
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.
219
int flush(int _flush) {
221
return ::gzflush(m_fp, _flush);
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.
230
const char* error(int* errnum) {
231
return ::gzerror(m_fp, errnum);
234
gzFile fp() { return m_fp; }
237
if (m_os == 0) m_os = new ostrstream;
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;
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).
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));
269
* Binary output with the '<' operator.
272
inline ozstream& operator<(ozstream& zs, const T& x) {
273
::gzwrite(zs.fp(), (voidp) &x, sizeof(T));
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);
284
* Write length of string + the string with the '<' operator.
286
inline ozstream& operator<(ozstream& zs, const char* x) {
287
zstringlen len(zs, x);
288
::gzwrite(zs.fp(), (voidp) x, len.value());
293
inline ozstream& operator<(ozstream& zs, char* const& x) {
294
return zs < (const char*) x;
299
* Ascii write with the << operator;
302
inline ostream& operator<<(ozstream& zs, const T& x) {