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