kusano fc6ab3
/*
kusano fc6ab3
 * A C++ I/O streams interface to the zlib gz* functions
kusano fc6ab3
 *
kusano fc6ab3
 * by Ludwig Schwardt <schwardt@sun.ac.za></schwardt@sun.ac.za>
kusano fc6ab3
 * original version by Kevin Ruland <kevin@rodin.wustl.edu></kevin@rodin.wustl.edu>
kusano fc6ab3
 *
kusano fc6ab3
 * This version is standard-compliant and compatible with gcc 3.x.
kusano fc6ab3
 */
kusano fc6ab3
kusano fc6ab3
#include "zfstream.h"
kusano fc6ab3
#include <cstring>          // for strcpy, strcat, strlen (mode strings)</cstring>
kusano fc6ab3
#include <cstdio>           // for BUFSIZ</cstdio>
kusano fc6ab3
kusano fc6ab3
// Internal buffer sizes (default and "unbuffered" versions)
kusano fc6ab3
#define BIGBUFSIZE BUFSIZ
kusano fc6ab3
#define SMALLBUFSIZE 1
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
// Default constructor
kusano fc6ab3
gzfilebuf::gzfilebuf()
kusano fc6ab3
: file(NULL), io_mode(std::ios_base::openmode(0)), own_fd(false),
kusano fc6ab3
  buffer(NULL), buffer_size(BIGBUFSIZE), own_buffer(true)
kusano fc6ab3
{
kusano fc6ab3
  // No buffers to start with
kusano fc6ab3
  this->disable_buffer();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Destructor
kusano fc6ab3
gzfilebuf::~gzfilebuf()
kusano fc6ab3
{
kusano fc6ab3
  // Sync output buffer and close only if responsible for file
kusano fc6ab3
  // (i.e. attached streams should be left open at this stage)
kusano fc6ab3
  this->sync();
kusano fc6ab3
  if (own_fd)
kusano fc6ab3
    this->close();
kusano fc6ab3
  // Make sure internal buffer is deallocated
kusano fc6ab3
  this->disable_buffer();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Set compression level and strategy
kusano fc6ab3
int
kusano fc6ab3
gzfilebuf::setcompression(int comp_level,
kusano fc6ab3
                          int comp_strategy)
kusano fc6ab3
{
kusano fc6ab3
  return gzsetparams(file, comp_level, comp_strategy);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Open gzipped file
kusano fc6ab3
gzfilebuf*
kusano fc6ab3
gzfilebuf::open(const char *name,
kusano fc6ab3
                std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  // Fail if file already open
kusano fc6ab3
  if (this->is_open())
kusano fc6ab3
    return NULL;
kusano fc6ab3
  // Don't support simultaneous read/write access (yet)
kusano fc6ab3
  if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // Build mode string for gzopen and check it [27.8.1.3.2]
kusano fc6ab3
  char char_mode[6] = "\0\0\0\0\0";
kusano fc6ab3
  if (!this->open_mode(mode, char_mode))
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // Attempt to open file
kusano fc6ab3
  if ((file = gzopen(name, char_mode)) == NULL)
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // On success, allocate internal buffer and set flags
kusano fc6ab3
  this->enable_buffer();
kusano fc6ab3
  io_mode = mode;
kusano fc6ab3
  own_fd = true;
kusano fc6ab3
  return this;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Attach to gzipped file
kusano fc6ab3
gzfilebuf*
kusano fc6ab3
gzfilebuf::attach(int fd,
kusano fc6ab3
                  std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  // Fail if file already open
kusano fc6ab3
  if (this->is_open())
kusano fc6ab3
    return NULL;
kusano fc6ab3
  // Don't support simultaneous read/write access (yet)
kusano fc6ab3
  if ((mode & std::ios_base::in) && (mode & std::ios_base::out))
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // Build mode string for gzdopen and check it [27.8.1.3.2]
kusano fc6ab3
  char char_mode[6] = "\0\0\0\0\0";
kusano fc6ab3
  if (!this->open_mode(mode, char_mode))
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // Attempt to attach to file
kusano fc6ab3
  if ((file = gzdopen(fd, char_mode)) == NULL)
kusano fc6ab3
    return NULL;
kusano fc6ab3
kusano fc6ab3
  // On success, allocate internal buffer and set flags
kusano fc6ab3
  this->enable_buffer();
kusano fc6ab3
  io_mode = mode;
kusano fc6ab3
  own_fd = false;
kusano fc6ab3
  return this;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Close gzipped file
kusano fc6ab3
gzfilebuf*
kusano fc6ab3
gzfilebuf::close()
kusano fc6ab3
{
kusano fc6ab3
  // Fail immediately if no file is open
kusano fc6ab3
  if (!this->is_open())
kusano fc6ab3
    return NULL;
kusano fc6ab3
  // Assume success
kusano fc6ab3
  gzfilebuf* retval = this;
kusano fc6ab3
  // Attempt to sync and close gzipped file
kusano fc6ab3
  if (this->sync() == -1)
kusano fc6ab3
    retval = NULL;
kusano fc6ab3
  if (gzclose(file) < 0)
kusano fc6ab3
    retval = NULL;
kusano fc6ab3
  // File is now gone anyway (postcondition [27.8.1.3.8])
kusano fc6ab3
  file = NULL;
kusano fc6ab3
  own_fd = false;
kusano fc6ab3
  // Destroy internal buffer if it exists
kusano fc6ab3
  this->disable_buffer();
kusano fc6ab3
  return retval;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
kusano fc6ab3
kusano fc6ab3
// Convert int open mode to mode string
kusano fc6ab3
bool
kusano fc6ab3
gzfilebuf::open_mode(std::ios_base::openmode mode,
kusano fc6ab3
                     char* c_mode) const
kusano fc6ab3
{
kusano fc6ab3
  bool testb = mode & std::ios_base::binary;
kusano fc6ab3
  bool testi = mode & std::ios_base::in;
kusano fc6ab3
  bool testo = mode & std::ios_base::out;
kusano fc6ab3
  bool testt = mode & std::ios_base::trunc;
kusano fc6ab3
  bool testa = mode & std::ios_base::app;
kusano fc6ab3
kusano fc6ab3
  // Check for valid flag combinations - see [27.8.1.3.2] (Table 92)
kusano fc6ab3
  // Original zfstream hardcoded the compression level to maximum here...
kusano fc6ab3
  // Double the time for less than 1% size improvement seems
kusano fc6ab3
  // excessive though - keeping it at the default level
kusano fc6ab3
  // To change back, just append "9" to the next three mode strings
kusano fc6ab3
  if (!testi && testo && !testt && !testa)
kusano fc6ab3
    strcpy(c_mode, "w");
kusano fc6ab3
  if (!testi && testo && !testt && testa)
kusano fc6ab3
    strcpy(c_mode, "a");
kusano fc6ab3
  if (!testi && testo && testt && !testa)
kusano fc6ab3
    strcpy(c_mode, "w");
kusano fc6ab3
  if (testi && !testo && !testt && !testa)
kusano fc6ab3
    strcpy(c_mode, "r");
kusano fc6ab3
  // No read/write mode yet
kusano fc6ab3
//  if (testi && testo && !testt && !testa)
kusano fc6ab3
//    strcpy(c_mode, "r+");
kusano fc6ab3
//  if (testi && testo && testt && !testa)
kusano fc6ab3
//    strcpy(c_mode, "w+");
kusano fc6ab3
kusano fc6ab3
  // Mode string should be empty for invalid combination of flags
kusano fc6ab3
  if (strlen(c_mode) == 0)
kusano fc6ab3
    return false;
kusano fc6ab3
  if (testb)
kusano fc6ab3
    strcat(c_mode, "b");
kusano fc6ab3
  return true;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Determine number of characters in internal get buffer
kusano fc6ab3
std::streamsize
kusano fc6ab3
gzfilebuf::showmanyc()
kusano fc6ab3
{
kusano fc6ab3
  // Calls to underflow will fail if file not opened for reading
kusano fc6ab3
  if (!this->is_open() || !(io_mode & std::ios_base::in))
kusano fc6ab3
    return -1;
kusano fc6ab3
  // Make sure get area is in use
kusano fc6ab3
  if (this->gptr() && (this->gptr() < this->egptr()))
kusano fc6ab3
    return std::streamsize(this->egptr() - this->gptr());
kusano fc6ab3
  else
kusano fc6ab3
    return 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Fill get area from gzipped file
kusano fc6ab3
gzfilebuf::int_type
kusano fc6ab3
gzfilebuf::underflow()
kusano fc6ab3
{
kusano fc6ab3
  // If something is left in the get area by chance, return it
kusano fc6ab3
  // (this shouldn't normally happen, as underflow is only supposed
kusano fc6ab3
  // to be called when gptr >= egptr, but it serves as error check)
kusano fc6ab3
  if (this->gptr() && (this->gptr() < this->egptr()))
kusano fc6ab3
    return traits_type::to_int_type(*(this->gptr()));
kusano fc6ab3
kusano fc6ab3
  // If the file hasn't been opened for reading, produce error
kusano fc6ab3
  if (!this->is_open() || !(io_mode & std::ios_base::in))
kusano fc6ab3
    return traits_type::eof();
kusano fc6ab3
kusano fc6ab3
  // Attempt to fill internal buffer from gzipped file
kusano fc6ab3
  // (buffer must be guaranteed to exist...)
kusano fc6ab3
  int bytes_read = gzread(file, buffer, buffer_size);
kusano fc6ab3
  // Indicates error or EOF
kusano fc6ab3
  if (bytes_read <= 0)
kusano fc6ab3
  {
kusano fc6ab3
    // Reset get area
kusano fc6ab3
    this->setg(buffer, buffer, buffer);
kusano fc6ab3
    return traits_type::eof();
kusano fc6ab3
  }
kusano fc6ab3
  // Make all bytes read from file available as get area
kusano fc6ab3
  this->setg(buffer, buffer, buffer + bytes_read);
kusano fc6ab3
kusano fc6ab3
  // Return next character in get area
kusano fc6ab3
  return traits_type::to_int_type(*(this->gptr()));
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Write put area to gzipped file
kusano fc6ab3
gzfilebuf::int_type
kusano fc6ab3
gzfilebuf::overflow(int_type c)
kusano fc6ab3
{
kusano fc6ab3
  // Determine whether put area is in use
kusano fc6ab3
  if (this->pbase())
kusano fc6ab3
  {
kusano fc6ab3
    // Double-check pointer range
kusano fc6ab3
    if (this->pptr() > this->epptr() || this->pptr() < this->pbase())
kusano fc6ab3
      return traits_type::eof();
kusano fc6ab3
    // Add extra character to buffer if not EOF
kusano fc6ab3
    if (!traits_type::eq_int_type(c, traits_type::eof()))
kusano fc6ab3
    {
kusano fc6ab3
      *(this->pptr()) = traits_type::to_char_type(c);
kusano fc6ab3
      this->pbump(1);
kusano fc6ab3
    }
kusano fc6ab3
    // Number of characters to write to file
kusano fc6ab3
    int bytes_to_write = this->pptr() - this->pbase();
kusano fc6ab3
    // Overflow doesn't fail if nothing is to be written
kusano fc6ab3
    if (bytes_to_write > 0)
kusano fc6ab3
    {
kusano fc6ab3
      // If the file hasn't been opened for writing, produce error
kusano fc6ab3
      if (!this->is_open() || !(io_mode & std::ios_base::out))
kusano fc6ab3
        return traits_type::eof();
kusano fc6ab3
      // If gzipped file won't accept all bytes written to it, fail
kusano fc6ab3
      if (gzwrite(file, this->pbase(), bytes_to_write) != bytes_to_write)
kusano fc6ab3
        return traits_type::eof();
kusano fc6ab3
      // Reset next pointer to point to pbase on success
kusano fc6ab3
      this->pbump(-bytes_to_write);
kusano fc6ab3
    }
kusano fc6ab3
  }
kusano fc6ab3
  // Write extra character to file if not EOF
kusano fc6ab3
  else if (!traits_type::eq_int_type(c, traits_type::eof()))
kusano fc6ab3
  {
kusano fc6ab3
    // If the file hasn't been opened for writing, produce error
kusano fc6ab3
    if (!this->is_open() || !(io_mode & std::ios_base::out))
kusano fc6ab3
      return traits_type::eof();
kusano fc6ab3
    // Impromptu char buffer (allows "unbuffered" output)
kusano fc6ab3
    char_type last_char = traits_type::to_char_type(c);
kusano fc6ab3
    // If gzipped file won't accept this character, fail
kusano fc6ab3
    if (gzwrite(file, &last_char, 1) != 1)
kusano fc6ab3
      return traits_type::eof();
kusano fc6ab3
  }
kusano fc6ab3
kusano fc6ab3
  // If you got here, you have succeeded (even if c was EOF)
kusano fc6ab3
  // The return value should therefore be non-EOF
kusano fc6ab3
  if (traits_type::eq_int_type(c, traits_type::eof()))
kusano fc6ab3
    return traits_type::not_eof(c);
kusano fc6ab3
  else
kusano fc6ab3
    return c;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Assign new buffer
kusano fc6ab3
std::streambuf*
kusano fc6ab3
gzfilebuf::setbuf(char_type* p,
kusano fc6ab3
                  std::streamsize n)
kusano fc6ab3
{
kusano fc6ab3
  // First make sure stuff is sync'ed, for safety
kusano fc6ab3
  if (this->sync() == -1)
kusano fc6ab3
    return NULL;
kusano fc6ab3
  // If buffering is turned off on purpose via setbuf(0,0), still allocate one...
kusano fc6ab3
  // "Unbuffered" only really refers to put [27.8.1.4.10], while get needs at
kusano fc6ab3
  // least a buffer of size 1 (very inefficient though, therefore make it bigger?)
kusano fc6ab3
  // This follows from [27.5.2.4.3]/12 (gptr needs to point at something, it seems)
kusano fc6ab3
  if (!p || !n)
kusano fc6ab3
  {
kusano fc6ab3
    // Replace existing buffer (if any) with small internal buffer
kusano fc6ab3
    this->disable_buffer();
kusano fc6ab3
    buffer = NULL;
kusano fc6ab3
    buffer_size = 0;
kusano fc6ab3
    own_buffer = true;
kusano fc6ab3
    this->enable_buffer();
kusano fc6ab3
  }
kusano fc6ab3
  else
kusano fc6ab3
  {
kusano fc6ab3
    // Replace existing buffer (if any) with external buffer
kusano fc6ab3
    this->disable_buffer();
kusano fc6ab3
    buffer = p;
kusano fc6ab3
    buffer_size = n;
kusano fc6ab3
    own_buffer = false;
kusano fc6ab3
    this->enable_buffer();
kusano fc6ab3
  }
kusano fc6ab3
  return this;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Write put area to gzipped file (i.e. ensures that put area is empty)
kusano fc6ab3
int
kusano fc6ab3
gzfilebuf::sync()
kusano fc6ab3
{
kusano fc6ab3
  return traits_type::eq_int_type(this->overflow(), traits_type::eof()) ? -1 : 0;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
kusano fc6ab3
kusano fc6ab3
// Allocate internal buffer
kusano fc6ab3
void
kusano fc6ab3
gzfilebuf::enable_buffer()
kusano fc6ab3
{
kusano fc6ab3
  // If internal buffer required, allocate one
kusano fc6ab3
  if (own_buffer && !buffer)
kusano fc6ab3
  {
kusano fc6ab3
    // Check for buffered vs. "unbuffered"
kusano fc6ab3
    if (buffer_size > 0)
kusano fc6ab3
    {
kusano fc6ab3
      // Allocate internal buffer
kusano fc6ab3
      buffer = new char_type[buffer_size];
kusano fc6ab3
      // Get area starts empty and will be expanded by underflow as need arises
kusano fc6ab3
      this->setg(buffer, buffer, buffer);
kusano fc6ab3
      // Setup entire internal buffer as put area.
kusano fc6ab3
      // The one-past-end pointer actually points to the last element of the buffer,
kusano fc6ab3
      // so that overflow(c) can safely add the extra character c to the sequence.
kusano fc6ab3
      // These pointers remain in place for the duration of the buffer
kusano fc6ab3
      this->setp(buffer, buffer + buffer_size - 1);
kusano fc6ab3
    }
kusano fc6ab3
    else
kusano fc6ab3
    {
kusano fc6ab3
      // Even in "unbuffered" case, (small?) get buffer is still required
kusano fc6ab3
      buffer_size = SMALLBUFSIZE;
kusano fc6ab3
      buffer = new char_type[buffer_size];
kusano fc6ab3
      this->setg(buffer, buffer, buffer);
kusano fc6ab3
      // "Unbuffered" means no put buffer
kusano fc6ab3
      this->setp(0, 0);
kusano fc6ab3
    }
kusano fc6ab3
  }
kusano fc6ab3
  else
kusano fc6ab3
  {
kusano fc6ab3
    // If buffer already allocated, reset buffer pointers just to make sure no
kusano fc6ab3
    // stale chars are lying around
kusano fc6ab3
    this->setg(buffer, buffer, buffer);
kusano fc6ab3
    this->setp(buffer, buffer + buffer_size - 1);
kusano fc6ab3
  }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Destroy internal buffer
kusano fc6ab3
void
kusano fc6ab3
gzfilebuf::disable_buffer()
kusano fc6ab3
{
kusano fc6ab3
  // If internal buffer exists, deallocate it
kusano fc6ab3
  if (own_buffer && buffer)
kusano fc6ab3
  {
kusano fc6ab3
    // Preserve unbuffered status by zeroing size
kusano fc6ab3
    if (!this->pbase())
kusano fc6ab3
      buffer_size = 0;
kusano fc6ab3
    delete[] buffer;
kusano fc6ab3
    buffer = NULL;
kusano fc6ab3
    this->setg(0, 0, 0);
kusano fc6ab3
    this->setp(0, 0);
kusano fc6ab3
  }
kusano fc6ab3
  else
kusano fc6ab3
  {
kusano fc6ab3
    // Reset buffer pointers to initial state if external buffer exists
kusano fc6ab3
    this->setg(buffer, buffer, buffer);
kusano fc6ab3
    if (buffer)
kusano fc6ab3
      this->setp(buffer, buffer + buffer_size - 1);
kusano fc6ab3
    else
kusano fc6ab3
      this->setp(0, 0);
kusano fc6ab3
  }
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
// Default constructor initializes stream buffer
kusano fc6ab3
gzifstream::gzifstream()
kusano fc6ab3
: std::istream(NULL), sb()
kusano fc6ab3
{ this->init(&sb); }
kusano fc6ab3
kusano fc6ab3
// Initialize stream buffer and open file
kusano fc6ab3
gzifstream::gzifstream(const char* name,
kusano fc6ab3
                       std::ios_base::openmode mode)
kusano fc6ab3
: std::istream(NULL), sb()
kusano fc6ab3
{
kusano fc6ab3
  this->init(&sb);
kusano fc6ab3
  this->open(name, mode);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Initialize stream buffer and attach to file
kusano fc6ab3
gzifstream::gzifstream(int fd,
kusano fc6ab3
                       std::ios_base::openmode mode)
kusano fc6ab3
: std::istream(NULL), sb()
kusano fc6ab3
{
kusano fc6ab3
  this->init(&sb);
kusano fc6ab3
  this->attach(fd, mode);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Open file and go into fail() state if unsuccessful
kusano fc6ab3
void
kusano fc6ab3
gzifstream::open(const char* name,
kusano fc6ab3
                 std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.open(name, mode | std::ios_base::in))
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
  else
kusano fc6ab3
    this->clear();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Attach to file and go into fail() state if unsuccessful
kusano fc6ab3
void
kusano fc6ab3
gzifstream::attach(int fd,
kusano fc6ab3
                   std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.attach(fd, mode | std::ios_base::in))
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
  else
kusano fc6ab3
    this->clear();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Close file
kusano fc6ab3
void
kusano fc6ab3
gzifstream::close()
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.close())
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
// Default constructor initializes stream buffer
kusano fc6ab3
gzofstream::gzofstream()
kusano fc6ab3
: std::ostream(NULL), sb()
kusano fc6ab3
{ this->init(&sb); }
kusano fc6ab3
kusano fc6ab3
// Initialize stream buffer and open file
kusano fc6ab3
gzofstream::gzofstream(const char* name,
kusano fc6ab3
                       std::ios_base::openmode mode)
kusano fc6ab3
: std::ostream(NULL), sb()
kusano fc6ab3
{
kusano fc6ab3
  this->init(&sb);
kusano fc6ab3
  this->open(name, mode);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Initialize stream buffer and attach to file
kusano fc6ab3
gzofstream::gzofstream(int fd,
kusano fc6ab3
                       std::ios_base::openmode mode)
kusano fc6ab3
: std::ostream(NULL), sb()
kusano fc6ab3
{
kusano fc6ab3
  this->init(&sb);
kusano fc6ab3
  this->attach(fd, mode);
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Open file and go into fail() state if unsuccessful
kusano fc6ab3
void
kusano fc6ab3
gzofstream::open(const char* name,
kusano fc6ab3
                 std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.open(name, mode | std::ios_base::out))
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
  else
kusano fc6ab3
    this->clear();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Attach to file and go into fail() state if unsuccessful
kusano fc6ab3
void
kusano fc6ab3
gzofstream::attach(int fd,
kusano fc6ab3
                   std::ios_base::openmode mode)
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.attach(fd, mode | std::ios_base::out))
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
  else
kusano fc6ab3
    this->clear();
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Close file
kusano fc6ab3
void
kusano fc6ab3
gzofstream::close()
kusano fc6ab3
{
kusano fc6ab3
  if (!sb.close())
kusano fc6ab3
    this->setstate(std::ios_base::failbit);
kusano fc6ab3
}