]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
0b281456549d294b7e7aa152258f31c3985c6636
[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 #ifdef HAVE_CONFIG_H
32 #  include <config.h>
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #ifdef HAVE_ZLIB
38 #  include <zlib.h>
39 #else
40 #  include <simgear/zlib/zlib.h>
41 #endif
42
43 // At least Irix needs this
44 #ifdef SG_HAVE_NATIVE_SGI_COMPILERS
45 #include <char_traits.h>
46 SG_USING_STD(char_traits);
47 #endif
48
49 #ifdef SG_HAVE_STD_INCLUDES
50
51 #  include <streambuf>
52 #  include <istream>
53
54 #  define ios_openmode ios_base::openmode
55 #  define ios_in       ios_base::in
56 #  define ios_out      ios_base::out
57 #  define ios_app      ios_base::app
58 #  define ios_binary   ios_base::binary
59
60 #  define ios_seekdir  ios_base::seekdir
61
62 #  define ios_badbit   ios_base::badbit
63 #  define ios_failbit  ios_base::failbit
64
65 SG_USING_STD(streambuf);
66 SG_USING_STD(ios_base);
67 SG_USING_STD(streampos);
68 SG_USING_STD(streamoff);
69
70 #else
71
72 #  ifdef SG_HAVE_STREAMBUF
73 #    include <streambuf.h>
74 #    include <istream.h>
75 #  else
76 #    include <iostream.h>
77 #  endif
78
79 //#  define ios_openmode ios::open_mode
80 #  define ios_openmode int
81 #  define ios_in       ios::in
82 #  define ios_out      ios::out
83 #  define ios_app      ios::app
84
85 #if defined(__GNUC__) && __GNUC_MINOR__ < 8
86 #  define ios_binary   ios::bin
87 #elif defined( SG_HAVE_NATIVE_SGI_COMPILERS )
88 #  define ios_binary   0
89 #else
90 #  define ios_binary   ios::binary
91 #endif
92
93 #  define ios_seekdir  ios::seek_dir
94
95 #  define ios_badbit   ios::badbit
96 #  define ios_failbit  ios::failbit
97
98 #  include <simgear/sg_traits.hxx>
99
100 #endif // SG_HAVE_STD_INCLUDES
101
102 /**
103  * A C++ I/O streams interface to the zlib gz* functions.
104  */
105 class gzfilebuf : public streambuf
106 {
107 public:
108
109 #ifndef SG_HAVE_STD_INCLUDES
110     typedef char_traits<char>           traits_type;
111     typedef char_traits<char>::int_type int_type;
112     // typedef char_traits<char>::pos_type pos_type;
113     // typedef char_traits<char>::off_type off_type;
114 #endif
115
116     /** Constructor */
117     gzfilebuf();
118
119     /** Destructor */
120     virtual ~gzfilebuf();
121
122     /**
123      * Open a stream
124      * @param name file name
125      * @param io_mode mdoe flags
126      * @return file stream
127      */
128     gzfilebuf* open( const char* name, ios_openmode io_mode );
129
130     /** 
131      * Attach to an existing file descriptor
132      * @param file_descriptor file descriptor
133      * @param io_mode mode flags
134      * @return file stream
135      */
136     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
137
138     /** Close stream */
139     gzfilebuf* close();
140
141     // int setcompressionlevel( int comp_level );
142     // int setcompressionstrategy( int comp_strategy );
143
144     /** @return true if open, false otherwise */
145     bool is_open() const { return (file != NULL); }
146
147     /** @return stream position */
148     virtual streampos seekoff( streamoff off, ios_seekdir way, int which );
149
150     /** sync the stream */
151     virtual int sync();
152
153 protected:
154
155     virtual int_type underflow();
156     virtual int_type overflow( int_type c = traits_type::eof() );
157
158 private:
159
160     int_type flushbuf();
161     int fillbuf();
162
163     // Convert io_mode to "rwab" string.
164     void cvt_iomode( char* mode_str, ios_openmode io_mode );
165
166 private:
167
168     gzFile file;
169     ios_openmode mode;
170     bool own_file_descriptor;
171
172     // Get (input) buffer.
173     int ibuf_size;
174     char* ibuffer;
175
176     enum { page_size = 4096 };
177
178 private:
179     // Not defined
180     gzfilebuf( const gzfilebuf& );
181     void operator= ( const gzfilebuf& );
182 };
183
184 /**
185  * document me
186  */
187 struct gzifstream_base
188 {
189     gzifstream_base() {}
190
191     Gzfilebuf gzbuf;
192 };
193
194 #endif // _zfstream_hxx
195