SoPlex Documentation
Loading...
Searching...
No Matches
gzstream.h
Go to the documentation of this file.
1#ifdef SOPLEX_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 <cstring>
41#include <zlib.h>
42
43#define SOPLEX_GZSTREAM_NAMESPACE gzstream
44
45#ifdef SOPLEX_GZSTREAM_NAMESPACE
46namespace SOPLEX_GZSTREAM_NAMESPACE
47{
48#endif
49
50// ----------------------------------------------------------------------------
51// Internal classes to implement gzstream. See below for user classes.
52// ----------------------------------------------------------------------------
53
54// ----------------------------------------------------------------------------
55// class gzstreambuf
56// ----------------------------------------------------------------------------
57
58/**@class gzstreambuf
59 @brief Internal class to implement gzstream.
60*/
61class gzstreambuf
62 : public std::streambuf
63{
64private:
65
66 //------------------------------------
67 /**@name Types */
68 ///@{
69 ///
70 static const int bufferSize = 47 + 256; ///< size of data buff
71 // totals 512 bytes under g++ for igzstream at the end.
72 ///@}
73
74 //------------------------------------
75 /**@name Data */
76 ///@{
77 gzFile file; ///< file handle for compressed file
78 char buffer[bufferSize]; ///< data buffer
79 char opened; ///< open/close state of stream
80 unsigned int mode; ///< I/O mode
81 ///@}
82
83 //------------------------------------
84 /**@name Internal helpers */
85 ///@{
86 ///
87 int flush_buffer();
88 ///@}
89
90public:
91
92 //------------------------------------
93 /**@name Construction / destruction */
94 ///@{
95 /// default constructor
96 gzstreambuf()
97 : file(0)
98 , opened(0)
99 , mode(0)
100 {
101 setp(buffer, buffer + (bufferSize - 1));
102 setg(buffer + 4, // beginning of putback area
103 buffer + 4, // read position
104 buffer + 4); // end position
105 // ASSERT: both input & output capabilities will not be used together
106 }
107
108 /// copy constructor
109 gzstreambuf(const gzstreambuf& sb)
110 : std::streambuf()
111 {
112 operator=(sb);
113 }
114 /// copy assignment operator
115 gzstreambuf& operator=(const gzstreambuf& sb)
116 {
117 std::streambuf::operator=(sb);
118
119 if(this != &sb)
120 {
121 opened = sb.opened;
122 mode = sb.mode;
123 file = sb.file;
124 std::strncpy(buffer, sb.buffer, bufferSize);
125 }
126
127 return *this;
128 }
129
130 /// destructor
131 ~gzstreambuf()
132 {
133 close();
134 }
135 ///@}
136
137 //------------------------------------
138 /**@name Interface */
139 ///@{
140 ///
141 int is_open()
142 {
143 return opened;
144 }
145 ///
146 gzstreambuf* open(const char* name, int open_mode);
147 ///
148 gzstreambuf* close();
149 ///
150 virtual int overflow(int c = EOF);
151 ///
152 virtual int underflow();
153 ///
154 virtual int sync();
155 ///@}
156};
157
158// ----------------------------------------------------------------------------
159// class gzstreambase
160// ----------------------------------------------------------------------------
161
162/**@class gzstreambase
163 @brief Internal class to implement gzstream.
164*/
165class gzstreambase
166 : virtual public std::ios
167{
168protected:
169
170 //------------------------------------
171 /**@name Data */
172 ///@{
173 ///
174 gzstreambuf buf;
175 ///@}
176
177public:
178
179 //------------------------------------
180 /**@name Construction / destruction */
181 ///@{
182 /// default constructor
183 gzstreambase()
184 {
185 init(&buf);
186 }
187 /// full constructor
188 gzstreambase(const char* _name, int _open_mode);
189 /// destructor
190 ~gzstreambase();
191 ///@}
192
193 //------------------------------------
194 /**@name Interface */
195 ///@{
196 ///
197 void open(const char* _name, int _open_mode);
198 ///
199 void close();
200 ///
201 gzstreambuf* rdbuf()
202 {
203 return &buf;
204 }
205 ///@}
206};
207
208// ----------------------------------------------------------------------------
209// User classes. Use igzstream and ogzstream analogously to ifstream and
210// ofstream respectively. They read and write files based on the gz*
211// function interface of the zlib. Files are compatible with gzip compression.
212// ----------------------------------------------------------------------------
213
214// ----------------------------------------------------------------------------
215// class igzstream
216// ----------------------------------------------------------------------------
217
218/**@class igzstream
219 @brief Class to implement a gzip'd input stream.
220*/
221class igzstream
222 : public std::istream
223 , public gzstreambase
224{
225public:
226
227 //------------------------------------
228 /**@name Construction / destruction */
229 ///@{
230 /// default constructor
231 igzstream()
232 : std::istream(&buf)
233 {}
234 /// full constructor
235 igzstream(const char* _name,
236 int _open_mode = std::ios::in)
237 : std::istream(&buf)
238 , gzstreambase(_name, _open_mode)
239 {}
240 ///@}
241
242 //------------------------------------
243 /**@name Interface */
244 ///@{
245 ///
246 gzstreambuf* rdbuf()
247 {
248 return gzstreambase::rdbuf();
249 }
250 ///
251 void open(const char* _name,
252 int _open_mode = std::ios::in)
253 {
254 gzstreambase::open(_name, _open_mode);
255 }
256 ///@}
257};
258
259// ----------------------------------------------------------------------------
260// class ogzstream
261// ----------------------------------------------------------------------------
262
263/**@class ogzstream
264 @brief Class to implement a gzip'd output stream.
265*/
266class ogzstream
267 : public gzstreambase
268 , public std::ostream
269{
270public:
271
272 //------------------------------------
273 /**@name Construction / destruction */
274 ///@{
275 /// default constructor
276 ogzstream()
277 : std::ostream(&buf)
278 {}
279 /// full constructor
280 explicit
281 ogzstream(const char* _name,
282 int _open_mode = std::ios::out)
283 : gzstreambase(_name, _open_mode)
284 , std::ostream(&buf)
285 {}
286 ///@}
287
288 //------------------------------------
289 /**@name Interface */
290 ///@{
291 ///
292 gzstreambuf* rdbuf()
293 {
294 return gzstreambase::rdbuf();
295 }
296 ///
297 void open(const char* _name,
298 int _open_mode = std::ios::out)
299 {
300 gzstreambase::open(_name, _open_mode);
301 }
302};
303
304#ifdef SOPLEX_GZSTREAM_NAMESPACE
305} // namespace SOPLEX_GZSTREAM_NAMESPACE
306#endif
307
308#endif // _GZSTREAM_H_
309// ============================================================================
310// EOF //
311
312#endif