2011-09-10 02:17:33 -04:00
|
|
|
|
2011-09-10 02:22:37 -04:00
|
|
|
#ifndef zfstream_h
|
|
|
|
#define zfstream_h
|
2011-09-10 02:17:33 -04:00
|
|
|
|
|
|
|
#include <fstream.h>
|
|
|
|
#include "zlib.h"
|
|
|
|
|
|
|
|
class gzfilebuf : public streambuf {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
gzfilebuf( );
|
|
|
|
virtual ~gzfilebuf();
|
|
|
|
|
|
|
|
gzfilebuf *open( const char *name, int io_mode );
|
|
|
|
gzfilebuf *attach( int file_descriptor, int io_mode );
|
|
|
|
gzfilebuf *close();
|
|
|
|
|
2011-09-10 02:22:37 -04:00
|
|
|
int setcompressionlevel( int comp_level );
|
|
|
|
int setcompressionstrategy( int comp_strategy );
|
2011-09-10 02:17:33 -04:00
|
|
|
|
|
|
|
inline int is_open() const { return (file !=NULL); }
|
|
|
|
|
|
|
|
virtual streampos seekoff( streamoff, ios::seek_dir, int );
|
|
|
|
|
|
|
|
virtual int sync();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual int underflow();
|
|
|
|
virtual int overflow( int = EOF );
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
gzFile file;
|
|
|
|
short mode;
|
|
|
|
short own_file_descriptor;
|
|
|
|
|
|
|
|
int flushbuf();
|
|
|
|
int fillbuf();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class gzfilestream_common : virtual public ios {
|
|
|
|
|
|
|
|
friend class gzifstream;
|
|
|
|
friend class gzofstream;
|
|
|
|
friend gzofstream &setcompressionlevel( gzofstream &, int );
|
|
|
|
friend gzofstream &setcompressionstrategy( gzofstream &, int );
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~gzfilestream_common();
|
|
|
|
|
|
|
|
void attach( int fd, int io_mode );
|
|
|
|
void open( const char *name, int io_mode );
|
|
|
|
void close();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
gzfilestream_common();
|
|
|
|
|
|
|
|
private:
|
|
|
|
gzfilebuf *rdbuf();
|
|
|
|
|
|
|
|
gzfilebuf buffer;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class gzifstream : public gzfilestream_common, public istream {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
gzifstream();
|
|
|
|
gzifstream( const char *name, int io_mode = ios::in );
|
|
|
|
gzifstream( int fd, int io_mode = ios::in );
|
|
|
|
|
|
|
|
virtual ~gzifstream();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class gzofstream : public gzfilestream_common, public ostream {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
gzofstream();
|
|
|
|
gzofstream( const char *name, int io_mode = ios::out );
|
|
|
|
gzofstream( int fd, int io_mode = ios::out );
|
|
|
|
|
|
|
|
virtual ~gzofstream();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T> class gzomanip {
|
|
|
|
friend gzofstream &operator<<(gzofstream &, const gzomanip<T> &);
|
|
|
|
public:
|
|
|
|
gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { }
|
|
|
|
private:
|
|
|
|
gzofstream &(*func)(gzofstream &, T);
|
|
|
|
T val;
|
|
|
|
};
|
|
|
|
|
2011-09-10 02:22:37 -04:00
|
|
|
template<class T> gzofstream &operator<<(gzofstream &s, const gzomanip<T> &m)
|
|
|
|
{
|
2011-09-10 02:17:33 -04:00
|
|
|
return (*m.func)(s, m.val);
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:22:37 -04:00
|
|
|
inline gzofstream &setcompressionlevel( gzofstream &s, int l )
|
|
|
|
{
|
2011-09-10 02:17:33 -04:00
|
|
|
(s.rdbuf())->setcompressionlevel(l);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-09-10 02:22:37 -04:00
|
|
|
inline gzofstream &setcompressionstrategy( gzofstream &s, int l )
|
|
|
|
{
|
2011-09-10 02:17:33 -04:00
|
|
|
(s.rdbuf())->setcompressionstrategy(l);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline gzomanip<int> setcompressionlevel(int l)
|
|
|
|
{
|
|
|
|
return gzomanip<int>(&setcompressionlevel,l);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline gzomanip<int> setcompressionstrategy(int l)
|
|
|
|
{
|
|
|
|
return gzomanip<int>(&setcompressionstrategy,l);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|