]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
Remove "glut" dependency.
[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 #ifdef SG_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 SG_USING_STD(streambuf);
52 SG_USING_STD(ios_base);
53 SG_USING_STD(streampos);
54 SG_USING_STD(streamoff);
55
56 #else
57
58 #  ifdef SG_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 #else
74 #  define ios_binary   ios::binary
75 #endif
76
77 #  define ios_seekdir  ios::seek_dir
78
79 #  define ios_badbit   ios::badbit
80 #  define ios_failbit  ios::failbit
81
82 #  include <simgear/sg_traits.hxx>
83
84 #endif // SG_HAVE_STD_INCLUDES
85
86 /**
87  * A C++ I/O streams interface to the zlib gz* functions.
88  */
89 #ifdef SG_NEED_STREAMBUF_HACK
90 class gzfilebuf : public __streambuf
91 #else
92 class gzfilebuf : public streambuf
93 #endif
94 {
95 public:
96
97 #ifndef SG_HAVE_STD_INCLUDES
98     typedef char_traits<char>           traits_type;
99     typedef char_traits<char>::int_type int_type;
100     // typedef char_traits<char>::pos_type pos_type;
101     // typedef char_traits<char>::off_type off_type;
102 #endif
103
104     /** Constructor */
105     gzfilebuf();
106
107     /** Destructor */
108     virtual ~gzfilebuf();
109
110     /**
111      * Open a stream
112      * @param name file name
113      * @param io_mode mdoe flags
114      * @return file stream
115      */
116     gzfilebuf* open( const char* name, ios_openmode io_mode );
117
118     /** 
119      * Attach to an existing file descriptor
120      * @param file_descriptor file descriptor
121      * @param io_mode mode flags
122      * @return file stream
123      */
124     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
125
126     /** Close stream */
127     gzfilebuf* close();
128
129     // int setcompressionlevel( int comp_level );
130     // int setcompressionstrategy( int comp_strategy );
131
132     /** @return true if open, false otherwise */
133     bool is_open() const { return (file != NULL); }
134
135     /** @return stream position */
136     virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
137
138     /** sync the stream */
139     virtual int sync();
140
141 protected:
142
143     virtual int_type underflow();
144     virtual int_type overflow( int_type c = traits_type::eof() );
145
146 private:
147
148     int_type flushbuf();
149     int fillbuf();
150
151     // Convert io_mode to "rwab" string.
152     void cvt_iomode( char* mode_str, ios_openmode io_mode );
153
154 private:
155
156     gzFile file;
157     ios_openmode mode;
158     bool own_file_descriptor;
159
160     // Get (input) buffer.
161     int ibuf_size;
162     char* ibuffer;
163
164     enum { page_size = 4096 };
165
166 private:
167     // Not defined
168     gzfilebuf( const gzfilebuf& );
169     void operator= ( const gzfilebuf& );
170 };
171
172 /**
173  * document me
174  */
175 struct gzifstream_base
176 {
177     gzifstream_base() {}
178
179     gzfilebuf gzbuf;
180 };
181
182 #endif // _zfstream_hxx
183