00001 00002 #ifndef zfstream_h 00003 #define zfstream_h 00004 00005 #include <fstream.h> 00006 #include "zlib.h" 00007 00008 class gzfilebuf : public streambuf { 00009 00010 public: 00011 00012 gzfilebuf( ); 00013 virtual ~gzfilebuf(); 00014 00015 gzfilebuf *open( const char *name, int io_mode ); 00016 gzfilebuf *attach( int file_descriptor, int io_mode ); 00017 gzfilebuf *close(); 00018 00019 int setcompressionlevel( int comp_level ); 00020 int setcompressionstrategy( int comp_strategy ); 00021 00022 inline int is_open() const { return (file !=NULL); } 00023 00024 virtual streampos seekoff( streamoff, ios::seek_dir, int ); 00025 00026 virtual int sync(); 00027 00028 protected: 00029 00030 virtual int underflow(); 00031 virtual int overflow( int = EOF ); 00032 00033 private: 00034 00035 gzFile file; 00036 short mode; 00037 short own_file_descriptor; 00038 00039 int flushbuf(); 00040 int fillbuf(); 00041 00042 }; 00043 00044 class gzfilestream_common : virtual public ios { 00045 00046 friend class gzifstream; 00047 friend class gzofstream; 00048 friend gzofstream &setcompressionlevel( gzofstream &, int ); 00049 friend gzofstream &setcompressionstrategy( gzofstream &, int ); 00050 00051 public: 00052 virtual ~gzfilestream_common(); 00053 00054 void attach( int fd, int io_mode ); 00055 void open( const char *name, int io_mode ); 00056 void close(); 00057 00058 protected: 00059 gzfilestream_common(); 00060 00061 private: 00062 gzfilebuf *rdbuf(); 00063 00064 gzfilebuf buffer; 00065 00066 }; 00067 00068 class gzifstream : public gzfilestream_common, public istream { 00069 00070 public: 00071 00072 gzifstream(); 00073 gzifstream( const char *name, int io_mode = ios::in ); 00074 gzifstream( int fd, int io_mode = ios::in ); 00075 00076 virtual ~gzifstream(); 00077 00078 }; 00079 00080 class gzofstream : public gzfilestream_common, public ostream { 00081 00082 public: 00083 00084 gzofstream(); 00085 gzofstream( const char *name, int io_mode = ios::out ); 00086 gzofstream( int fd, int io_mode = ios::out ); 00087 00088 virtual ~gzofstream(); 00089 00090 }; 00091 00092 template<class T> class gzomanip { 00093 friend gzofstream &operator<<(gzofstream &, const gzomanip<T> &); 00094 public: 00095 gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { } 00096 private: 00097 gzofstream &(*func)(gzofstream &, T); 00098 T val; 00099 }; 00100 00101 template<class T> gzofstream &operator<<(gzofstream &s, const gzomanip<T> &m) 00102 { 00103 return (*m.func)(s, m.val); 00104 } 00105 00106 inline gzofstream &setcompressionlevel( gzofstream &s, int l ) 00107 { 00108 (s.rdbuf())->setcompressionlevel(l); 00109 return s; 00110 } 00111 00112 inline gzofstream &setcompressionstrategy( gzofstream &s, int l ) 00113 { 00114 (s.rdbuf())->setcompressionstrategy(l); 00115 return s; 00116 } 00117 00118 inline gzomanip<int> setcompressionlevel(int l) 00119 { 00120 return gzomanip<int>(&setcompressionlevel,l); 00121 } 00122 00123 inline gzomanip<int> setcompressionstrategy(int l) 00124 { 00125 return gzomanip<int>(&setcompressionstrategy,l); 00126 } 00127 00128 #endif