]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
b093499f1597f186cca70c153be248a98758736c
[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 #include <simgear/compiler.h>
28
29 #ifdef HAVE_ZLIB
30 #  include <zlib.h>
31 #else
32 #  include <simgear/zlib/zlib.h>
33 #endif
34
35 #ifdef FG_HAVE_STD_INCLUDES
36
37 #  include <streambuf>
38 #  include <istream>
39
40 #  define ios_openmode ios_base::openmode
41 #  define ios_in       ios_base::in
42 #  define ios_out      ios_base::out
43 #  define ios_app      ios_base::app
44 #  define ios_binary   ios_base::binary
45
46 #  define ios_seekdir  ios_base::seekdir
47
48 #  define ios_badbit   ios_base::badbit
49 #  define ios_failbit  ios_base::failbit
50
51 FG_USING_STD(streambuf);
52 FG_USING_STD(ios_base);
53 FG_USING_STD(streampos);
54 FG_USING_STD(streamoff);
55
56 #else
57
58 #  ifdef FG_HAVE_STREAMBUF
59 #    include <streambuf.h>
60 #    include <istream.h>
61 #  else
62 #    include <iostream.h>
63 #  endif
64
65 //#  define ios_openmode ios::open_mode
66 #  define ios_openmode int
67 #  define ios_in       ios::in
68 #  define ios_out      ios::out
69 #  define ios_app      ios::app
70
71 #if defined(__GNUC__) && __GNUC_MINOR__ < 8
72 #  define ios_binary   ios::bin
73 #elif defined( FG_HAVE_NATIVE_SGI_COMPILERS )
74 #  define ios_binary   0
75 #else
76 #  define ios_binary   ios::binary
77 #endif
78
79 #  define ios_seekdir  ios::seek_dir
80
81 #  define ios_badbit   ios::badbit
82 #  define ios_failbit  ios::failbit
83
84 #  include <simgear/fg_traits.hxx>
85
86 #endif // FG_HAVE_STD_INCLUDES
87
88 //-----------------------------------------------------------------------------
89 //
90 //
91 //
92 class gzfilebuf : public streambuf
93 {
94 public:
95
96 #ifndef FG_HAVE_STD_INCLUDES
97     typedef char_traits<char>           traits_type;
98     typedef char_traits<char>::int_type int_type;
99     typedef char_traits<char>::pos_type pos_type;
100     typedef char_traits<char>::off_type off_type;
101 #endif
102
103     gzfilebuf();
104     virtual ~gzfilebuf();
105
106     gzfilebuf* open( const char* name, ios_openmode io_mode );
107     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
108     gzfilebuf* close();
109
110 //     int setcompressionlevel( int comp_level );
111 //     int setcompressionstrategy( int comp_strategy );
112     bool is_open() const { return (file != NULL); }
113     virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
114     virtual int sync();
115
116 protected:
117
118     virtual int_type underflow();
119     virtual int_type overflow( int_type c = traits_type::eof() );
120
121 private:
122
123     int_type flushbuf();
124     int fillbuf();
125
126     // Convert io_mode to "rwab" string.
127     void cvt_iomode( char* mode_str, ios_openmode io_mode );
128
129 private:
130
131     gzFile file;
132     ios_openmode mode;
133     bool own_file_descriptor;
134
135     // Get (input) buffer.
136     int ibuf_size;
137     char* ibuffer;
138
139     enum { page_size = 4096 };
140
141 private:
142     // Not defined
143     gzfilebuf( const gzfilebuf& );
144     void operator= ( const gzfilebuf& );
145 };
146
147 //-----------------------------------------------------------------------------
148 //
149 // 
150 //
151 struct gzifstream_base
152 {
153     gzifstream_base() {}
154
155     gzfilebuf gzbuf;
156 };
157
158 #endif // _zfstream_hxx
159