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