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"
28 const int gzfilebuf::page_size;
31 // Construct a gzfilebuf object.
32 // Allocate memory for 'get' buffer and zero all buffer pointers.
34 gzfilebuf::gzfilebuf()
38 own_file_descriptor(false),
43 ibuf_size = page_size / sizeof(char);
44 ibuffer = new char [ibuf_size];
49 // Null get and set pointers.
54 gzfilebuf::~gzfilebuf()
57 if ( own_file_descriptor )
63 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
65 // memset( char_mode, '\0', 10 );
66 // char* p = char_mode;
68 if ( io_mode & ios_in )
73 else if ( io_mode & ios_app )
84 if ( io_mode & ios_binary )
90 // Hard code the compression level
91 if ( io_mode & (ios_out | ios_app) )
100 gzfilebuf::open( const char *name, ios_openmode io_mode )
106 cvt_iomode( char_mode, io_mode );
107 if ( (file = gzopen(name, char_mode)) == NULL )
110 own_file_descriptor = true;
116 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
122 cvt_iomode( char_mode, io_mode );
123 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
126 own_file_descriptor = false;
145 // gzfilebuf::setcompressionlevel( int comp_level )
147 // return gzsetparams(file, comp_level, -2);
151 // gzfilebuf::setcompressionstrategy( int comp_strategy )
153 // return gzsetparams(file, -2, comp_strategy);
158 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
160 return streampos(EOF);
164 gzfilebuf::overflow( int_type )
167 if ( !is_open() || !(mode & ios::out) )
172 if ( allocate() == EOF )
185 if (flushbuf() == EOF)
191 setp( base(), base() + bl);
208 if ( pptr() != 0 && pptr() > pbase() )
215 gzfilebuf::flushbuf()
220 if ( gzwrite( file, q, n) < n )
221 return traits_type::eof();
229 gzfilebuf::underflow()
231 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
232 // Error if the file not open for reading.
233 if ( !is_open() || !(mode & ios_in) )
234 return traits_type::eof();
236 // If the input buffer is empty then try to fill it.
237 if ( gptr() != 0 && gptr() < egptr() )
239 return int_type(*gptr());
243 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
248 // Load the input buffer from the underlying gz file.
249 // Returns number of characters read, or EOF.
254 int t = gzread( file, ibuffer, ibuf_size );
262 // Set the input (get) pointers
263 setg( ibuffer, ibuffer, ibuffer+t );
265 // cerr << "gzfilebuf::fillbuf():"
267 // << ", ibuffer=" << (void*)ibuffer
268 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
274 gzifstream::gzifstream()
275 : istream(&buffer), buffer()
280 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
281 : istream(&buffer), buffer()
283 this->open( name, io_mode );
286 gzifstream::gzifstream( int fd, ios_openmode io_mode )
287 : istream(&buffer), buffer()
289 buffer.attach( fd, io_mode );
292 gzifstream::~gzifstream()
297 gzifstream::open( const char *name, ios_openmode io_mode )
299 if ( !buffer.open( name, io_mode ) )
300 clear( ios_failbit | ios_badbit );
308 if ( !buffer.close() )
309 clear( ios_failbit | ios_badbit );
314 // Revision 1.2 1998/11/06 14:05:14 curt
315 // More portability improvements by Bernie Bright.