1 /*
2 * IO layer : zlib streambuf
3 *
4 * Authors:
5 * Johan Ceuppens <jceuppen at easynet dot be>
6 *
7 * Copyright (C) 2004 Johan Ceuppens
8 *
9 * Released under GNU LGPL, read the file 'COPYING.LIB' for more information
10 */
12 #ifndef __STREAMS_ZLIB_H_
13 #define __STREAMS_ZLIB_H_
15 #include "streams-handles.h"
17 #include <glib/gtypes.h>
18 #include <glib/garray.h>
19 #include <zlib.h>
20 #include <iostream>
22 namespace Inkscape {
24 class ZlibBufferException : public std::exception {};
26 // This is the initial buffersize for the stream and
27 // zipbuffers (the streambuffers expand as needed).
28 const unsigned int BUFSIZE_STREAM = 4096;
30 /**
31 * ZlibBuffer
32 */
34 //TODO: unbuffered IO
35 class ZlibBuffer : public std::streambuf
36 {
37 public:
39 ZlibBuffer(URIHandle& urih);
40 virtual ~ZlibBuffer() {}
42 protected:
44 virtual int allocate_buffers();
45 virtual int reallocate_buffers(int new_getsize, int new_putsize);
46 virtual int underflow();
47 virtual int overflow(int c = EOF);
48 virtual int flush_output();
50 virtual void init_inflation() throw(ZlibBufferException);
51 virtual void reset_inflation() throw(ZlibBufferException);
52 virtual int consume_and_inflate();
53 virtual int do_consume_and_inflate(int nbytes);
54 virtual int consume(guint8 *buf, int nbytes);
55 virtual int do_consume(guint8 *buf, int nbytes);
56 virtual GByteArray *inflate(guint8 *in_buffer, int nbytes);
57 virtual GByteArray *do_inflate(guint8 *in_buffer, int nbytes);
58 virtual int copy_to_get(guint8 *data, int nbytes);
59 virtual int do_copy_to_get(guint8 *data, int nbytes);
61 URIHandle *_urihandle;
63 private:
65 ZlibBuffer& operator=(ZlibBuffer const& rhs);
66 ZlibBuffer(ZlibBuffer const& rhs);
68 z_stream _zs;
69 int _putsize, _getsize;//sizes of in and out buffers
71 };
73 class izstream : public std::istream {
74 public:
76 explicit izstream(std::streambuf& sb) : std::istream(&sb) {}
77 ~izstream() { std::ios::init(0); }
79 std::streambuf *rdbuf() { return std::ios::rdbuf(); }
80 std::streambuf *operator ->() { return rdbuf(); }
82 };
84 }
86 #endif
88 /*
89 Local Variables:
90 mode:c++
91 c-file-style:"stroustrup"
92 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
93 indent-tabs-mode:nil
94 fill-column:99
95 End:
96 */
97 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :