]> git.mxchange.org Git - simgear.git/blob - Misc/zfstream.hxx
Fixes by Charlie Hotchkiss.
[simgear.git] / Misc / zfstream.hxx
1
2 #ifndef _zfstream_hxx
3 #define _zfstream_hxx
4
5 #include <fstream.h>
6
7 #include "zlib/zlib.h"
8 #include "Include/fg_stl_config.h"
9
10 class gzfilebuf : public streambuf {
11
12 public:
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( short comp_level );
22   int setcompressionstrategy( short 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
30 protected:
31
32   virtual int underflow();
33   virtual int overflow( int = EOF );
34
35 private:
36
37   gzFile file;
38   short mode;
39   short own_file_descriptor;
40
41   int flushbuf();
42   int fillbuf();
43
44 };
45
46 class 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
53 public:
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
60 protected:
61   gzfilestream_common();
62
63   gzfilebuf *rdbuf();
64
65 private:
66
67   gzfilebuf buffer;
68
69 };
70
71 class gzifstream : public gzfilestream_common, public istream {
72
73 public:
74
75   gzifstream();
76   gzifstream( const char *name, int io_mode = ios::in );
77   gzifstream( int fd, int io_mode = ios::in );
78
79   virtual ~gzifstream();
80
81 };
82
83 class gzofstream : public gzfilestream_common, public ostream {
84
85 public:
86
87   gzofstream();
88   gzofstream( const char *name, int io_mode = ios::out );
89   gzofstream( int fd, int io_mode = ios::out );
90
91   virtual ~gzofstream();
92
93 };
94
95 template<class T> class gzomanip {
96     friend gzofstream &operator << _FG_NULL_TMPL_ARGS (gzofstream &, const gzomanip<T> &);
97 public:
98   gzomanip(gzofstream &(*f)(gzofstream &, T), T v) : func(f), val(v) { }
99 private:
100   gzofstream &(*func)(gzofstream &, T);
101   T val;
102 };
103
104 template<class T> gzofstream &operator<<(gzofstream &s,
105                                          const gzomanip<T> &m) {
106   return (*m.func)(s, m.val);
107   
108 }
109
110 inline gzofstream &setcompressionlevel( gzofstream &s, int l ) {
111   (s.rdbuf())->setcompressionlevel(l);
112   return s;
113 }
114
115 inline gzofstream &setcompressionstrategy( gzofstream &s, int l ) {
116   (s.rdbuf())->setcompressionstrategy(l);
117   return s;
118 }
119
120 inline gzomanip<int> setcompressionlevel(int l)
121 {
122   return gzomanip<int>( /* & */ setcompressionlevel,l);     // & superfluous
123 }
124
125 inline gzomanip<int> setcompressionstrategy(int l)
126 {
127   return gzomanip<int>( /* & */ setcompressionstrategy,l);  // & superfluous
128 }
129
130 #endif // _zfstream_hxx