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.
25 #include <simgear/compiler.h>
27 #ifdef FG_HAVE_STD_INCLUDES
35 #include "zfstream.hxx"
38 // Construct a gzfilebuf object.
39 // Allocate memory for 'get' buffer and zero all buffer pointers.
41 gzfilebuf::gzfilebuf()
44 #if defined( __MWERKS__ )
45 mode(ios_openmode(0)),
49 own_file_descriptor(false),
54 ibuf_size = page_size / sizeof(char);
55 ibuffer = new char [ibuf_size];
60 // Null get and set pointers.
65 gzfilebuf::~gzfilebuf()
68 if ( own_file_descriptor )
74 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
76 // memset( char_mode, '\0', 10 );
77 // char* p = char_mode;
79 if ( io_mode & ios_in )
84 else if ( io_mode & ios_app )
95 if ( io_mode & ios_binary )
101 // Hard code the compression level
102 if ( io_mode & (ios_out | ios_app) )
111 gzfilebuf::open( const char *name, ios_openmode io_mode )
117 cvt_iomode( char_mode, io_mode );
118 if ( (file = gzopen(name, char_mode)) == NULL ) {
119 perror( "gzfilebuf::open(): " );
124 own_file_descriptor = true;
130 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
136 cvt_iomode( char_mode, io_mode );
137 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL ) {
138 perror( "gzfilebuf::attach(): " );
143 own_file_descriptor = false;
151 // cout << "closing ..." ;
157 // cout << "done" << endl;
159 // cout << "error" << endl;
166 // gzfilebuf::setcompressionlevel( int comp_level )
168 // return gzsetparams(file, comp_level, -2);
172 // gzfilebuf::setcompressionstrategy( int comp_strategy )
174 // return gzsetparams(file, -2, comp_strategy);
179 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
181 return streampos(EOF);
185 gzfilebuf::overflow( int_type )
188 if ( !is_open() || !(mode & ios::out) )
193 if ( allocate() == EOF )
206 if (flushbuf() == EOF)
212 setp( base(), base() + bl);
229 if ( pptr() != 0 && pptr() > pbase() )
236 gzfilebuf::flushbuf()
241 if ( gzwrite( file, q, n) < n )
242 return traits_type::eof();
250 gzfilebuf::underflow()
252 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
253 // Error if the file not open for reading.
254 if ( !is_open() || !(mode & ios_in) )
255 return traits_type::eof();
257 // If the input buffer is empty then try to fill it.
258 if ( gptr() != 0 && gptr() < egptr() )
260 return int_type(*gptr());
264 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
269 // Load the input buffer from the underlying gz file.
270 // Returns number of characters read, or EOF.
275 int t = gzread( file, ibuffer, ibuf_size );
283 // Set the input (get) pointers
284 setg( ibuffer, ibuffer, ibuffer+t );
286 // cerr << "gzfilebuf::fillbuf():"
288 // << ", ibuffer=" << (void*)ibuffer
289 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
295 gzifstream::gzifstream()
296 : istream(&buffer), buffer()
301 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
302 : istream(&buffer), buffer()
304 this->open( name, io_mode );
307 gzifstream::gzifstream( int fd, ios_openmode io_mode )
308 : istream(&buffer), buffer()
310 buffer.attach( fd, io_mode );
313 gzifstream::~gzifstream()
318 gzifstream::open( const char *name, ios_openmode io_mode )
320 if ( !buffer.open( name, io_mode ) )
321 clear( ios_failbit | ios_badbit );
329 if ( !buffer.close() )
330 clear( ios_failbit | ios_badbit );