summaryrefslogtreecommitdiffstats
path: root/src/streams-zlib.h
blob: 6ab7405d1013d2aa0cd591797a1e10a56c6b919e (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * IO layer : zlib streambuf
 *
 * Authors:
 *   Johan Ceuppens <jceuppen at easynet dot be>
 *
 * Copyright (C) 2004 Johan Ceuppens
 *
 * Released under GNU LGPL, read the file 'COPYING.LIB' for more information
 */

#ifndef __STREAMS_ZLIB_H_
#define __STREAMS_ZLIB_H_

#include "streams-handles.h"

#include <glib/gtypes.h>
#include <glib/garray.h>
#include <zlib.h>
#include <iostream>

namespace Inkscape {

class ZlibBufferException : public std::exception {};

// This is the initial buffersize for the stream and
// zipbuffers (the streambuffers expand as needed).
const unsigned int BUFSIZE_STREAM = 4096; 

/**
 * ZlibBuffer
 */ 

//TODO: unbuffered IO
class ZlibBuffer : public std::streambuf
{
public:

    ZlibBuffer(URIHandle& urih);
    virtual ~ZlibBuffer() {}
    
protected:

    virtual int allocate_buffers();
    virtual int reallocate_buffers(int new_getsize, int new_putsize);
    virtual int underflow();
    virtual int overflow(int c = EOF);
    virtual int flush_output();

    virtual void init_inflation() throw(ZlibBufferException);
    virtual void reset_inflation() throw(ZlibBufferException);
    virtual int consume_and_inflate();
    virtual int do_consume_and_inflate(int nbytes);
    virtual int consume(guint8 *buf, int nbytes);
    virtual int do_consume(guint8 *buf, int nbytes);
    virtual GByteArray *inflate(guint8 *in_buffer, int nbytes);
    virtual GByteArray *do_inflate(guint8 *in_buffer, int nbytes);
    virtual int copy_to_get(guint8 *data, int nbytes);
    virtual int do_copy_to_get(guint8 *data, int nbytes);

    URIHandle *_urihandle;

private:

    ZlibBuffer& operator=(ZlibBuffer const& rhs);
    ZlibBuffer(ZlibBuffer const& rhs);

    z_stream _zs;
    int _putsize, _getsize;//sizes of in and out buffers
    
};

class izstream : public std::istream {
public:

    explicit izstream(std::streambuf& sb) : std::istream(&sb) {}
    ~izstream() { std::ios::init(0); }
    
    std::streambuf *rdbuf() { return std::ios::rdbuf(); }
    std::streambuf *operator ->() { return rdbuf(); }

};

}

#endif

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :