1 // A C++ I/O streams interface to the zlib gz* functions
3 // Written by Bernie Bright, 1998
4 // Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
6 // Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "zfstream.hxx"
28 // Construct a gzfilebuf object.
29 // Allocate memory for 'get' buffer and zero all buffer pointers.
31 gzfilebuf::gzfilebuf()
34 #if defined( __MWERKS__ )
35 mode(ios_openmode(0)),
39 own_file_descriptor(false),
44 ibuf_size = page_size / sizeof(char);
45 ibuffer = new char [ibuf_size];
50 // Null get and set pointers.
55 gzfilebuf::~gzfilebuf()
58 if ( own_file_descriptor )
64 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
66 // memset( char_mode, '\0', 10 );
67 // char* p = char_mode;
69 if ( io_mode & ios_in )
74 else if ( io_mode & ios_app )
85 if ( io_mode & ios_binary )
91 // Hard code the compression level
92 if ( io_mode & (ios_out | ios_app) )
101 gzfilebuf::open( const char *name, ios_openmode io_mode )
107 cvt_iomode( char_mode, io_mode );
108 if ( (file = gzopen(name, char_mode)) == NULL )
111 own_file_descriptor = true;
117 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
123 cvt_iomode( char_mode, io_mode );
124 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
127 own_file_descriptor = false;
146 // gzfilebuf::setcompressionlevel( int comp_level )
148 // return gzsetparams(file, comp_level, -2);
152 // gzfilebuf::setcompressionstrategy( int comp_strategy )
154 // return gzsetparams(file, -2, comp_strategy);
159 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
161 return streampos(EOF);
165 gzfilebuf::overflow( int_type )
168 if ( !is_open() || !(mode & ios::out) )
173 if ( allocate() == EOF )
186 if (flushbuf() == EOF)
192 setp( base(), base() + bl);
209 if ( pptr() != 0 && pptr() > pbase() )
216 gzfilebuf::flushbuf()
221 if ( gzwrite( file, q, n) < n )
222 return traits_type::eof();
230 gzfilebuf::underflow()
232 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
233 // Error if the file not open for reading.
234 if ( !is_open() || !(mode & ios_in) )
235 return traits_type::eof();
237 // If the input buffer is empty then try to fill it.
238 if ( gptr() != 0 && gptr() < egptr() )
240 return int_type(*gptr());
244 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
249 // Load the input buffer from the underlying gz file.
250 // Returns number of characters read, or EOF.
255 int t = gzread( file, ibuffer, ibuf_size );
263 // Set the input (get) pointers
264 setg( ibuffer, ibuffer, ibuffer+t );
266 // cerr << "gzfilebuf::fillbuf():"
268 // << ", ibuffer=" << (void*)ibuffer
269 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
275 gzifstream::gzifstream()
276 : istream(&buffer), buffer()
281 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
282 : istream(&buffer), buffer()
284 this->open( name, io_mode );
287 gzifstream::gzifstream( int fd, ios_openmode io_mode )
288 : istream(&buffer), buffer()
290 buffer.attach( fd, io_mode );
293 gzifstream::~gzifstream()
298 gzifstream::open( const char *name, ios_openmode io_mode )
300 if ( !buffer.open( name, io_mode ) )
301 clear( ios_failbit | ios_badbit );
309 if ( !buffer.close() )
310 clear( ios_failbit | ios_badbit );