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