Scippy

SoPlex

Sequential object-oriented simPlex

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 <zlib.h>
41 
42 #define GZSTREAM_NAMESPACE gzstream
43 
44 #ifdef GZSTREAM_NAMESPACE
45 namespace 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 */
59 class gzstreambuf
60  : public std::streambuf
61 {
62 private:
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 
88 public:
89 
90  //------------------------------------
91  /**@name Construction / destruction */
92  //@{
93  /// default constructor
94  gzstreambuf()
95  : file(0)
96  , opened(0)
97  , mode(0)
98  {
99  setp( buffer, buffer + (bufferSize-1));
100  setg( buffer + 4, // beginning of putback area
101  buffer + 4, // read position
102  buffer + 4); // end position
103  // ASSERT: both input & output capabilities will not be used together
104  }
105  /// destructor
106  ~gzstreambuf()
107  {
108  close();
109  }
110  //@}
111 
112  //------------------------------------
113  /**@name Interface */
114  //@{
115  ///
116  int is_open()
117  {
118  return opened;
119  }
120  ///
121  gzstreambuf* open( const char* name, int open_mode );
122  ///
123  gzstreambuf* close();
124  ///
125  virtual int overflow( int c = EOF );
126  ///
127  virtual int underflow();
128  ///
129  virtual int sync();
130  //@}
131 };
132 
133 // ----------------------------------------------------------------------------
134 // class gzstreambase
135 // ----------------------------------------------------------------------------
136 
137 /**@class gzstreambase
138  @brief Internal class to implement gzstream.
139 */
140 class gzstreambase
141  : virtual public std::ios
142 {
143 protected:
144 
145  //------------------------------------
146  /**@name Data */
147  //@{
148  ///
149  gzstreambuf buf;
150  //@}
151 
152 public:
153 
154  //------------------------------------
155  /**@name Construction / destruction */
156  //@{
157  /// default constructor
158  gzstreambase()
159  {
160  init(&buf);
161  }
162  /// full constructor
163  gzstreambase( const char* _name, int _open_mode );
164  /// destructor
165  ~gzstreambase();
166  //@}
167 
168  //------------------------------------
169  /**@name Interface */
170  //@{
171  ///
172  void open( const char* _name, int _open_mode );
173  ///
174  void close();
175  ///
176  gzstreambuf* rdbuf()
177  {
178  return &buf;
179  }
180  //@}
181 };
182 
183 // ----------------------------------------------------------------------------
184 // User classes. Use igzstream and ogzstream analogously to ifstream and
185 // ofstream respectively. They read and write files based on the gz*
186 // function interface of the zlib. Files are compatible with gzip compression.
187 // ----------------------------------------------------------------------------
188 
189 // ----------------------------------------------------------------------------
190 // class igzstream
191 // ----------------------------------------------------------------------------
192 
193 /**@class igzstream
194  @brief Class to implement a gzip'd input stream.
195 */
196 class igzstream
197  : public std::istream
198  , public gzstreambase
199 {
200 public:
201 
202  //------------------------------------
203  /**@name Construction / destruction */
204  //@{
205  /// default constructor
206  igzstream()
207  : std::istream( &buf)
208  {}
209  /// full constructor
210  igzstream( const char* _name,
211  int _open_mode = std::ios::in )
212  : std::istream( &buf )
213  , gzstreambase( _name, _open_mode )
214  {}
215  //@}
216 
217  //------------------------------------
218  /**@name Interface */
219  //@{
220  ///
221  gzstreambuf* rdbuf()
222  {
223  return gzstreambase::rdbuf();
224  }
225  ///
226  void open( const char* _name,
227  int _open_mode = std::ios::in )
228  {
229  gzstreambase::open( _name, _open_mode );
230  }
231  //@}
232 };
233 
234 // ----------------------------------------------------------------------------
235 // class ogzstream
236 // ----------------------------------------------------------------------------
237 
238 /**@class ogzstream
239  @brief Class to implement a gzip'd output stream.
240 */
241 class ogzstream
242  : public gzstreambase
243  , public std::ostream
244 {
245 public:
246 
247  //------------------------------------
248  /**@name Construction / destruction */
249  //@{
250  /// default constructor
251  ogzstream()
252  : std::ostream( &buf)
253  {}
254  /// full constructor
255  explicit
256  ogzstream( const char* _name,
257  int _open_mode = std::ios::out )
258  : gzstreambase( _name, _open_mode )
259  , std::ostream( &buf)
260  {}
261  //@}
262 
263  //------------------------------------
264  /**@name Interface */
265  //@{
266  ///
267  gzstreambuf* rdbuf()
268  {
269  return gzstreambase::rdbuf();
270  }
271  ///
272  void open( const char* _name,
273  int _open_mode = std::ios::out )
274  {
275  gzstreambase::open( _name, _open_mode );
276  }
277 };
278 
279 #ifdef GZSTREAM_NAMESPACE
280 } // namespace GZSTREAM_NAMESPACE
281 #endif
282 
283 #endif // GZSTREAM_H
284 // ============================================================================
285 // EOF //
286 
287 #endif