| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 | 
							
- #include "zfstream.h"
 
- gzfilebuf::gzfilebuf() :
 
-   file(NULL),
 
-   mode(0),
 
-   own_file_descriptor(0)
 
- { }
 
- gzfilebuf::~gzfilebuf() {
 
-   sync();
 
-   if ( own_file_descriptor )
 
-     close();
 
- }
 
- gzfilebuf *gzfilebuf::open( const char *name,
 
-                             int io_mode ) {
 
-   if ( is_open() )
 
-     return NULL;
 
-   char char_mode[10];
 
-   char *p = char_mode;
 
-   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';
 
-   }
 
-   if ( io_mode & ios::binary ) {
 
-     mode |= ios::binary;
 
-     *p++ = 'b';
 
-   }
 
-   // Hard code the compression level
 
-   if ( io_mode & (ios::out|ios::app )) {
 
-     *p++ = '9';
 
-   }
 
-   // Put the end-of-string indicator
 
-   *p = '\0';
 
-   if ( (file = gzopen(name, char_mode)) == NULL )
 
-     return NULL;
 
-   own_file_descriptor = 1;
 
-   return this;
 
- }
 
- gzfilebuf *gzfilebuf::attach( int file_descriptor,
 
-                               int io_mode ) {
 
-   if ( is_open() )
 
-     return NULL;
 
-   char char_mode[10];
 
-   char *p = char_mode;
 
-   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';
 
-   }
 
-   if ( io_mode & ios::binary ) {
 
-     mode |= ios::binary;
 
-     *p++ = 'b';
 
-   }
 
-   // Hard code the compression level
 
-   if ( io_mode & (ios::out|ios::app )) {
 
-     *p++ = '9';
 
-   }
 
-   // Put the end-of-string indicator
 
-   *p = '\0';
 
-   if ( (file = gzdopen(file_descriptor, char_mode)) == NULL )
 
-     return NULL;
 
-   own_file_descriptor = 0;
 
-   return this;
 
- }
 
- gzfilebuf *gzfilebuf::close() {
 
-   if ( is_open() ) {
 
-     sync();
 
-     gzclose( file );
 
-     file = NULL;
 
-   }
 
-   return this;
 
- }
 
- int gzfilebuf::setcompressionlevel( int comp_level ) {
 
-   return gzsetparams(file, comp_level, -2);
 
- }
 
- int gzfilebuf::setcompressionstrategy( int comp_strategy ) {
 
-   return gzsetparams(file, -2, comp_strategy);
 
- }
 
- streampos gzfilebuf::seekoff( streamoff off, ios::seek_dir dir, int which ) {
 
-   return streampos(EOF);
 
- }
 
- int gzfilebuf::underflow() {
 
-   // If the file hasn't been opened for reading, error.
 
-   if ( !is_open() || !(mode & ios::in) )
 
-     return EOF;
 
-   // if a buffer doesn't exists, allocate one.
 
-   if ( !base() ) {
 
-     if ( (allocate()) == EOF )
 
-       return EOF;
 
-     setp(0,0);
 
-   } else {
 
-     if ( in_avail() )
 
-       return (unsigned char) *gptr();
 
-     if ( out_waiting() ) {
 
-       if ( flushbuf() == EOF )
 
-         return EOF;
 
-     }
 
-   }
 
-   // Attempt to fill the buffer.
 
-   int result = fillbuf();
 
-   if ( result == EOF ) {
 
-     // disable get area
 
-     setg(0,0,0);
 
-     return EOF;
 
-   }
 
-   return (unsigned char) *gptr();
 
- }
 
- int gzfilebuf::overflow( int c ) {
 
-   if ( !is_open() || !(mode & ios::out) )
 
-     return EOF;
 
-   if ( !base() ) {
 
-     if ( allocate() == EOF )
 
-       return EOF;
 
-     setg(0,0,0);
 
-   } else {
 
-     if (in_avail()) {
 
-         return EOF;
 
-     }
 
-     if (out_waiting()) {
 
-       if (flushbuf() == EOF)
 
-         return EOF;
 
-     }
 
-   }
 
-   int bl = blen();
 
-   setp( base(), base() + bl);
 
-   if ( c != EOF ) {
 
-     *pptr() = c;
 
-     pbump(1);
 
-   }
 
-   return 0;
 
- }
 
- int gzfilebuf::sync() {
 
-   if ( !is_open() )
 
-     return EOF;
 
-   if ( out_waiting() )
 
-     return flushbuf();
 
-   return 0;
 
- }
 
- int gzfilebuf::flushbuf() {
 
-   int n;
 
-   char *q;
 
-   q = pbase();
 
-   n = pptr() - q;
 
-   if ( gzwrite( file, q, n) < n )
 
-     return EOF;
 
-   setp(0,0);
 
-   return 0;
 
- }
 
- int gzfilebuf::fillbuf() {
 
-   int required;
 
-   char *p;
 
-   p = base();
 
-   required = blen();
 
-   int t = gzread( file, p, required );
 
-   if ( t <= 0) return EOF;
 
-   setg( base(), base(), base()+t);
 
-   return t;
 
- }
 
- gzfilestream_common::gzfilestream_common() :
 
-   ios( gzfilestream_common::rdbuf() )
 
- { }
 
- gzfilestream_common::~gzfilestream_common()
 
- { }
 
- void gzfilestream_common::attach( int fd, int io_mode ) {
 
-   if ( !buffer.attach( fd, io_mode) )
 
-     clear( ios::failbit | ios::badbit );
 
-   else
 
-     clear();
 
- }
 
- void gzfilestream_common::open( const char *name, int io_mode ) {
 
-   if ( !buffer.open( name, io_mode ) )
 
-     clear( ios::failbit | ios::badbit );
 
-   else
 
-     clear();
 
- }
 
- void gzfilestream_common::close() {
 
-   if ( !buffer.close() )
 
-     clear( ios::failbit | ios::badbit );
 
- }
 
- gzfilebuf *gzfilestream_common::rdbuf()
 
- {
 
-   return &buffer;
 
- }
 
- gzifstream::gzifstream() :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   clear( ios::badbit );
 
- }
 
- gzifstream::gzifstream( const char *name, int io_mode ) :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   gzfilestream_common::open( name, io_mode );
 
- }
 
- gzifstream::gzifstream( int fd, int io_mode ) :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   gzfilestream_common::attach( fd, io_mode );
 
- }
 
- gzifstream::~gzifstream() { }
 
- gzofstream::gzofstream() :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   clear( ios::badbit );
 
- }
 
- gzofstream::gzofstream( const char *name, int io_mode ) :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   gzfilestream_common::open( name, io_mode );
 
- }
 
- gzofstream::gzofstream( int fd, int io_mode ) :
 
-   ios( gzfilestream_common::rdbuf() )
 
- {
 
-   gzfilestream_common::attach( fd, io_mode );
 
- }
 
- gzofstream::~gzofstream() { }
 
 
  |