Ruby: ext/zlib/zlib-1.2.13/contrib/iostream3/zfstream.h Source File

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.
31 gzfilebuf();
32
33 // Destructor.
34 virtual
35 ~gzfilebuf();
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
65 gzfilebuf*
66 open(const char* name,
67 std::ios_base::openmode mode);
68
75 gzfilebuf*
76 attach(int fd,
77 std::ios_base::openmode mode);
78
83 gzfilebuf*
84 close();
85
86protected:
91 bool
92 open_mode(std::ios_base::openmode mode,
93 char* c_mode) const;
94
102 virtual std::streamsize
103 showmanyc();
104
112 virtual int_type
113 underflow();
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
146 sync();
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
238 gzifstream();
239
245 explicit
246 gzifstream(const char* name,
247 std::ios_base::openmode mode = std::ios_base::in);
248
254 explicit
255 gzifstream(int fd,
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
306 close();
307
308private:
312 gzfilebuf sb;
313};
314
315/*****************************************************************************/
316
323class gzofstream : public std::ostream
324{
325public:
326 // Default constructor
327 gzofstream();
328
334 explicit
335 gzofstream(const char* name,
336 std::ios_base::openmode mode = std::ios_base::out);
337
343 explicit
344 gzofstream(int fd,
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
395 close();
396
397private:
401 gzfilebuf sb;
402};
403
404/*****************************************************************************/
405
412template<typename T1, typename T2>
413 class gzomanip2
414 {
415 public:
416 // Allows insertor to peek at internals
417 template <typename Ta, typename Tb>
418 friend gzofstream&
419 operator<<(gzofstream&,
420 const gzomanip2<Ta,Tb>&);
421
422 // Constructor
423 gzomanip2(gzofstream& (*f)(gzofstream&, T1, T2),
424 T1 v1,
425 T2 v2);
426 private:
427 // Underlying manipulator function
428 gzofstream&
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&
440setcompression(gzofstream &gzs, int l, int s = Z_DEFAULT_STRATEGY)
441{
442 (gzs.rdbuf())->setcompression(l, s);
443 return gzs;
444}
445
446// Manipulator constructor stores arguments
447template<typename T1, typename T2>
448 inline
449 gzomanip2<T1,T2>::gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2),
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>
457 inline gzofstream&
458 operator<<(gzofstream& s, const gzomanip2<T1,T2>& m)
459 { return (*m.func)(s, m.val1, m.val2); }
460
461// Insert this onto stream to simplify setting of compression level
462inline gzomanip2<int,int>
463 setcompression(int l, int s = Z_DEFAULT_STRATEGY)
464{ return gzomanip2<int,int>(&setcompression, l, s); }
465
466#endif // ZFSTREAM_H
gzfilebuf
Gzipped file stream buffer class.
Definition: zfstream.h:8
gzfilebuf::overflow
virtual int_type overflow(int_type c=traits_type::eof())
Write put area to gzipped file.
gzfilebuf::close
gzfilebuf * close()
Close gzipped file.
gzfilebuf::underflow
virtual int_type underflow()
Fill get area from gzipped file.
gzfilebuf::is_open
bool is_open() const
Check if file is open.
Definition: zfstream.h:57
gzfilebuf::sync
virtual int sync()
Flush stream buffer to file.
gzfilebuf::open
gzfilebuf * open(const char *name, std::ios_base::openmode mode)
Open gzipped file.
gzfilebuf::gzfilebuf
gzfilebuf()
gzfilebuf::showmanyc
virtual std::streamsize showmanyc()
Number of characters available in stream buffer.
gzfilebuf::open_mode
bool open_mode(std::ios_base::openmode mode, char *c_mode) const
Convert ios open mode int to mode string used by zlib.
gzfilebuf::setbuf
virtual std::streambuf * setbuf(char_type *p, std::streamsize n)
Installs external stream buffer.
gzfilebuf::~gzfilebuf
virtual ~gzfilebuf()
gzfilebuf::setcompression
int setcompression(int comp_level, int comp_strategy=Z_DEFAULT_STRATEGY)
Set compression level and strategy on the fly.
gzfilebuf::attach
gzfilebuf * attach(int fd, std::ios_base::openmode mode)
Attach to already open gzipped file.
gzfilebuf::is_open
int is_open() const
Definition: zfstream.h:22
gzifstream
Gzipped file input stream class.
Definition: zfstream.h:68
gzifstream::close
void close()
Close gzipped file.
gzifstream::attach
void attach(int fd, std::ios_base::openmode mode=std::ios_base::in)
Attach to already open gzipped file.
gzifstream::gzifstream
gzifstream()
gzifstream::open
void open(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Open gzipped file.
gzifstream::is_open
bool is_open()
Check if file is open.
Definition: zfstream.h:270
gzifstream::gzifstream
gzifstream(const char *name, std::ios_base::openmode mode=std::ios_base::in)
Construct stream on gzipped file to be opened.
gzifstream::rdbuf
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:262
gzifstream::gzifstream
gzifstream(int fd, std::ios_base::openmode mode=std::ios_base::in)
Construct stream on already open gzipped file.
gzofstream
Gzipped file output stream class.
Definition: zfstream.h:80
gzofstream::gzofstream
gzofstream(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Construct stream on gzipped file to be opened.
gzofstream::rdbuf
gzfilebuf * rdbuf() const
Obtain underlying stream buffer.
Definition: zfstream.h:351
gzofstream::close
void close()
Close gzipped file.
gzofstream::attach
void attach(int fd, std::ios_base::openmode mode=std::ios_base::out)
Attach to already open gzipped file.
gzofstream::gzofstream
gzofstream(int fd, std::ios_base::openmode mode=std::ios_base::out)
Construct stream on already open gzipped file.
gzofstream::is_open
bool is_open()
Check if file is open.
Definition: zfstream.h:359
gzofstream::gzofstream
gzofstream()
gzofstream::open
void open(const char *name, std::ios_base::openmode mode=std::ios_base::out)
Open gzipped file.
gzomanip2
Gzipped file output stream manipulator class.
Definition: zfstream.h:414
gzomanip2::gzomanip2
gzomanip2(gzofstream &(*f)(gzofstream &, T1, T2), T1 v1, T2 v2)
Definition: zfstream.h:449
gzomanip2::operator<<
friend gzofstream & operator<<(gzofstream &, const gzomanip2< Ta, Tb > &)
mode
const char int mode
Definition: ioapi.h:137
setcompression
gzofstream & setcompression(gzofstream &gzs, int l, int s=Z_DEFAULT_STRATEGY)
Definition: zfstream.h:440
operator<<
gzofstream & operator<<(gzofstream &s, const gzomanip< T > &m)
Definition: zfstream.h:101
T2
#define T2
Definition: md5.c:133
T1
#define T1
Definition: md5.c:132
name
const char * name
Definition: nkf.c:208
NULL
#define NULL
Definition: regenc.h:69
f
#define f
file
Definition: gzappend.c:170
gzFile_s
Definition: zlib.h:1834
zlib.h
Z_DEFAULT_STRATEGY
#define Z_DEFAULT_STRATEGY
Definition: zlib.h:200