1998-11-06 14:05:12 +00:00
|
|
|
// A C++ I/O streams interface to the zlib gz* functions
|
|
|
|
//
|
|
|
|
// Written by Bernie Bright, 1998
|
|
|
|
// Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
|
|
|
|
//
|
|
|
|
// Copyright (C) 1998 Bernie Bright - bbright@c031.aone.net.au
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
|
|
|
// (Log is kept at end of this file)
|
1998-09-01 19:06:28 +00:00
|
|
|
|
|
|
|
#include <memory.h>
|
1998-11-06 14:05:12 +00:00
|
|
|
#include "zfstream.hxx"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Construct a gzfilebuf object.
|
|
|
|
// Allocate memory for 'get' buffer and zero all buffer pointers.
|
|
|
|
//
|
|
|
|
gzfilebuf::gzfilebuf()
|
|
|
|
: streambuf(),
|
|
|
|
file(NULL),
|
|
|
|
mode(0),
|
|
|
|
own_file_descriptor(false),
|
|
|
|
ibuf_size(0),
|
|
|
|
ibuffer(0)
|
|
|
|
{
|
|
|
|
// try {
|
|
|
|
ibuf_size = page_size / sizeof(char);
|
|
|
|
ibuffer = new char [ibuf_size];
|
|
|
|
// } catch (...) {
|
|
|
|
// delete [] ibuffer;
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Null get and set pointers.
|
|
|
|
this->setg(0,0,0);
|
|
|
|
this->setp(0,0);
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf::~gzfilebuf()
|
|
|
|
{
|
1998-09-01 19:06:28 +00:00
|
|
|
sync();
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( own_file_descriptor )
|
|
|
|
this->close();
|
|
|
|
delete [] ibuffer;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
void
|
|
|
|
gzfilebuf::cvt_iomode( char* p, ios_openmode io_mode )
|
|
|
|
{
|
|
|
|
// memset( char_mode, '\0', 10 );
|
|
|
|
// char* p = char_mode;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( io_mode & ios_in )
|
|
|
|
{
|
|
|
|
mode = ios_in;
|
|
|
|
*p++ = 'r';
|
|
|
|
}
|
|
|
|
else if ( io_mode & ios_app )
|
|
|
|
{
|
|
|
|
mode = ios_app;
|
|
|
|
*p++ = 'a';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mode = ios_out;
|
|
|
|
*p++ = 'w';
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( io_mode & ios_binary )
|
|
|
|
{
|
|
|
|
mode |= ios_binary;
|
|
|
|
*p++ = 'b';
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// Hard code the compression level
|
|
|
|
if ( io_mode & (ios_out | ios_app) )
|
|
|
|
{
|
|
|
|
*p++ = '9';
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
*p = '\0';
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf*
|
|
|
|
gzfilebuf::open( const char *name, ios_openmode io_mode )
|
|
|
|
{
|
|
|
|
if ( is_open() )
|
|
|
|
return NULL;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
char char_mode[10];
|
|
|
|
cvt_iomode( char_mode, io_mode );
|
|
|
|
if ( (file = gzopen(name, char_mode)) == NULL )
|
|
|
|
return NULL;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
own_file_descriptor = true;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return this;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf*
|
|
|
|
gzfilebuf::attach( int file_descriptor, ios_openmode io_mode )
|
|
|
|
{
|
|
|
|
if ( is_open() )
|
|
|
|
return NULL;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
char char_mode[10];
|
|
|
|
cvt_iomode( char_mode, io_mode );
|
|
|
|
if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
|
|
|
|
return NULL;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
own_file_descriptor = false;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return this;
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf*
|
|
|
|
gzfilebuf::close()
|
|
|
|
{
|
|
|
|
if ( is_open() )
|
|
|
|
{
|
|
|
|
sync();
|
|
|
|
gzclose( file );
|
|
|
|
file = NULL;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return this;
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// int
|
|
|
|
// gzfilebuf::setcompressionlevel( int comp_level )
|
|
|
|
// {
|
|
|
|
// return gzsetparams(file, comp_level, -2);
|
|
|
|
// }
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// int
|
|
|
|
// gzfilebuf::setcompressionstrategy( int comp_strategy )
|
|
|
|
// {
|
|
|
|
// return gzsetparams(file, -2, comp_strategy);
|
|
|
|
// }
|
1998-09-01 19:06:28 +00:00
|
|
|
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
streampos
|
|
|
|
gzfilebuf::seekoff( streamoff, ios_seekdir, int )
|
|
|
|
{
|
|
|
|
return streampos(EOF);
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf::int_type
|
|
|
|
gzfilebuf::overflow( int_type )
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
if ( !is_open() || !(mode & ios::out) )
|
1998-09-01 19:06:28 +00:00
|
|
|
return EOF;
|
1998-11-06 14:05:12 +00:00
|
|
|
|
|
|
|
if ( !base() )
|
|
|
|
{
|
|
|
|
if ( allocate() == EOF )
|
|
|
|
return EOF;
|
|
|
|
setg(0,0,0);
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
1998-11-06 14:05:12 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (in_avail())
|
|
|
|
{
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out_waiting())
|
|
|
|
{
|
|
|
|
if (flushbuf() == EOF)
|
|
|
|
return EOF;
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
int bl = blen();
|
|
|
|
setp( base(), base() + bl);
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( c != EOF )
|
|
|
|
{
|
|
|
|
*pptr() = c;
|
|
|
|
pbump(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
int
|
|
|
|
gzfilebuf::sync()
|
|
|
|
{
|
|
|
|
if ( !is_open() )
|
|
|
|
return EOF;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( pptr() != 0 && pptr() > pbase() )
|
|
|
|
return flushbuf();
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return 0;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf::int_type
|
|
|
|
gzfilebuf::flushbuf()
|
|
|
|
{
|
|
|
|
char* q = pbase();
|
|
|
|
int n = pptr() - q;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( gzwrite( file, q, n) < n )
|
|
|
|
return traits_type::eof();
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
setp(0,0);
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return 0;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzfilebuf::int_type
|
|
|
|
gzfilebuf::underflow()
|
|
|
|
{
|
|
|
|
// cerr << "gzfilebuf::underflow(): gptr()=" << (void*)gptr() << endl;
|
|
|
|
// Error if the file not open for reading.
|
|
|
|
if ( !is_open() || !(mode & ios_in) )
|
|
|
|
return traits_type::eof();
|
|
|
|
|
|
|
|
// If the input buffer is empty then try to fill it.
|
|
|
|
if ( gptr() != 0 && gptr() < egptr() )
|
|
|
|
{
|
|
|
|
return int_type(*gptr());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return fillbuf() == EOF ? traits_type::eof() : int_type(*gptr());
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
//
|
|
|
|
// Load the input buffer from the underlying gz file.
|
|
|
|
// Returns number of characters read, or EOF.
|
|
|
|
//
|
|
|
|
int
|
|
|
|
gzfilebuf::fillbuf()
|
|
|
|
{
|
|
|
|
int t = gzread( file, ibuffer, ibuf_size );
|
|
|
|
if ( t <= 0)
|
|
|
|
{
|
|
|
|
// disable get area
|
|
|
|
setg(0,0,0);
|
|
|
|
return EOF;
|
|
|
|
}
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// Set the input (get) pointers
|
|
|
|
setg( ibuffer, ibuffer, ibuffer+t );
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// cerr << "gzfilebuf::fillbuf():"
|
|
|
|
// << " t=" << t
|
|
|
|
// << ", ibuffer=" << (void*)ibuffer
|
|
|
|
// << ", ibuffer+t=" << (void*)(ibuffer+t) << endl;
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
return t;
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
#if 0
|
|
|
|
gzifstream::gzifstream()
|
|
|
|
: istream(&buffer), buffer()
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
1998-11-06 14:05:12 +00:00
|
|
|
clear( ios_badbit );
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzifstream::gzifstream( const char *name, ios_openmode io_mode )
|
|
|
|
: istream(&buffer), buffer()
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
1998-11-06 14:05:12 +00:00
|
|
|
this->open( name, io_mode );
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzifstream::gzifstream( int fd, ios_openmode io_mode )
|
|
|
|
: istream(&buffer), buffer()
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
1998-11-06 14:05:12 +00:00
|
|
|
buffer.attach( fd, io_mode );
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
gzifstream::~gzifstream()
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
void
|
|
|
|
gzifstream::open( const char *name, ios_openmode io_mode )
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( !buffer.open( name, io_mode ) )
|
|
|
|
clear( ios_failbit | ios_badbit );
|
|
|
|
else
|
|
|
|
clear();
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
void
|
|
|
|
gzifstream::close()
|
1998-09-01 19:06:28 +00:00
|
|
|
{
|
1998-11-06 14:05:12 +00:00
|
|
|
if ( !buffer.close() )
|
|
|
|
clear( ios_failbit | ios_badbit );
|
1998-09-01 19:06:28 +00:00
|
|
|
}
|
1998-11-06 14:05:12 +00:00
|
|
|
#endif
|
1998-09-01 19:06:28 +00:00
|
|
|
|
1998-11-06 14:05:12 +00:00
|
|
|
// $Log$
|
1999-01-19 20:41:46 +00:00
|
|
|
// Revision 1.3 1999/01/19 20:41:48 curt
|
|
|
|
// Portability updates contributed by Bernie Bright.
|
|
|
|
//
|
1998-11-06 14:05:12 +00:00
|
|
|
// Revision 1.2 1998/11/06 14:05:14 curt
|
|
|
|
// More portability improvements by Bernie Bright.
|
|
|
|
//
|