]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
370eda5e848a11bf6064cf105b59ff4a6ab9a234
[simgear.git] / simgear / misc / zfstream.hxx
1 //  A C++ I/O streams interface to the zlib gz* functions
2 //
3 // Written by Bernie Bright, 1998
4 // Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
5 //
6 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24 #ifndef _zfstream_hxx
25 #define _zfstream_hxx
26
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30
31 #include <simgear/compiler.h>
32
33 #ifdef HAVE_ZLIB
34 #  include <zlib.h>
35 #else
36 #  include <simgear/zlib/zlib.h>
37 #endif
38
39 #ifdef FG_HAVE_STD_INCLUDES
40
41 #  include <streambuf>
42 #  include <istream>
43
44 #  define ios_openmode ios_base::openmode
45 #  define ios_in       ios_base::in
46 #  define ios_out      ios_base::out
47 #  define ios_app      ios_base::app
48 #  define ios_binary   ios_base::binary
49
50 #  define ios_seekdir  ios_base::seekdir
51
52 #  define ios_badbit   ios_base::badbit
53 #  define ios_failbit  ios_base::failbit
54
55 FG_USING_STD(streambuf);
56 FG_USING_STD(ios_base);
57 FG_USING_STD(streampos);
58 FG_USING_STD(streamoff);
59
60 #else
61
62 #  ifdef FG_HAVE_STREAMBUF
63 #    include <streambuf.h>
64 #    include <istream.h>
65 #  else
66 #    include <iostream.h>
67 #  endif
68
69 //#  define ios_openmode ios::open_mode
70 #  define ios_openmode int
71 #  define ios_in       ios::in
72 #  define ios_out      ios::out
73 #  define ios_app      ios::app
74
75 #if defined(__GNUC__) && __GNUC_MINOR__ < 8
76 #  define ios_binary   ios::bin
77 #elif defined( FG_HAVE_NATIVE_SGI_COMPILERS )
78 #  define ios_binary   0
79 #else
80 #  define ios_binary   ios::binary
81 #endif
82
83 #  define ios_seekdir  ios::seek_dir
84
85 #  define ios_badbit   ios::badbit
86 #  define ios_failbit  ios::failbit
87
88 #  include <simgear/fg_traits.hxx>
89
90 #endif // FG_HAVE_STD_INCLUDES
91
92 //-----------------------------------------------------------------------------
93 //
94 //
95 //
96 class gzfilebuf : public streambuf
97 {
98 public:
99
100 #ifndef FG_HAVE_STD_INCLUDES
101     typedef char_traits<char>           traits_type;
102     typedef char_traits<char>::int_type int_type;
103     typedef char_traits<char>::pos_type pos_type;
104     typedef char_traits<char>::off_type off_type;
105 #endif
106
107     gzfilebuf();
108     virtual ~gzfilebuf();
109
110     gzfilebuf* open( const char* name, ios_openmode io_mode );
111     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
112     gzfilebuf* close();
113
114 //     int setcompressionlevel( int comp_level );
115 //     int setcompressionstrategy( int comp_strategy );
116     bool is_open() const { return (file != NULL); }
117     virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
118     virtual int sync();
119
120 protected:
121
122     virtual int_type underflow();
123     virtual int_type overflow( int_type c = traits_type::eof() );
124
125 private:
126
127     int_type flushbuf();
128     int fillbuf();
129
130     // Convert io_mode to "rwab" string.
131     void cvt_iomode( char* mode_str, ios_openmode io_mode );
132
133 private:
134
135     gzFile file;
136     ios_openmode mode;
137     bool own_file_descriptor;
138
139     // Get (input) buffer.
140     int ibuf_size;
141     char* ibuffer;
142
143     enum { page_size = 4096 };
144
145 private:
146     // Not defined
147     gzfilebuf( const gzfilebuf& );
148     void operator= ( const gzfilebuf& );
149 };
150
151 //-----------------------------------------------------------------------------
152 //
153 // 
154 //
155 struct gzifstream_base
156 {
157     gzifstream_base() {}
158
159     gzfilebuf gzbuf;
160 };
161
162 #endif // _zfstream_hxx
163