ZBuffer.h
1/*
2 * Copyright (C) 2015 by Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef PT_ZBUFFER_H
30#define PT_ZBUFFER_H
31
32#include <Pt/Api.h>
33#include <Pt/StreamBuffer.h>
34#include <ios>
35#include <cstddef>
36
37typedef struct z_stream_s z_stream;
38
39namespace Pt {
40
64class PT_API ZBuffer : public BasicStreamBuffer<char>
65{
66 public:
69 enum Format
70 {
71 Zlib = 0,
73 };
74
78
81 ZBuffer(std::ios& ios, Format fmt = Zlib);
82
85 virtual ~ZBuffer();
86
89 void attach(std::ios& target);
90
93 void detach();
94
97 void discard();
98
101 void reset();
102
105 void reset(std::ios& target);
106
109 void finish();
110
113 std::size_t zcount() const { return _zcount; }
114
119 std::streamsize import(std::streamsize maxImport = 0);
120
121 std::streamsize import(const char* data, std::streamsize size);
122
123 protected:
124 // inheritdoc
125 virtual std::streamsize showmanyc();
126
127 // inheritdoc
128 virtual std::streamsize showfull();
129
130 // inheritdoc
131 virtual int sync();
132
133 // inheritdoc
134 virtual int_type underflow();
135
136 // inheritdoc
137 virtual int_type overflow(int_type ch);
138
139 private:
140 void inflateBuffer();
141
142 private:
143 std::ios* _target;
144 z_stream* _zread;
145 z_stream* _zwrite;
146 Format _format;
147
148 static const int _pbmax = 4;
149 static const int _bufmax = 4096;
150 char _ibuf[_bufmax];
151 char _obuf[_bufmax];
152
153 static const int _zbufmax = 4096;
154 char _zibuf[_zbufmax];
155 int _zibufsize;
156 char _zobuf[_zbufmax];
157
158 std::size_t _zcount;
159};
160
161} // namespace Pt
162
163#endif // PT_ZBUFFER_H
BasicStreamBuffer()
Definition StreamBuffer.h:139
void attach(std::ios &target)
Attach to target stream.
void reset(std::ios &target)
Attach to target stream, discard the buffer and reset the state.
virtual ~ZBuffer()
Destructor.
void discard()
Discards the buffer content and resets the state.
virtual std::streamsize showfull()
Returns the number of characters buffered for output.
void finish()
Finish and flush remaining data to the target stream.
std::size_t zcount() const
Returns the total number of decompressed bytes produced so far.
Definition ZBuffer.h:113
Format
Compression/decompression format.
Definition ZBuffer.h:70
@ Zlib
zlib header/trailer with Adler-32 checksum.
Definition ZBuffer.h:71
@ Gzip
gzip header/trailer with CRC-32 checksum (RFC 1952).
Definition ZBuffer.h:72
void detach()
Detach from target stream.
ZBuffer(std::ios &ios, Format fmt=Zlib)
Construct with target stream.
void reset()
Detach from target stream, discard the buffer and reset the state.
ZBuffer(Format fmt=Zlib)
Default Constructor.
Core module.
Definition Allocator.h:33