]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
b6c0715ba1d73315e093773113c4a7d18d21d9c0
[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 #else
55 class gzfilebuf : public std::streambuf
56 #endif
57 {
58 public:
59     /** Constructor */
60     gzfilebuf();
61
62     /** Destructor */
63     virtual ~gzfilebuf();
64
65     /**
66      * Open a stream
67      * @param name file name
68      * @param io_mode mdoe flags
69      * @return file stream
70      */
71     gzfilebuf* open( const char* name, ios_openmode io_mode );
72
73     /** 
74      * Attach to an existing file descriptor
75      * @param file_descriptor file descriptor
76      * @param io_mode mode flags
77      * @return file stream
78      */
79     gzfilebuf* attach( int file_descriptor, ios_openmode io_mode );
80
81     /** Close stream */
82     gzfilebuf* close();
83
84     // int setcompressionlevel( int comp_level );
85     // int setcompressionstrategy( int comp_strategy );
86
87     /** @return true if open, false otherwise */
88     bool is_open() const { return (file != NULL); }
89
90     /** @return stream position */
91     virtual std::streampos seekoff( std::streamoff off, ios_seekdir way, int which );
92
93     /** sync the stream */
94     virtual int sync();
95
96 protected:
97
98     virtual int_type underflow();
99
100     virtual int_type overflow( int_type c = traits_type::eof() );
101 private:
102
103     int_type flushbuf();
104     int fillbuf();
105
106     // Convert io_mode to "rwab" string.
107     void cvt_iomode( char* mode_str, ios_openmode io_mode );
108
109 private:
110
111     gzFile file;
112     ios_openmode mode;
113     bool own_file_descriptor;
114
115     // Get (input) buffer.
116     int ibuf_size;
117     char* ibuffer;
118
119     enum { page_size = 4096 };
120
121 private:
122     // Not defined
123     gzfilebuf( const gzfilebuf& );
124     void operator= ( const gzfilebuf& );
125 };
126
127 /**
128  * document me
129  */
130 struct gzifstream_base
131 {
132     gzifstream_base() {}
133
134     gzfilebuf gzbuf;
135 };
136
137 #endif // _zfstream_hxx
138