]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
Patch from Tony Peden to remove unused variable and eliminate compiler
[simgear.git] / simgear / misc / zfstream.hxx
1 /**
2  * \file zfstream.hxx
3  * A C++ I/O streams interface to the zlib gz* functions.
4  */
5
6 // Written by Bernie Bright, 1998
7 // Based on zlib/contrib/iostream/ by Kevin Ruland <kevin@rodin.wustl.edu>
8 //
9 // Copyright (C) 1998  Bernie Bright - bbright@c031.aone.net.au
10 //
11 // This library is free software; you can redistribute it and/or
12 // modify it under the terms of the GNU Library General Public
13 // License as published by the Free Software Foundation; either
14 // version 2 of the License, or (at your option) any later version.
15 //
16 // This library is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 // Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public
22 // License along with this library; if not, write to the
23 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 // Boston, MA  02111-1307, USA.
25 //
26 // $Id$
27
28 #ifndef _zfstream_hxx
29 #define _zfstream_hxx
30
31 #include <simgear/compiler.h>
32
33 #include <zlib.h>
34
35 // At least Irix needs this
36 #ifdef SG_HAVE_NATIVE_SGI_COMPILERS
37 #include <char_traits.h>
38 SG_USING_STD(char_traits);
39 #endif
40
41 #ifdef SG_HAVE_STD_INCLUDES
42
43 #  include <streambuf>
44 #  include <istream>
45
46 #  define ios_openmode ios_base::openmode
47 #  define ios_in       ios_base::in
48 #  define ios_out      ios_base::out
49 #  define ios_app      ios_base::app
50 #  define ios_binary   ios_base::binary
51
52 #  define ios_seekdir  ios_base::seekdir
53
54 #  define ios_badbit   ios_base::badbit
55 #  define ios_failbit  ios_base::failbit
56
57 SG_USING_STD(streambuf);
58 SG_USING_STD(ios_base);
59 SG_USING_STD(streampos);
60 SG_USING_STD(streamoff);
61
62 #else
63
64 #  ifdef SG_HAVE_STREAMBUF
65 #    include <streambuf.h>
66 #    include <istream.h>
67 #  else
68 #    include <iostream.h>
69 #  endif
70
71 //#  define ios_openmode ios::open_mode
72 #  define ios_openmode int
73 #  define ios_in       ios::in
74 #  define ios_out      ios::out
75 #  define ios_app      ios::app
76
77 #if defined(__GNUC__) && __GNUC_MINOR__ < 8
78 #  define ios_binary   ios::bin
79 #elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
80 #  define ios_binary   0
81 #else
82 #  define ios_binary   ios::binary
83 #endif
84
85 #  define ios_seekdir  ios::seek_dir
86
87 #  define ios_badbit   ios::badbit
88 #  define ios_failbit  ios::failbit
89
90 #  include <simgear/sg_traits.hxx>
91
92 #endif // SG_HAVE_STD_INCLUDES
93
94 /**
95  * A C++ I/O streams interface to the zlib gz* functions.
96  */
97 class gzfilebuf : public streambuf
98 {
99 public:
100
101 #ifndef SG_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     /** Constructor */
109     gzfilebuf();
110
111     /** Destructor */
112     virtual ~gzfilebuf();
113
114     /**
115      * Open a stream
116      * @param name file name
117      * @param io_mode mdoe flags
118      * @return file stream
119      */
120     gzfilebuf* open( const char* name, ios_openmode io_mode );
121
122     /** 
123      * Attach to an existing file descriptor
124      * @param file_descriptor file descriptor
125      * @param io_mode mode flags
126      * @return file stream
127      */
128     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
129
130     /** Close stream */
131     gzfilebuf* close();
132
133     // int setcompressionlevel( int comp_level );
134     // int setcompressionstrategy( int comp_strategy );
135
136     /** @return true if open, false otherwise */
137     bool is_open() const { return (file != NULL); }
138
139     /** @return stream position */
140     virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
141
142     /** sync the stream */
143     virtual int sync();
144
145 protected:
146
147     virtual int_type underflow();
148     virtual int_type overflow( int_type c = traits_type::eof() );
149
150 private:
151
152     int_type flushbuf();
153     int fillbuf();
154
155     // Convert io_mode to "rwab" string.
156     void cvt_iomode( char* mode_str, ios_openmode io_mode );
157
158 private:
159
160     gzFile file;
161     ios_openmode mode;
162     bool own_file_descriptor;
163
164     // Get (input) buffer.
165     int ibuf_size;
166     char* ibuffer;
167
168     enum { page_size = 4096 };
169
170 private:
171     // Not defined
172     gzfilebuf( const gzfilebuf& );
173     void operator= ( const gzfilebuf& );
174 };
175
176 /**
177  * document me
178  */
179 struct gzifstream_base
180 {
181     gzifstream_base() {}
182
183     gzfilebuf gzbuf;
184 };
185
186 #endif // _zfstream_hxx
187