Home | History | Annotate | Line # | Download | only in iostream
      1  1.1  christos 
      2  1.1  christos #ifndef zfstream_h
      3  1.1  christos #define zfstream_h
      4  1.1  christos 
      5  1.1  christos #include <fstream.h>
      6  1.1  christos #include "zlib.h"
      7  1.1  christos 
      8  1.1  christos class gzfilebuf : public streambuf {
      9  1.1  christos 
     10  1.1  christos public:
     11  1.1  christos 
     12  1.1  christos   gzfilebuf( );
     13  1.1  christos   virtual ~gzfilebuf();
     14  1.1  christos 
     15  1.1  christos   gzfilebuf *open( const char *name, int io_mode );
     16  1.1  christos   gzfilebuf *attach( int file_descriptor, int io_mode );
     17  1.1  christos   gzfilebuf *close();
     18  1.1  christos 
     19  1.1  christos   int setcompressionlevel( int comp_level );
     20  1.1  christos   int setcompressionstrategy( int comp_strategy );
     21  1.1  christos 
     22  1.1  christos   inline int is_open() const { return (file !=NULL); }
     23  1.1  christos 
     24  1.1  christos   virtual streampos seekoff( streamoff, ios::seek_dir, int );
     25  1.1  christos 
     26  1.1  christos   virtual int sync();
     27  1.1  christos 
     28  1.1  christos protected:
     29  1.1  christos 
     30  1.1  christos   virtual int underflow();
     31  1.1  christos   virtual int overflow( int = EOF );
     32  1.1  christos 
     33  1.1  christos private:
     34  1.1  christos 
     35  1.1  christos   gzFile file;
     36  1.1  christos   short mode;
     37  1.1  christos   short own_file_descriptor;
     38  1.1  christos 
     39  1.1  christos   int flushbuf();
     40  1.1  christos   int fillbuf();
     41  1.1  christos 
     42  1.1  christos };
     43  1.1  christos 
     44  1.1  christos class gzfilestream_common : virtual public ios {
     45  1.1  christos 
     46  1.1  christos   friend class gzifstream;
     47  1.1  christos   friend class gzofstream;
     48  1.1  christos   friend gzofstream &setcompressionlevel( gzofstream &, int );
     49  1.1  christos   friend gzofstream &setcompressionstrategy( gzofstream &, int );
     50  1.1  christos 
     51  1.1  christos public:
     52  1.1  christos   virtual ~gzfilestream_common();
     53  1.1  christos 
     54  1.1  christos   void attach( int fd, int io_mode );
     55  1.1  christos   void open( const char *name, int io_mode );
     56  1.1  christos   void close();
     57  1.1  christos 
     58  1.1  christos protected:
     59  1.1  christos   gzfilestream_common();
     60  1.1  christos 
     61  1.1  christos private:
     62  1.1  christos   gzfilebuf *rdbuf();
     63  1.1  christos 
     64  1.1  christos   gzfilebuf buffer;
     65  1.1  christos 
     66  1.1  christos };
     67  1.1  christos 
     68  1.1  christos class gzifstream : public gzfilestream_common, public istream {
     69  1.1  christos 
     70  1.1  christos public:
     71  1.1  christos 
     72  1.1  christos   gzifstream();
     73  1.1  christos   gzifstream( const char *name, int io_mode = ios::in );
     74  1.1  christos   gzifstream( int fd, int io_mode = ios::in );
     75  1.1  christos 
     76  1.1  christos   virtual ~gzifstream();
     77  1.1  christos 
     78  1.1  christos };
     79  1.1  christos 
     80  1.1  christos class gzofstream : public gzfilestream_common, public ostream {
     81  1.1  christos 
     82  1.1  christos public:
     83  1.1  christos 
     84  1.1  christos   gzofstream();
     85  1.1  christos   gzofstream( const char *name, int io_mode = ios::out );
     86  1.1  christos   gzofstream( int fd, int io_mode = ios::out );
     87  1.1  christos 
     88  1.1  christos   virtual ~gzofstream();
     89  1.1  christos 
     90  1.1  christos };
     91  1.1  christos 
     92  1.1  christos template<class T> class gzomanip {
     93  1.1  christos   friend gzofstream &operator<<(gzofstream &, const gzomanip<T> &);
     94  1.1  christos public:
     95  1.1  christos   gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { }
     96  1.1  christos private:
     97  1.1  christos   gzofstream &(*func)(gzofstream &, T);
     98  1.1  christos   T val;
     99  1.1  christos };
    100  1.1  christos 
    101  1.1  christos template<class T> gzofstream &operator<<(gzofstream &s, const gzomanip<T> &m)
    102  1.1  christos {
    103  1.1  christos   return (*m.func)(s, m.val);
    104  1.1  christos }
    105  1.1  christos 
    106  1.1  christos inline gzofstream &setcompressionlevel( gzofstream &s, int l )
    107  1.1  christos {
    108  1.1  christos   (s.rdbuf())->setcompressionlevel(l);
    109  1.1  christos   return s;
    110  1.1  christos }
    111  1.1  christos 
    112  1.1  christos inline gzofstream &setcompressionstrategy( gzofstream &s, int l )
    113  1.1  christos {
    114  1.1  christos   (s.rdbuf())->setcompressionstrategy(l);
    115  1.1  christos   return s;
    116  1.1  christos }
    117  1.1  christos 
    118  1.1  christos inline gzomanip<int> setcompressionlevel(int l)
    119  1.1  christos {
    120  1.1  christos   return gzomanip<int>(&setcompressionlevel,l);
    121  1.1  christos }
    122  1.1  christos 
    123  1.1  christos inline gzomanip<int> setcompressionstrategy(int l)
    124  1.1  christos {
    125  1.1  christos   return gzomanip<int>(&setcompressionstrategy,l);
    126  1.1  christos }
    127  1.1  christos 
    128  1.1  christos #endif
    129