summaryrefslogtreecommitdiff
path: root/include/io/zlib_streambuf.h
blob: 36062192b75d01e04961496caf14f57a4c4fb95b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef ZLIB_STREAMBUF_H_INCLUDED
#define ZLIB_STREAMBUF_H_INCLUDED

#include <stdexcept>
#include <streambuf>
#include <vector>
#include <zlib.h>

namespace zlib
{

///Exception thrown in case zlib encounters a problem
class zlib_error : public std::runtime_error
{
public:
    const int errcode;

    explicit zlib_error(const char* msg, int errcode):
        std::runtime_error(msg
                         ? std::string(zError(errcode)) + ": " + msg
                         : zError(errcode)),
        errcode(errcode)
    {}
};

///Base class for deflate_streambuf and inflate_streambuf
class zlib_streambuf : public std::streambuf
{
protected:
    std::vector<char> in;
    std::vector<char> out;
    z_stream zstr;

    explicit zlib_streambuf(size_t bufsize):
        in(bufsize), out(bufsize)
    {
        zstr.zalloc = Z_NULL;
        zstr.zfree = Z_NULL;
        zstr.opaque = Z_NULL;
    }
};

}

#endif // ZLIB_STREAMBUF_H_INCLUDED