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
61class PT_API ZBuffer : public BasicStreamBuffer<char>
62{
63 public:
66 enum Format
67 {
68 Zlib = 0,
70 };
71
75
78 ZBuffer(std::ios& ios, Format fmt = Zlib);
79
82 virtual ~ZBuffer();
83
86 void attach(std::ios& target);
87
90 void detach();
91
94 void discard();
95
98 void reset();
99
102 void reset(std::ios& target);
103
106 void finish();
107
110 std::size_t zcount() const { return _zcount; }
111
116 std::streamsize import(std::streamsize maxImport = 0);
117
118 std::streamsize import(const char* data, std::streamsize size);
119
120 protected:
121 // inheritdoc
122 virtual std::streamsize showmanyc();
123
124 // inheritdoc
125 virtual std::streamsize showfull();
126
127 // inheritdoc
128 virtual int sync();
129
130 // inheritdoc
131 virtual int_type underflow();
132
133 // inheritdoc
134 virtual int_type overflow(int_type ch);
135
136 private:
137 void inflateBuffer();
138
139 private:
140 std::ios* _target;
141 z_stream* _zstr;
142 Format _format;
143
144 static const int _pbmax = 4;
145 static const int _bufmax = 4096;
146 char _buf[_bufmax];
147
148 static const int _zbufmax = 4096;
149 char _zbuf[_zbufmax];
150 int _zbufsize;
151
152 std::size_t _zcount;
153};
154
155} // namespace Pt
156
157#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:110
Format
Compression/decompression format.
Definition ZBuffer.h:67
@ Zlib
zlib header/trailer with Adler-32 checksum.
Definition ZBuffer.h:68
@ Gzip
gzip header/trailer with CRC-32 checksum (RFC 1952).
Definition ZBuffer.h:69
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