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
#ifndef ZFSTREAM_H
kusano fc6ab3
#define ZFSTREAM_H
kusano fc6ab3
kusano fc6ab3
#include <istream>  // not iostream, since we don't need cin/cout</istream>
kusano fc6ab3
#include <ostream></ostream>
kusano fc6ab3
#include "zlib.h"
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
/**
kusano fc6ab3
 *  @brief  Gzipped file stream buffer class.
kusano fc6ab3
 *
kusano fc6ab3
 *  This class implements basic_filebuf for gzipped files. It doesn't yet support
kusano fc6ab3
 *  seeking (allowed by zlib but slow/limited), putback and read/write access
kusano fc6ab3
 *  (tricky). Otherwise, it attempts to be a drop-in replacement for the standard
kusano fc6ab3
 *  file streambuf.
kusano fc6ab3
*/
kusano fc6ab3
class gzfilebuf : public std::streambuf
kusano fc6ab3
{
kusano fc6ab3
public:
kusano fc6ab3
  //  Default constructor.
kusano fc6ab3
  gzfilebuf();
kusano fc6ab3
kusano fc6ab3
  //  Destructor.
kusano fc6ab3
  virtual
kusano fc6ab3
  ~gzfilebuf();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Set compression level and strategy on the fly.
kusano fc6ab3
   *  @param  comp_level  Compression level (see zlib.h for allowed values)
kusano fc6ab3
   *  @param  comp_strategy  Compression strategy (see zlib.h for allowed values)
kusano fc6ab3
   *  @return  Z_OK on success, Z_STREAM_ERROR otherwise.
kusano fc6ab3
   *
kusano fc6ab3
   *  Unfortunately, these parameters cannot be modified separately, as the
kusano fc6ab3
   *  previous zfstream version assumed. Since the strategy is seldom changed,
kusano fc6ab3
   *  it can default and setcompression(level) then becomes like the old
kusano fc6ab3
   *  setcompressionlevel(level).
kusano fc6ab3
  */
kusano fc6ab3
  int
kusano fc6ab3
  setcompression(int comp_level,
kusano fc6ab3
                 int comp_strategy = Z_DEFAULT_STRATEGY);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Check if file is open.
kusano fc6ab3
   *  @return  True if file is open.
kusano fc6ab3
  */
kusano fc6ab3
  bool
kusano fc6ab3
  is_open() const { return (file != NULL); }
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Open gzipped file.
kusano fc6ab3
   *  @param  name  File name.
kusano fc6ab3
   *  @param  mode  Open mode flags.
kusano fc6ab3
   *  @return  @c this on success, NULL on failure.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf*
kusano fc6ab3
  open(const char* name,
kusano fc6ab3
       std::ios_base::openmode mode);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Attach to already open gzipped file.
kusano fc6ab3
   *  @param  fd  File descriptor.
kusano fc6ab3
   *  @param  mode  Open mode flags.
kusano fc6ab3
   *  @return  @c this on success, NULL on failure.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf*
kusano fc6ab3
  attach(int fd,
kusano fc6ab3
         std::ios_base::openmode mode);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Close gzipped file.
kusano fc6ab3
   *  @return  @c this on success, NULL on failure.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf*
kusano fc6ab3
  close();
kusano fc6ab3
kusano fc6ab3
protected:
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Convert ios open mode int to mode string used by zlib.
kusano fc6ab3
   *  @return  True if valid mode flag combination.
kusano fc6ab3
  */
kusano fc6ab3
  bool
kusano fc6ab3
  open_mode(std::ios_base::openmode mode,
kusano fc6ab3
            char* c_mode) const;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Number of characters available in stream buffer.
kusano fc6ab3
   *  @return  Number of characters.
kusano fc6ab3
   *
kusano fc6ab3
   *  This indicates number of characters in get area of stream buffer.
kusano fc6ab3
   *  These characters can be read without accessing the gzipped file.
kusano fc6ab3
  */
kusano fc6ab3
  virtual std::streamsize
kusano fc6ab3
  showmanyc();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Fill get area from gzipped file.
kusano fc6ab3
   *  @return  First character in get area on success, EOF on error.
kusano fc6ab3
   *
kusano fc6ab3
   *  This actually reads characters from gzipped file to stream
kusano fc6ab3
   *  buffer. Always buffered.
kusano fc6ab3
  */
kusano fc6ab3
  virtual int_type
kusano fc6ab3
  underflow();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Write put area to gzipped file.
kusano fc6ab3
   *  @param  c  Extra character to add to buffer contents.
kusano fc6ab3
   *  @return  Non-EOF on success, EOF on error.
kusano fc6ab3
   *
kusano fc6ab3
   *  This actually writes characters in stream buffer to
kusano fc6ab3
   *  gzipped file. With unbuffered output this is done one
kusano fc6ab3
   *  character at a time.
kusano fc6ab3
  */
kusano fc6ab3
  virtual int_type
kusano fc6ab3
  overflow(int_type c = traits_type::eof());
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Installs external stream buffer.
kusano fc6ab3
   *  @param  p  Pointer to char buffer.
kusano fc6ab3
   *  @param  n  Size of external buffer.
kusano fc6ab3
   *  @return  @c this on success, NULL on failure.
kusano fc6ab3
   *
kusano fc6ab3
   *  Call setbuf(0,0) to enable unbuffered output.
kusano fc6ab3
  */
kusano fc6ab3
  virtual std::streambuf*
kusano fc6ab3
  setbuf(char_type* p,
kusano fc6ab3
         std::streamsize n);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Flush stream buffer to file.
kusano fc6ab3
   *  @return  0 on success, -1 on error.
kusano fc6ab3
   *
kusano fc6ab3
   *  This calls underflow(EOF) to do the job.
kusano fc6ab3
  */
kusano fc6ab3
  virtual int
kusano fc6ab3
  sync();
kusano fc6ab3
kusano fc6ab3
//
kusano fc6ab3
// Some future enhancements
kusano fc6ab3
//
kusano fc6ab3
//  virtual int_type uflow();
kusano fc6ab3
//  virtual int_type pbackfail(int_type c = traits_type::eof());
kusano fc6ab3
//  virtual pos_type
kusano fc6ab3
//  seekoff(off_type off,
kusano fc6ab3
//          std::ios_base::seekdir way,
kusano fc6ab3
//          std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
kusano fc6ab3
//  virtual pos_type
kusano fc6ab3
//  seekpos(pos_type sp,
kusano fc6ab3
//          std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
kusano fc6ab3
kusano fc6ab3
private:
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Allocate internal buffer.
kusano fc6ab3
   *
kusano fc6ab3
   *  This function is safe to call multiple times. It will ensure
kusano fc6ab3
   *  that a proper internal buffer exists if it is required. If the
kusano fc6ab3
   *  buffer already exists or is external, the buffer pointers will be
kusano fc6ab3
   *  reset to their original state.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  enable_buffer();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Destroy internal buffer.
kusano fc6ab3
   *
kusano fc6ab3
   *  This function is safe to call multiple times. It will ensure
kusano fc6ab3
   *  that the internal buffer is deallocated if it exists. In any
kusano fc6ab3
   *  case, it will also reset the buffer pointers.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  disable_buffer();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  Underlying file pointer.
kusano fc6ab3
  */
kusano fc6ab3
  gzFile file;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  Mode in which file was opened.
kusano fc6ab3
  */
kusano fc6ab3
  std::ios_base::openmode io_mode;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  True if this object owns file descriptor.
kusano fc6ab3
   *
kusano fc6ab3
   *  This makes the class responsible for closing the file
kusano fc6ab3
   *  upon destruction.
kusano fc6ab3
  */
kusano fc6ab3
  bool own_fd;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Stream buffer.
kusano fc6ab3
   *
kusano fc6ab3
   *  For simplicity this remains allocated on the free store for the
kusano fc6ab3
   *  entire life span of the gzfilebuf object, unless replaced by setbuf.
kusano fc6ab3
  */
kusano fc6ab3
  char_type* buffer;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Stream buffer size.
kusano fc6ab3
   *
kusano fc6ab3
   *  Defaults to system default buffer size (typically 8192 bytes).
kusano fc6ab3
   *  Modified by setbuf.
kusano fc6ab3
  */
kusano fc6ab3
  std::streamsize buffer_size;
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  True if this object owns stream buffer.
kusano fc6ab3
   *
kusano fc6ab3
   *  This makes the class responsible for deleting the buffer
kusano fc6ab3
   *  upon destruction.
kusano fc6ab3
  */
kusano fc6ab3
  bool own_buffer;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
/**
kusano fc6ab3
 *  @brief  Gzipped file input stream class.
kusano fc6ab3
 *
kusano fc6ab3
 *  This class implements ifstream for gzipped files. Seeking and putback
kusano fc6ab3
 *  is not supported yet.
kusano fc6ab3
*/
kusano fc6ab3
class gzifstream : public std::istream
kusano fc6ab3
{
kusano fc6ab3
public:
kusano fc6ab3
  //  Default constructor
kusano fc6ab3
  gzifstream();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Construct stream on gzipped file to be opened.
kusano fc6ab3
   *  @param  name  File name.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::in).
kusano fc6ab3
  */
kusano fc6ab3
  explicit
kusano fc6ab3
  gzifstream(const char* name,
kusano fc6ab3
             std::ios_base::openmode mode = std::ios_base::in);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Construct stream on already open gzipped file.
kusano fc6ab3
   *  @param  fd    File descriptor.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::in).
kusano fc6ab3
  */
kusano fc6ab3
  explicit
kusano fc6ab3
  gzifstream(int fd,
kusano fc6ab3
             std::ios_base::openmode mode = std::ios_base::in);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  Obtain underlying stream buffer.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf*
kusano fc6ab3
  rdbuf() const
kusano fc6ab3
  { return const_cast<gzfilebuf*>(&sb); }</gzfilebuf*>
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Check if file is open.
kusano fc6ab3
   *  @return  True if file is open.
kusano fc6ab3
  */
kusano fc6ab3
  bool
kusano fc6ab3
  is_open() { return sb.is_open(); }
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Open gzipped file.
kusano fc6ab3
   *  @param  name  File name.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::in).
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state good() if file opens successfully;
kusano fc6ab3
   *  otherwise in state fail(). This differs from the behavior of
kusano fc6ab3
   *  ifstream, which never sets the state to good() and therefore
kusano fc6ab3
   *  won't allow you to reuse the stream for a second file unless
kusano fc6ab3
   *  you manually clear() the state. The choice is a matter of
kusano fc6ab3
   *  convenience.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  open(const char* name,
kusano fc6ab3
       std::ios_base::openmode mode = std::ios_base::in);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Attach to already open gzipped file.
kusano fc6ab3
   *  @param  fd  File descriptor.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::in).
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state good() if attach succeeded; otherwise
kusano fc6ab3
   *  in state fail().
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  attach(int fd,
kusano fc6ab3
         std::ios_base::openmode mode = std::ios_base::in);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Close gzipped file.
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state fail() if close failed.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  close();
kusano fc6ab3
kusano fc6ab3
private:
kusano fc6ab3
  /**
kusano fc6ab3
   *  Underlying stream buffer.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf sb;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
/**
kusano fc6ab3
 *  @brief  Gzipped file output stream class.
kusano fc6ab3
 *
kusano fc6ab3
 *  This class implements ofstream for gzipped files. Seeking and putback
kusano fc6ab3
 *  is not supported yet.
kusano fc6ab3
*/
kusano fc6ab3
class gzofstream : public std::ostream
kusano fc6ab3
{
kusano fc6ab3
public:
kusano fc6ab3
  //  Default constructor
kusano fc6ab3
  gzofstream();
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Construct stream on gzipped file to be opened.
kusano fc6ab3
   *  @param  name  File name.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::out).
kusano fc6ab3
  */
kusano fc6ab3
  explicit
kusano fc6ab3
  gzofstream(const char* name,
kusano fc6ab3
             std::ios_base::openmode mode = std::ios_base::out);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Construct stream on already open gzipped file.
kusano fc6ab3
   *  @param  fd    File descriptor.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::out).
kusano fc6ab3
  */
kusano fc6ab3
  explicit
kusano fc6ab3
  gzofstream(int fd,
kusano fc6ab3
             std::ios_base::openmode mode = std::ios_base::out);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  Obtain underlying stream buffer.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf*
kusano fc6ab3
  rdbuf() const
kusano fc6ab3
  { return const_cast<gzfilebuf*>(&sb); }</gzfilebuf*>
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Check if file is open.
kusano fc6ab3
   *  @return  True if file is open.
kusano fc6ab3
  */
kusano fc6ab3
  bool
kusano fc6ab3
  is_open() { return sb.is_open(); }
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Open gzipped file.
kusano fc6ab3
   *  @param  name  File name.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::out).
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state good() if file opens successfully;
kusano fc6ab3
   *  otherwise in state fail(). This differs from the behavior of
kusano fc6ab3
   *  ofstream, which never sets the state to good() and therefore
kusano fc6ab3
   *  won't allow you to reuse the stream for a second file unless
kusano fc6ab3
   *  you manually clear() the state. The choice is a matter of
kusano fc6ab3
   *  convenience.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  open(const char* name,
kusano fc6ab3
       std::ios_base::openmode mode = std::ios_base::out);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Attach to already open gzipped file.
kusano fc6ab3
   *  @param  fd  File descriptor.
kusano fc6ab3
   *  @param  mode  Open mode flags (forced to contain ios::out).
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state good() if attach succeeded; otherwise
kusano fc6ab3
   *  in state fail().
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  attach(int fd,
kusano fc6ab3
         std::ios_base::openmode mode = std::ios_base::out);
kusano fc6ab3
kusano fc6ab3
  /**
kusano fc6ab3
   *  @brief  Close gzipped file.
kusano fc6ab3
   *
kusano fc6ab3
   *  Stream will be in state fail() if close failed.
kusano fc6ab3
  */
kusano fc6ab3
  void
kusano fc6ab3
  close();
kusano fc6ab3
kusano fc6ab3
private:
kusano fc6ab3
  /**
kusano fc6ab3
   *  Underlying stream buffer.
kusano fc6ab3
  */
kusano fc6ab3
  gzfilebuf sb;
kusano fc6ab3
};
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
/**
kusano fc6ab3
 *  @brief  Gzipped file output stream manipulator class.
kusano fc6ab3
 *
kusano fc6ab3
 *  This class defines a two-argument manipulator for gzofstream. It is used
kusano fc6ab3
 *  as base for the setcompression(int,int) manipulator.
kusano fc6ab3
*/
kusano fc6ab3
template<typename t1,="" t2="" typename=""></typename>
kusano fc6ab3
  class gzomanip2
kusano fc6ab3
  {
kusano fc6ab3
  public:
kusano fc6ab3
    // Allows insertor to peek at internals
kusano fc6ab3
    template <typename ta,="" tb="" typename=""></typename>
kusano fc6ab3
      friend gzofstream&
kusano fc6ab3
      operator<<(gzofstream&,
kusano fc6ab3
                 const gzomanip2<ta,tb>&);</ta,tb>
kusano fc6ab3
kusano fc6ab3
    // Constructor
kusano fc6ab3
    gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
kusano fc6ab3
              T1 v1,
kusano fc6ab3
              T2 v2);
kusano fc6ab3
  private:
kusano fc6ab3
    // Underlying manipulator function
kusano fc6ab3
    gzofstream&
kusano fc6ab3
    (*func)(gzofstream&, T1, T2);
kusano fc6ab3
kusano fc6ab3
    // Arguments for manipulator function
kusano fc6ab3
    T1 val1;
kusano fc6ab3
    T2 val2;
kusano fc6ab3
  };
kusano fc6ab3
kusano fc6ab3
/*****************************************************************************/
kusano fc6ab3
kusano fc6ab3
// Manipulator function thunks through to stream buffer
kusano fc6ab3
inline gzofstream&
kusano fc6ab3
setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
kusano fc6ab3
{
kusano fc6ab3
  (gzs.rdbuf())->setcompression(l, s);
kusano fc6ab3
  return gzs;
kusano fc6ab3
}
kusano fc6ab3
kusano fc6ab3
// Manipulator constructor stores arguments
kusano fc6ab3
template<typename t1,="" t2="" typename=""></typename>
kusano fc6ab3
  inline
kusano fc6ab3
  gzomanip2<t1,t2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),</t1,t2>
kusano fc6ab3
                              T1 v1,
kusano fc6ab3
                              T2 v2)
kusano fc6ab3
  : func(f), val1(v1), val2(v2)
kusano fc6ab3
  { }
kusano fc6ab3
kusano fc6ab3
// Insertor applies underlying manipulator function to stream
kusano fc6ab3
template<typename t1,="" t2="" typename=""></typename>
kusano fc6ab3
  inline gzofstream&
kusano fc6ab3
  operator<<(gzofstream& s, const gzomanip2<t1,t2>& m)</t1,t2>
kusano fc6ab3
  { return (*m.func)(s, m.val1, m.val2); }
kusano fc6ab3
kusano fc6ab3
// Insert this onto stream to simplify setting of compression level
kusano fc6ab3
inline gzomanip2<int,int></int,int>
kusano fc6ab3
setcompression(int l, int s = Z_DEFAULT_STRATEGY)
kusano fc6ab3
{ return gzomanip2<int,int>(&setcompression, l, s); }</int,int>
kusano fc6ab3
kusano fc6ab3
#endif // ZFSTREAM_H