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