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 library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Library General Public
10 // License as published by the Free Software Foundation; either
11 // version 2 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Library General Public License for more details.
18 // You should have received a copy of the GNU Library General Public
19 // License along with this library; if not, write to the
20 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 // Boston, MA 02111-1307, USA.
26 #include "zfstream.hxx"
29 // Construct a gzfilebuf object.
30 // Allocate memory for 'get' buffer and zero all buffer pointers.
32 gzfilebuf::gzfilebuf()
35 #if defined( __MWERKS__ )
36 mode(ios_openmode(0)),
40 own_file_descriptor(false),
45 ibuf_size = page_size / sizeof(char);
46 ibuffer = new char [ibuf_size];
51 // Null get and set pointers.
56 gzfilebuf::~gzfilebuf()
59 if ( own_file_descriptor )
65 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
67 // memset( char_mode, '\0', 10 );
68 // char* p = char_mode;
70 if ( io_mode & ios_in )
75 else if ( io_mode & ios_app )
86 if ( io_mode & ios_binary )
92 // Hard code the compression level
93 if ( io_mode & (ios_out | ios_app) )
102 gzfilebuf::open( const char *name, ios_openmode io_mode )
108 cvt_iomode( char_mode, io_mode );
109 if ( (file = gzopen(name, char_mode)) == NULL )
112 own_file_descriptor = true;
118 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
124 cvt_iomode( char_mode, io_mode );
125 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
128 own_file_descriptor = false;
147 // gzfilebuf::setcompressionlevel( int comp_level )
149 // return gzsetparams(file, comp_level, -2);
153 // gzfilebuf::setcompressionstrategy( int comp_strategy )
155 // return gzsetparams(file, -2, comp_strategy);
160 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
162 return streampos(EOF);
166 gzfilebuf::overflow( int_type )
169 if ( !is_open() || !(mode & ios::out) )
174 if ( allocate() == EOF )
187 if (flushbuf() == EOF)
193 setp( base(), base() + bl);
210 if ( pptr() != 0 && pptr() > pbase() )
217 gzfilebuf::flushbuf()
222 if ( gzwrite( file, q, n) < n )
223 return traits_type::eof();
231 gzfilebuf::underflow()
233 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
234 // Error if the file not open for reading.
235 if ( !is_open() || !(mode & ios_in) )
236 return traits_type::eof();
238 // If the input buffer is empty then try to fill it.
239 if ( gptr() != 0 && gptr() < egptr() )
241 return int_type(*gptr());
245 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
250 // Load the input buffer from the underlying gz file.
251 // Returns number of characters read, or EOF.
256 int t = gzread( file, ibuffer, ibuf_size );
264 // Set the input (get) pointers
265 setg( ibuffer, ibuffer, ibuffer+t );
267 // cerr << "gzfilebuf::fillbuf():"
269 // << ", ibuffer=" << (void*)ibuffer
270 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
276 gzifstream::gzifstream()
277 : istream(&buffer), buffer()
282 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
283 : istream(&buffer), buffer()
285 this->open( name, io_mode );
288 gzifstream::gzifstream( int fd, ios_openmode io_mode )
289 : istream(&buffer), buffer()
291 buffer.attach( fd, io_mode );
294 gzifstream::~gzifstream()
299 gzifstream::open( const char *name, ios_openmode io_mode )
301 if ( !buffer.open( name, io_mode ) )
302 clear( ios_failbit | ios_badbit );
310 if ( !buffer.close() )
311 clear( ios_failbit | ios_badbit );