Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
zfstream.h
Go to the documentation of this file.
1/*
2 * A C++ I/O streams interface to the zlib gz* functions
3 *
4 * by Ludwig Schwardt <schwardt@sun.ac.za>
5 * original version by Kevin Ruland <kevin@rodin.wustl.edu>
6 *
7 * This version is standard-compliant and compatible with gcc 3.x.
8 */
9
10#ifndef ZFSTREAM_H
11#define ZFSTREAM_H
12
13#include <istream> // not iostream, since we don't need cin/cout
14#include <ostream>
15#include "zlib.h"
16
17/*****************************************************************************/
18
27class gzfilebuf : public std::streambuf
28{
29public:
30 // Default constructor.
32
33 // Destructor.
34 virtual
36
48 int
49 setcompression(int comp_level,
50 int comp_strategy = Z_DEFAULT_STRATEGY);
51
56 bool
57 is_open() const { return (file != NULL); }
58
66 open(const char* name,
67 std::ios_base::openmode mode);
68
76 attach(int fd,
77 std::ios_base::openmode mode);
78
85
86protected:
91 bool
92 open_mode(std::ios_base::openmode mode,
93 char* c_mode) const;
94
102 virtual std::streamsize
104
112 virtual int_type
114
124 virtual int_type
125 overflow(int_type c = traits_type::eof());
126
135 virtual std::streambuf*
136 setbuf(char_type* p,
137 std::streamsize n);
138
145 virtual int
147
148//
149// Some future enhancements
150//
151// virtual int_type uflow();
152// virtual int_type pbackfail(int_type c = traits_type::eof());
153// virtual pos_type
154// seekoff(off_type off,
155// std::ios_base::seekdir way,
156// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
157// virtual pos_type
158// seekpos(pos_type sp,
159// std::ios_base::openmode mode = std::ios_base::in|std::ios_base::out);
160
161private:
170 void
171 enable_buffer();
172
180 void
181 disable_buffer();
182
186 gzFile file;
187
191 std::ios_base::openmode io_mode;
192
199 bool own_fd;
200
207 char_type* buffer;
208
215 std::streamsize buffer_size;
216
223 bool own_buffer;
224};
225
226/*****************************************************************************/
227
234class gzifstream : public std::istream
235{
236public:
237 // Default constructor
239
245 explicit
246 gzifstream(const char* name,
247 std::ios_base::openmode mode = std::ios_base::in);
248
254 explicit
256 std::ios_base::openmode mode = std::ios_base::in);
257
261 gzfilebuf*
262 rdbuf() const
263 { return const_cast<gzfilebuf*>(&sb); }
264
269 bool
270 is_open() { return sb.is_open(); }
271
284 void
285 open(const char* name,
286 std::ios_base::openmode mode = std::ios_base::in);
287
296 void
297 attach(int fd,
298 std::ios_base::openmode mode = std::ios_base::in);
299
305 void
307
308private:
312 gzfilebuf sb;
313};
314
315/*****************************************************************************/
316
323class gzofstream : public std::ostream
324{
325public:
326 // Default constructor
328
334 explicit
335 gzofstream(const char* name,
336 std::ios_base::openmode mode = std::ios_base::out);
337
343 explicit
345 std::ios_base::openmode mode = std::ios_base::out);
346
350 gzfilebuf*
351 rdbuf() const
352 { return const_cast<gzfilebuf*>(&sb); }
353
358 bool
359 is_open() { return sb.is_open(); }
360
373 void
374 open(const char* name,
375 std::ios_base::openmode mode = std::ios_base::out);
376
385 void
386 attach(int fd,
387 std::ios_base::openmode mode = std::ios_base::out);
388
394 void
396
397private:
401 gzfilebuf sb;
402};
403
404/*****************************************************************************/
405
412template<typename T1, typename T2>
414 {
415 public:
416 // Allows insertor to peek at internals
417 template <typename Ta, typename Tb>
420 const gzomanip2<Ta,Tb>&);
421
422 // Constructor
424 T1 v1,
425 T2 v2);
426 private:
427 // Underlying manipulator function
429 (*func)(gzofstream&, T1, T2);
430
431 // Arguments for manipulator function
432 T1 val1;
433 T2 val2;
434 };
435
436/*****************************************************************************/
437
438// Manipulator function thunks through to stream buffer
439inline gzofstream&
441{
442 (gzs.rdbuf())->setcompression(l, s);
443 return gzs;
444}
445
446// Manipulator constructor stores arguments
447template<typename T1, typename T2>
448 inline
450 T1 v1,
451 T2 v2)
452 : func(f), val1(v1), val2(v2)
453 { }
454
455// Insertor applies underlying manipulator function to stream
456template<typename T1, typename T2>
459 { return (*m.func)(s, m.val1, m.val2); }
460
461// Insert this onto stream to simplify setting of compression level
464{ return gzomanip2<int,int>(&setcompression, l, s); }
465
466#endif // ZFSTREAM_H
Gzipped file stream buffer class.
Definition: zfstream.h:8
virtual int_type overflow(int_type c=traits_type::eof())
Write put area to gzipped file.
gzfilebuf * close()
Close gzipped file.
virtual int_type underflow()
Fill get area from gzipped file.
bool is_open() const
Check if file is open.
Definition: zfstream.h:57
virtual int sync()
Flush stream buffer to file.
gzfilebuf * open(const char *name, std::ios_base::openmode mode)
Open gzipped file.
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
bool open_mode(std::ios_base::openmode mode, char *c_mode) const
Convert ios open mode int to mode string used by zlib.
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
virtual ~gzfilebuf()
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
gzfilebuf * attach(int fd, std::ios_base::openmode mode)
Attach to already open gzipped file.
int is_open() const
Definition: zfstream.h:22
Gzipped file input stream class.
Definition: zfstream.h:68
void close()
Close gzipped file.
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
bool is_open()
Check if file is open.
Definition: zfstream.h:270
gzifstream(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Construct stream on gzipped file to be opened.
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:262
gzifstream(int fd, std::ios_base::openmode mode=std::ios_base::in)
Construct stream on already open gzipped file.
Gzipped file output stream class.
Definition: zfstream.h:80
gzofstream(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Construct stream on gzipped file to be opened.
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:351
void close()
Close gzipped file.
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
gzofstream(int fd, std::ios_base::openmode mode=std::ios_base::out)
Construct stream on already open gzipped file.
bool is_open()
Check if file is open.
Definition: zfstream.h:359
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
Gzipped file output stream manipulator class.
Definition: zfstream.h:414
gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), T1 v1, T2 v2)
Definition: zfstream.h:449
friend gzofstream & operator<<(gzofstream &, const gzomanip2< Ta, Tb > &)
const char int mode
Definition: ioapi.h:137
gzofstream & setcompression(gzofstream &gzs, int l, int s=Z_DEFAULT_STRATEGY)
Definition: zfstream.h:440
gzofstream & operator<<(gzofstream &s, const gzomanip< T > &m)
Definition: zfstream.h:101
#define T2
Definition: md5.c:133
#define T1
Definition: md5.c:132
const char * name
Definition: nkf.c:208
#define NULL
Definition: regenc.h:69
#define f
Definition: gzappend.c:170
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200