1 // ============================================================================
2 // gzstream, C++ iostream classes wrapping the zlib compression library.
3 // Copyright (C) 2001 Deepak Bandyopadhyay, Lutz Kettner
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // ============================================================================
21 // Revision : $Revision: 1.5 $
22 // Revision_date : $Date: 2002/04/26 23:30:15 $
23 // Author(s) : Deepak Bandyopadhyay, Lutz Kettner
25 // Standard streambuf implementation following Nicolai Josuttis, "The
26 // Standard C++ Library".
27 // ============================================================================
32 // standard C++ with new header file names and std:: namespace
37 #include <boost/iostreams/filtering_stream.hpp>
38 #include <boost/iostreams/filter/gzip.hpp>
39 #include <boost/iostreams/device/file.hpp>
41 #ifdef GZSTREAM_NAMESPACE
42 namespace GZSTREAM_NAMESPACE
{
45 // ----------------------------------------------------------------------------
46 // Internal classes to implement gzstream. See below for user classes.
47 // ----------------------------------------------------------------------------
49 class gzstreambuf
: public std::streambuf
{
51 static const int bufferSize
= 47+256; // size of data buff
52 // totals 512 bytes under g++ for igzstream at the end.
54 gzFile file
; // file handle for compressed file
55 char buffer
[bufferSize
]; // data buffer
56 int opened
; // open/close state of stream
61 gzstreambuf() : opened(0)
63 setp( buffer
, buffer
+ (bufferSize
-1));
64 setg( buffer
+ 4, // beginning of putback area
65 buffer
+ 4, // read position
66 buffer
+ 4); // end position
67 // ASSERT: both input & output capabilities will not be used together
73 gzstreambuf
* open( const char* name
, int open_mode
);
75 ~gzstreambuf() { close(); }
77 virtual int overflow( int c
= EOF
);
78 virtual int underflow();
80 gzstreambuf
& operator=(const gzstreambuf
& gzs
);
83 class gzstreambase
: virtual public std::ios
92 gzstreambase( const char* name
, int open_mode
);
98 void open( const char* name
, int open_mode
);
104 gzstreambase
& operator=(const gzstreambase
& gzst
);
107 // ----------------------------------------------------------------------------
108 // User classes. Use igzstream and ogzstream analogously to ifstream and
109 // ofstream respectively. They read and write files based on the gz*
110 // function interface of the zlib. Files are compatible with gzip compression.
111 // ----------------------------------------------------------------------------
113 class igzstream
: public gzstreambase
, public std::istream
116 igzstream() : std::istream( &buf
)
118 /*gzstreambase::init(&buf);*/
120 igzstream( const char* name
, int open_mode
= std::ios::in
)
121 : gzstreambase( name
, open_mode
), std::istream( &buf
)
123 /*gzstreambase::init(&buf);*/
125 gzstreambuf
* rdbuf() { return gzstreambase::rdbuf(); }
126 void open( const char* name
, int open_mode
= std::ios::in
)
128 gzstreambase::open( name
, open_mode
);
132 class ogzstream
: public gzstreambase
, public std::ostream
135 ogzstream() : std::ostream( &buf
)
137 /*gzstreambase::init(&buf);*/
139 ogzstream( const char* name
, int mode
= std::ios::out
)
140 : gzstreambase( name
, mode
), std::ostream( &buf
)
142 /*gzstreambase::init(&buf);*/
146 return gzstreambase::rdbuf();
148 void open( const char* name
, int open_mode
= std::ios::out
)
150 gzstreambase::open( name
, open_mode
);
152 ogzstream
& operator=(const ogzstream
& ogzst
);
159 virtual void open(const char* name
, int open_mode
= std::ios::out
) = 0;
160 virtual void close() = 0;
161 virtual void clear() = 0;
162 virtual bool good() =0;
163 virtual std::ostream
& flush() =0;
164 virtual std::ostream
& endl() =0;
165 virtual void write(const char* name
, int streamsizecount
) =0;
166 virtual int is_open() = 0;
167 virtual myfstream
& operator<<(const double value
) = 0;
168 virtual myfstream
& operator<<(const char* value
) = 0;
169 virtual myfstream
& operator<<(const char value
) = 0;
170 virtual myfstream
& operator<<(const unsigned char value
) = 0;
171 virtual myfstream
& operator<<(const std::string
& value
) = 0;
172 virtual myfstream
& operator<<(const int value
) = 0;
173 virtual myfstream
& operator<<(const int* value
) = 0;
174 virtual myfstream
& operator<<(const unsigned int value
) = 0;
175 virtual myfstream
& operator<<(const float value
) = 0;
176 virtual int set_f(std::ios_base::fmtflags value
) = 0;
177 //virtual ostream & operator<<(std::ostream& value) = 0;
178 //virtual myfstream & operator<< (std::ostream& (__cdecl *fun) (ostream& value)) =0;
183 * a class to use ofstream.
186 class myofstream
: public myfstream
191 myofstream() : ogz(){}
192 myofstream(const char * name
, int mode
= std::ios::out
)
194 virtual inline void open( const char* name
, int open_mode
= std::ios::out
)
198 virtual inline void close()
202 virtual inline void write(const char* name
, int streamsizecount
)
204 ogz
.write(name
, streamsizecount
);
206 virtual inline std::ostream
& flush()
210 virtual inline std::ostream
& endl()
215 virtual inline void clear()
219 virtual inline bool good()
223 virtual inline int is_open()
225 return ogz
.is_open();
228 virtual inline myofstream
& operator<<(const double value
)
233 virtual inline myofstream
& operator<<(const char * value
)
238 virtual inline myofstream
& operator<<(const int value
)
243 virtual inline myofstream
& operator<<(const int* value
)
248 virtual inline myofstream
& operator<<(const unsigned int value
)
253 virtual inline myofstream
& operator<<(const char value
)
258 virtual inline myofstream
& operator<<(const unsigned char value
)
263 virtual inline myofstream
& operator<<(const std::string
& value
)
268 virtual inline myofstream
& operator<<(const float value
)
273 virtual inline int set_f(std::ios_base::fmtflags value
)
275 return ogz
.setf(value
);
277 /*virtual inline myofstream & operator<<(const std::ios& value)
282 /*virtual myofstream& operator<< (std::ostream& (__cdecl *fun)(ostream& value))
291 * a class to use ogzstream.
294 class myogzstream
: public myfstream
299 myogzstream() : ogz(){}
300 myogzstream(const char * name
, int mode
= std::ios::out
)
302 virtual inline void open( const char* name
, int open_mode
= std::ios::out
)
304 ogz
.open(name
, open_mode
);
306 virtual inline void close()
310 virtual inline bool good()
314 virtual inline void write(const char* name
, int streamsizecount
)
316 ogz
.write(name
,streamsizecount
);
318 virtual inline void clear()
322 virtual std::ostream
& flush()
326 virtual std::ostream
& endl()
331 virtual inline int is_open()
333 return ogz
.is_open();
336 virtual inline myogzstream
& operator<<(const double value
)
341 virtual inline myogzstream
& operator<<(const char * value
)
346 virtual inline myogzstream
& operator<<(const int value
)
351 virtual inline myogzstream
& operator<<(const int* value
)
356 virtual inline myogzstream
& operator<<(const unsigned int value
)
361 virtual inline myogzstream
& operator<<(const char value
)
366 virtual inline myogzstream
& operator<<(const unsigned char value
)
371 virtual inline myogzstream
& operator<<(const std::string
& value
)
376 virtual inline myogzstream
& operator<<(const float value
)
381 virtual inline int set_f(std::ios_base::fmtflags value
)
383 return ogz
.setf(value
);
385 /*virtual inline myogzstream & operator<<(const std::ios& value)
390 /*virtual myogzstream& operator<< (std::ostream& (__cdecl *fun)(ostream& value))
397 #ifdef GZSTREAM_NAMESPACE
398 } // namespace GZSTREAM_NAMESPACE
402 // ============================================================================