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()
35 own_file_descriptor(false),
40 ibuf_size = page_size / sizeof(char);
41 ibuffer = new char [ibuf_size];
46 // Null get and set pointers.
51 gzfilebuf::~gzfilebuf()
54 if ( own_file_descriptor )
60 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
62 // memset( char_mode, '\0', 10 );
63 // char* p = char_mode;
65 if ( io_mode & ios_in )
70 else if ( io_mode & ios_app )
81 if ( io_mode & ios_binary )
87 // Hard code the compression level
88 if ( io_mode & (ios_out | ios_app) )
97 gzfilebuf::open( const char *name, ios_openmode io_mode )
103 cvt_iomode( char_mode, io_mode );
104 if ( (file = gzopen(name, char_mode)) == NULL )
107 own_file_descriptor = true;
113 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
119 cvt_iomode( char_mode, io_mode );
120 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
123 own_file_descriptor = false;
142 // gzfilebuf::setcompressionlevel( int comp_level )
144 // return gzsetparams(file, comp_level, -2);
148 // gzfilebuf::setcompressionstrategy( int comp_strategy )
150 // return gzsetparams(file, -2, comp_strategy);
155 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
157 return streampos(EOF);
161 gzfilebuf::overflow( int_type )
164 if ( !is_open() || !(mode & ios::out) )
169 if ( allocate() == EOF )
182 if (flushbuf() == EOF)
188 setp( base(), base() + bl);
205 if ( pptr() != 0 && pptr() > pbase() )
212 gzfilebuf::flushbuf()
217 if ( gzwrite( file, q, n) < n )
218 return traits_type::eof();
226 gzfilebuf::underflow()
228 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
229 // Error if the file not open for reading.
230 if ( !is_open() || !(mode & ios_in) )
231 return traits_type::eof();
233 // If the input buffer is empty then try to fill it.
234 if ( gptr() != 0 && gptr() < egptr() )
236 return int_type(*gptr());
240 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
245 // Load the input buffer from the underlying gz file.
246 // Returns number of characters read, or EOF.
251 int t = gzread( file, ibuffer, ibuf_size );
259 // Set the input (get) pointers
260 setg( ibuffer, ibuffer, ibuffer+t );
262 // cerr << "gzfilebuf::fillbuf():"
264 // << ", ibuffer=" << (void*)ibuffer
265 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
271 gzifstream::gzifstream()
272 : istream(&buffer), buffer()
277 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
278 : istream(&buffer), buffer()
280 this->open( name, io_mode );
283 gzifstream::gzifstream( int fd, ios_openmode io_mode )
284 : istream(&buffer), buffer()
286 buffer.attach( fd, io_mode );
289 gzifstream::~gzifstream()
294 gzifstream::open( const char *name, ios_openmode io_mode )
296 if ( !buffer.open( name, io_mode ) )
297 clear( ios_failbit | ios_badbit );
305 if ( !buffer.close() )
306 clear( ios_failbit | ios_badbit );