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 General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include <simgear/compiler.h>
26 #ifdef SG_HAVE_STD_INCLUDES
34 #include "zfstream.hxx"
37 // Construct a gzfilebuf object.
38 // Allocate memory for 'get' buffer and zero all buffer pointers.
40 gzfilebuf::gzfilebuf()
43 #if defined( __MWERKS__ ) || __GNUC__ > 2
44 mode(ios_openmode(0)),
48 own_file_descriptor(false),
53 ibuf_size = page_size / sizeof(char);
54 ibuffer = new char [ibuf_size];
59 // Null get and set pointers.
64 gzfilebuf::~gzfilebuf()
67 if ( own_file_descriptor )
73 gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
75 // memset( char_mode, '\0', 10 );
76 // char* p = char_mode;
78 if ( io_mode & ios_in )
83 else if ( io_mode & ios_app )
94 if ( io_mode & ios_binary )
100 // Hard code the compression level
101 if ( io_mode & (ios_out | ios_app) )
110 gzfilebuf::open( const char *name, ios_openmode io_mode )
116 cvt_iomode( char_mode, io_mode );
117 if ( (file = gzopen(name, char_mode)) == NULL ) {
118 // perror( "gzfilebuf::open(): " );
123 own_file_descriptor = true;
129 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
135 cvt_iomode( char_mode, io_mode );
136 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL ) {
137 perror( "gzfilebuf::attach(): " );
142 own_file_descriptor = false;
150 // cout << "closing ..." ;
156 // cout << "done" << endl;
158 // cout << "error" << endl;
165 // gzfilebuf::setcompressionlevel( int comp_level )
167 // return gzsetparams(file, comp_level, -2);
171 // gzfilebuf::setcompressionstrategy( int comp_strategy )
173 // return gzsetparams(file, -2, comp_strategy);
178 gzfilebuf::seekoff( streamoff, ios_seekdir, int )
180 return streampos(EOF);
184 gzfilebuf::overflow( int_type )
187 if ( !is_open() || !(mode & ios::out) )
192 if ( allocate() == EOF )
205 if (flushbuf() == EOF)
211 setp( base(), base() + bl);
228 if ( pptr() != 0 && pptr() > pbase() )
235 gzfilebuf::flushbuf()
240 if ( gzwrite( file, q, n) < n )
241 return traits_type::eof();
249 gzfilebuf::underflow()
251 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
252 // Error if the file not open for reading.
253 if ( !is_open() || !(mode & ios_in) )
254 return traits_type::eof();
256 // If the input buffer is empty then try to fill it.
257 if ( gptr() != 0 && gptr() < egptr() )
259 return int_type(*gptr());
263 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
268 // Load the input buffer from the underlying gz file.
269 // Returns number of characters read, or EOF.
274 int t = gzread( file, ibuffer, ibuf_size );
282 // Set the input (get) pointers
283 setg( ibuffer, ibuffer, ibuffer+t );
285 // cerr << "gzfilebuf::fillbuf():"
287 // << ", ibuffer=" << (void*)ibuffer
288 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
294 gzifstream::gzifstream()
295 : istream(&buffer), buffer()
300 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
301 : istream(&buffer), buffer()
303 this->open( name, io_mode );
306 gzifstream::gzifstream( int fd, ios_openmode io_mode )
307 : istream(&buffer), buffer()
309 buffer.attach( fd, io_mode );
312 gzifstream::~gzifstream()
317 gzifstream::open( const char *name, ios_openmode io_mode )
319 if ( !buffer.open( name, io_mode ) )
320 clear( ios_failbit | ios_badbit );
328 if ( !buffer.close() )
329 clear( ios_failbit | ios_badbit );