]> git.mxchange.org Git - simgear.git/blob - simgear/misc/zfstream.hxx
Removal of PLIB/SG from SimGear
[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 mdoe 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, int 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 private:
104
105     int_type flushbuf();
106     int fillbuf();
107
108     // Convert io_mode to "rwab" string.
109     void cvt_iomode( char* mode_str, ios_openmode io_mode );
110
111 private:
112
113     gzFile file;
114     ios_openmode mode;
115     bool own_file_descriptor;
116
117     // Get (input) buffer.
118     int ibuf_size;
119     char* ibuffer;
120
121     enum { page_size = 4096 };
122
123 private:
124     // Not defined
125     gzfilebuf( const gzfilebuf& );
126     void operator= ( const gzfilebuf& );
127 };
128
129 /**
130  * document me
131  */
132 struct gzifstream_base
133 {
134     gzifstream_base() {}
135
136     gzfilebuf gzbuf;
137 };
138
139 #endif // _zfstream_hxx
140