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.
23 // (Log is kept at end of this file)
26 #include "zfstream.hxx"
29 // Construct a gzfilebuf object.
30 // Allocate memory for 'get' buffer and zero all buffer pointers.
32 gzfilebuf::gzfilebuf()
36 own_file_descriptor(false),
41 ibuf_size = page_size / sizeof(char);
42 ibuffer = new char [ibuf_size];
47 // Null get and set pointers.
52 gzfilebuf::~gzfilebuf()
55 if ( own_file_descriptor )
61 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
63 // memset( char_mode, '\0', 10 );
64 // char* p = char_mode;
66 if ( io_mode & ios_in )
71 else if ( io_mode & ios_app )
82 if ( io_mode & ios_binary )
88 // Hard code the compression level
89 if ( io_mode & (ios_out | ios_app) )
98 gzfilebuf::open( const char *name, ios_openmode io_mode )
104 cvt_iomode( char_mode, io_mode );
105 if ( (file = gzopen(name, char_mode)) == NULL )
108 own_file_descriptor = true;
114 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
120 cvt_iomode( char_mode, io_mode );
121 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
124 own_file_descriptor = false;
143 // gzfilebuf::setcompressionlevel( int comp_level )
145 // return gzsetparams(file, comp_level, -2);
149 // gzfilebuf::setcompressionstrategy( int comp_strategy )
151 // return gzsetparams(file, -2, comp_strategy);
156 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
158 return streampos(EOF);
162 gzfilebuf::overflow( int_type )
165 if ( !is_open() || !(mode & ios::out) )
170 if ( allocate() == EOF )
183 if (flushbuf() == EOF)
189 setp( base(), base() + bl);
206 if ( pptr() != 0 && pptr() > pbase() )
213 gzfilebuf::flushbuf()
218 if ( gzwrite( file, q, n) < n )
219 return traits_type::eof();
227 gzfilebuf::underflow()
229 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
230 // Error if the file not open for reading.
231 if ( !is_open() || !(mode & ios_in) )
232 return traits_type::eof();
234 // If the input buffer is empty then try to fill it.
235 if ( gptr() != 0 && gptr() < egptr() )
237 return int_type(*gptr());
241 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
246 // Load the input buffer from the underlying gz file.
247 // Returns number of characters read, or EOF.
252 int t = gzread( file, ibuffer, ibuf_size );
260 // Set the input (get) pointers
261 setg( ibuffer, ibuffer, ibuffer+t );
263 // cerr << "gzfilebuf::fillbuf():"
265 // << ", ibuffer=" << (void*)ibuffer
266 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
272 gzifstream::gzifstream()
273 : istream(&buffer), buffer()
278 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
279 : istream(&buffer), buffer()
281 this->open( name, io_mode );
284 gzifstream::gzifstream( int fd, ios_openmode io_mode )
285 : istream(&buffer), buffer()
287 buffer.attach( fd, io_mode );
290 gzifstream::~gzifstream()
295 gzifstream::open( const char *name, ios_openmode io_mode )
297 if ( !buffer.open( name, io_mode ) )
298 clear( ios_failbit | ios_badbit );
306 if ( !buffer.close() )
307 clear( ios_failbit | ios_badbit );
312 // Revision 1.3 1999/01/19 20:41:48 curt
313 // Portability updates contributed by Bernie Bright.
315 // Revision 1.2 1998/11/06 14:05:14 curt
316 // More portability improvements by Bernie Bright.