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