Scippy

UG

Ubiquity Generator framework

gzstream.h
Go to the documentation of this file.
1#ifdef UG_WITH_ZLIB
2
3// ============================================================================
4// gzstream, C++ iostream classes wrapping the zlib compression library.
5// Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
6//
7// This library is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 2.1 of the License, or (at your option) any later version.
11//
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15// Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public
18// License along with this library; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20// ============================================================================
21//
22// File : gzstream.h
23// Revision : $Revision: 1.8 $
24// Revision_date : $Date: 2005/11/09 13:53:50 $
25// Author(s) : Deepak Bandyopadhyay, Lutz Kettner
26//
27// Standard streambuf implementation following Nicolai Josuttis, "The
28// Standard C++ Library".
29// ============================================================================
30
31/**@file gzstream.h
32 * @brief Utilities for handling gzipped input and output streams.
33 */
34#ifndef GZSTREAM_H
35#define GZSTREAM_H 1
36
37// standard C++ with new header file names and std:: namespace
38#include <iostream>
39#include <fstream>
40#include <zlib.h>
41
42#define GZSTREAM_NAMESPACE gzstream
43
44#ifdef GZSTREAM_NAMESPACE
45namespace GZSTREAM_NAMESPACE {
46#endif
47
48// ----------------------------------------------------------------------------
49// Internal classes to implement gzstream. See below for user classes.
50// ----------------------------------------------------------------------------
51
52// ----------------------------------------------------------------------------
53// class gzstreambuf
54// ----------------------------------------------------------------------------
55
56/**@class gzstreambuf
57 @brief Internal class to implement gzstream.
58*/
60 : public std::streambuf
61{
62private:
63
64 //------------------------------------
65 /**@name Types */
66 //@{
67 ///
68 static const int bufferSize = 47+256; ///< size of data buff
69 // totals 512 bytes under g++ for igzstream at the end.
70 //@}
71
72 //------------------------------------
73 /**@name Data */
74 //@{
75 gzFile file; ///< file handle for compressed file
76 char buffer[bufferSize]; ///< data buffer
77 char opened; ///< open/close state of stream
78 int mode; ///< I/O mode
79 //@}
80
81 //------------------------------------
82 /**@name Internal helpers */
83 //@{
84 ///
85 int flush_buffer();
86 //@}
87
88public:
89
90 //------------------------------------
91 /**@name Construction / destruction */
92 //@{
93 /// default constructor
95 : opened(0)
96 {
97 setp( buffer, buffer + (bufferSize-1));
98 setg( buffer + 4, // beginning of putback area
99 buffer + 4, // read position
100 buffer + 4); // end position
101 // ASSERT: both input & output capabilities will not be used together
102 }
103 /// destructor
105 {
106 close();
107 }
108 //@}
109
110 //------------------------------------
111 /**@name Interface */
112 //@{
113 ///
114 int is_open()
115 {
116 return opened;
117 }
118 ///
119 gzstreambuf* open( const char* name, int open_mode );
120 ///
122 ///
123 virtual int overflow( int c = EOF );
124 ///
125 virtual int underflow();
126 ///
127 virtual int sync();
128 //@}
129};
130
131// ----------------------------------------------------------------------------
132// class gzstreambase
133// ----------------------------------------------------------------------------
134
135/**@class gzstreambase
136 @brief Internal class to implement gzstream.
137*/
139 : virtual public std::ios
140{
141protected:
142
143 //------------------------------------
144 /**@name Data */
145 //@{
146 ///
148 //@}
149
150public:
151
152 //------------------------------------
153 /**@name Construction / destruction */
154 //@{
155 /// default constructor
157 {
158 init(&buf);
159 }
160 /// full constructor
161 gzstreambase( const char* _name, int _open_mode );
162 /// destructor
164 //@}
165
166 //------------------------------------
167 /**@name Interface */
168 //@{
169 ///
170 void open( const char* _name, int _open_mode );
171 ///
172 void close();
173 ///
175 {
176 return &buf;
177 }
178 //@}
179};
180
181// ----------------------------------------------------------------------------
182// User classes. Use igzstream and ogzstream analogously to ifstream and
183// ofstream respectively. They read and write files based on the gz*
184// function interface of the zlib. Files are compatible with gzip compression.
185// ----------------------------------------------------------------------------
186
187// ----------------------------------------------------------------------------
188// class igzstream
189// ----------------------------------------------------------------------------
190
191/**@class igzstream
192 @brief Class to implement a gzip'd input stream.
193*/
195 : public std::istream
196 , public gzstreambase
197{
198public:
199
200 //------------------------------------
201 /**@name Construction / destruction */
202 //@{
203 /// default constructor
205 : std::istream( &buf)
206 {}
207 /// full constructor
208 igzstream( const char* _name,
209 int _open_mode = std::ios::in )
210 : std::istream( &buf )
211 , gzstreambase( _name, _open_mode )
212 {}
213 //@}
214
215 //------------------------------------
216 /**@name Interface */
217 //@{
218 ///
220 {
221 return gzstreambase::rdbuf();
222 }
223 ///
224 void open( const char* _name,
225 int _open_mode = std::ios::in )
226 {
227 gzstreambase::open( _name, _open_mode );
228 }
229 //@}
230};
231
232// ----------------------------------------------------------------------------
233// class ogzstream
234// ----------------------------------------------------------------------------
235
236/**@class ogzstream
237 @brief Class to implement a gzip'd output stream.
238*/
240 : public gzstreambase
241 , public std::ostream
242{
243public:
244
245 //------------------------------------
246 /**@name Construction / destruction */
247 //@{
248 /// default constructor
250 : std::ostream( &buf)
251 {}
252 /// full constructor
253 explicit
254 ogzstream( const char* _name,
255 int _open_mode = std::ios::out )
256 : gzstreambase( _name, _open_mode )
257 , std::ostream( &buf)
258 {}
259 //@}
260
261 //------------------------------------
262 /**@name Interface */
263 //@{
264 ///
266 {
267 return gzstreambase::rdbuf();
268 }
269 ///
270 void open( const char* _name,
271 int _open_mode = std::ios::out )
272 {
273 gzstreambase::open( _name, _open_mode );
274 }
275};
276
277#ifdef GZSTREAM_NAMESPACE
278} // namespace GZSTREAM_NAMESPACE
279#endif
280
281#endif // GZSTREAM_H
282// ============================================================================
283// EOF //
284
285#endif
Internal class to implement gzstream.
Definition: gzstream.h:140
gzstreambase()
default constructor
Definition: gzstream.h:156
void open(const char *_name, int _open_mode)
Definition: gzstream.cpp:151
Internal class to implement gzstream.
Definition: gzstream.h:61
char opened
open/close state of stream
Definition: gzstream.h:77
gzFile file
file handle for compressed file
Definition: gzstream.h:75
gzstreambuf()
default constructor
Definition: gzstream.h:94
static const int bufferSize
size of data buff
Definition: gzstream.h:68
virtual int overflow(int c=EOF)
Definition: gzstream.cpp:115
gzstreambuf * open(const char *name, int open_mode)
Definition: gzstream.cpp:47
char buffer[bufferSize]
data buffer
Definition: gzstream.h:76
Class to implement a gzip'd input stream.
Definition: gzstream.h:197
void open(const char *_name, int _open_mode=std::ios::in)
Definition: gzstream.h:224
igzstream()
default constructor
Definition: gzstream.h:204
igzstream(const char *_name, int _open_mode=std::ios::in)
full constructor
Definition: gzstream.h:208
Class to implement a gzip'd output stream.
Definition: gzstream.h:242
ogzstream()
default constructor
Definition: gzstream.h:249
ogzstream(const char *_name, int _open_mode=std::ios::out)
full constructor
Definition: gzstream.h:254
void open(const char *_name, int _open_mode=std::ios::out)
Definition: gzstream.h:270