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>
30 #include "zfstream.hxx"
33 // Construct a gzfilebuf object.
34 // Allocate memory for 'get' buffer and zero all buffer pointers.
36 gzfilebuf::gzfilebuf()
39 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 ) {
110 // perror( "gzfilebuf::open(): " );
115 own_file_descriptor = true;
121 gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
127 cvt_iomode( char_mode, io_mode );
128 if ( (file = gzdopen(file_descriptor, char_mode)) == NULL ) {
129 perror( "gzfilebuf::attach(): " );
134 own_file_descriptor = false;
142 // cout << "closing ..." ;
148 // cout << "done" << endl;
150 // cout << "error" << endl;
157 // gzfilebuf::setcompressionlevel( int comp_level )
159 // return gzsetparams(file, comp_level, -2);
163 // gzfilebuf::setcompressionstrategy( int comp_strategy )
165 // return gzsetparams(file, -2, comp_strategy);
170 gzfilebuf::seekoff( std::streamoff, ios_seekdir, ios_openmode )
172 return std::streampos(EOF);
176 gzfilebuf::overflow( int_type )
179 if ( !is_open() || !(mode & ios::out) )
184 if ( allocate() == EOF )
197 if (flushbuf() == EOF)
203 setp( base(), base() + bl);
220 if ( pptr() != 0 && pptr() > pbase() )
227 gzfilebuf::flushbuf()
232 if ( gzwrite( file, q, n) < n )
233 return traits_type::eof();
241 gzfilebuf::underflow()
243 // cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
244 // Error if the file not open for reading.
245 if ( !is_open() || !(mode & ios_in) )
246 return traits_type::eof();
248 // If the input buffer is empty then try to fill it.
249 if ( gptr() != 0 && gptr() < egptr() )
251 return int_type(*gptr());
255 return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
260 // Load the input buffer from the underlying gz file.
261 // Returns number of characters read, or EOF.
266 int t = gzread( file, ibuffer, ibuf_size );
274 // Set the input (get) pointers
275 setg( ibuffer, ibuffer, ibuffer+t );
277 // cerr << "gzfilebuf::fillbuf():"
279 // << ", ibuffer=" << (void*)ibuffer
280 // << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
286 gzifstream::gzifstream()
287 : istream(&buffer), buffer()
292 gzifstream::gzifstream( const char *name, ios_openmode io_mode )
293 : istream(&buffer), buffer()
295 this->open( name, io_mode );
298 gzifstream::gzifstream( int fd, ios_openmode io_mode )
299 : istream(&buffer), buffer()
301 buffer.attach( fd, io_mode );
304 gzifstream::~gzifstream()
309 gzifstream::open( const char *name, ios_openmode io_mode )
311 if ( !buffer.open( name, io_mode ) )
312 clear( ios_failbit | ios_badbit );
320 if ( !buffer.close() )
321 clear( ios_failbit | ios_badbit );