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