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