synfig-core  1.0.3
zstreambuf.h
Go to the documentation of this file.
1 /* === S Y N F I G ========================================================= */
21 /* ========================================================================= */
22 
23 /* === S T A R T =========================================================== */
24 
25 #ifndef __SYNFIG_ZSTREAMBUF_H
26 #define __SYNFIG_ZSTREAMBUF_H
27 
28 /* === H E A D E R S ======================================================= */
29 
30 #include <streambuf>
31 #include <istream>
32 #include <ostream>
33 #include <vector>
34 #include <zlib.h>
35 #include "filesystem.h"
36 
37 /* === M A C R O S ========================================================= */
38 
39 /* === T Y P E D E F S ===================================================== */
40 
41 /* === C L A S S E S & S T R U C T S ======================================= */
42 
43 namespace synfig {
44 
45  class zstreambuf : public std::streambuf
46  {
47  public:
48  enum {
50  option_method = Z_DEFLATED,
52  option_window_bits = 16+MAX_WBITS,
54  option_strategy = Z_DEFAULT_STRATEGY
55  };
56 
57  private:
58  std::streambuf *buf_;
59 
60  bool inflate_initialized;
61  z_stream inflate_stream_;
62  std::vector<char> read_buffer_;
63 
64  bool deflate_initialized;
65  z_stream deflate_stream_;
66  std::vector<char> write_buffer_;
67 
68  bool inflate_buf();
69  bool deflate_buf(bool flush);
70 
71  public:
72  explicit zstreambuf(std::streambuf *buf);
73  virtual ~zstreambuf();
74 
75  protected:
76  virtual int sync();
77  virtual int underflow();
78  virtual int overflow(int c = EOF);
79  };
80 
82  {
83  private:
85  zstreambuf buf_;
86  std::istream istream_;
87 
88  protected:
89  virtual size_t internal_read(void *buffer, size_t size)
90  { return (size_t)istream_.read((char*)buffer, size).gcount(); }
91 
92  public:
94  FileSystem::ReadStream(stream->file_system()),
95  stream_(stream),
96  buf_(stream_->rdbuf()),
97  istream_(&buf_)
98  { }
99 
100  };
101 
103  {
104  private:
106  zstreambuf buf_;
107  std::ostream ostream_;
108 
109  protected:
110  virtual size_t internal_write(const void *buffer, size_t size)
111  {
112  for(size_t i = 0; i < size; i++)
113  if (!ostream_.put(((const char*)buffer)[i]).good())
114  return i;
115  return size;
116  }
117 
118  public:
120  FileSystem::WriteStream(stream->file_system()),
121  stream_(stream),
122  buf_(stream_->rdbuf()),
123  ostream_(&buf_)
124  { }
125  };
126 }
127 
128 /* === E N D =============================================================== */
129 
130 #endif