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