]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
Update doxgen config and some comments.
[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
35 #include <streambuf>
36 #include <istream>
37
38 #define ios_openmode std::ios_base::openmode
39 #define ios_in       std::ios_base::in
40 #define ios_out      std::ios_base::out
41 #define ios_app      std::ios_base::app
42 #define ios_binary   std::ios_base::binary
43
44 #define ios_seekdir  std::ios_base::seekdir
45
46 #define ios_badbit   std::ios_base::badbit
47 #define ios_failbit  std::ios_base::failbit
48
49 /**
50  * A C++ I/O streams interface to the zlib gz* functions.
51  */
52 #ifdef SG_NEED_STREAMBUF_HACK
53 class gzfilebuf : public __streambuf {
54     typedef __streambuf parent;
55 #else
56 class gzfilebuf : public std::streambuf {
57     typedef std::streambuf parent;
58 #endif
59
60 public:
61     /** Constructor */
62     gzfilebuf();
63
64     /** Destructor */
65     virtual ~gzfilebuf();
66
67     /**
68      * Open a stream
69      * @param name file name
70      * @param io_mode mode flags
71      * @return file stream
72      */
73     gzfilebuf* open( const char* name, ios_openmode io_mode );
74
75     /** 
76      * Attach to an existing file descriptor
77      * @param file_descriptor file descriptor
78      * @param io_mode mode flags
79      * @return file stream
80      */
81     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
82
83     /** Close stream */
84     gzfilebuf* close();
85
86     int setcompressionlevel( int comp_level );
87     int setcompressionstrategy( int comp_strategy );
88
89     /** @return true if open, false otherwise */
90     bool is_open() const { return (file != NULL); }
91
92     /** @return stream position */
93     virtual std::streampos seekoff( std::streamoff off, ios_seekdir way, ios_openmode which );
94
95     /** sync the stream */
96     virtual int sync();
97
98 protected:
99
100     virtual int_type underflow();
101
102     virtual int_type overflow( int_type c = parent::traits_type::eof() );
103     bool    out_waiting();
104     char*   base() {return obuffer;}
105     int     blen() {return obuf_size;}
106     char    allocate();
107
108 private:
109
110     int_type flushbuf();
111     int fillbuf();
112
113     // Convert io_mode to "rwab" string.
114     void cvt_iomode( char* mode_str, ios_openmode io_mode );
115
116 private:
117
118     gzFile file;
119     ios_openmode mode;
120     bool own_file_descriptor;
121
122     // Get (input) buffer.
123     int ibuf_size;
124     char* ibuffer;
125
126     // Put (output) buffer.
127     int obuf_size;
128     char* obuffer;
129
130     enum { page_size = 65536 };
131
132 private:
133     // Not defined
134     gzfilebuf( const gzfilebuf& );
135     void operator= ( const gzfilebuf& );
136 };
137
138 /**
139  * document me
140  */
141 struct gzifstream_base
142 {
143     gzifstream_base() {}
144
145     gzfilebuf gzbuf;
146 };
147
148 /**
149  * document me too
150  */
151 struct gzofstream_base
152 {
153     gzofstream_base() {}
154
155     gzfilebuf gzbuf;
156 };
157
158 #endif // _zfstream_hxx