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