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() { }
|
---|