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