Scippy

SCIP

Solving Constraint Integer Programs

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