ZStream.h
1 /*
2  * Copyright (C) 2015 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,
27  * MA 02110-1301 USA
28  */
29 
30 #ifndef Pt_ZStream_h
31 #define Pt_ZStream_h
32 
33 #include <Pt/Api.h>
34 #include <Pt/IOStream.h>
35 #include <Pt/ZBuffer.h>
36 
37 namespace Pt {
38 
41 class ZIStream : public BasicIStream<char>
42 {
43  public:
47 
50  ZIStream(Format fmt = Format::Zlib)
51  : BasicIStream<char>(0)
52  , _buffer(fmt)
53  {
54  this->setBuffer(&_buffer);
55  }
56 
59  explicit ZIStream(std::istream& is, Format fmt = Format::Zlib)
60  : BasicIStream<char>(0)
61  , _buffer(is, fmt)
62  {
63  this->setBuffer(&_buffer);
64  }
65 
68  {}
69 
73  { return _buffer; }
74 
77  void attach(std::istream& is)
78  {
79  _buffer.attach(is);
80  }
81 
84  void detach()
85  {
86  _buffer.detach();
87  }
88 
91  void reset()
92  {
93  _buffer.reset();
94  }
95 
98  void reset(std::istream& is)
99  {
100  _buffer.reset(is);
101  }
102 
105  void finish()
106  {
107  _buffer.finish();
108  }
109 
112  std::size_t zcount() const
113  { return _buffer.zcount(); }
114 
115  private:
116  ZBuffer _buffer;
117 };
118 
121 class ZOStream : public BasicOStream<char>
122 {
123  public:
127 
130  ZOStream(Format fmt = Format::Zlib)
131  : BasicOStream<char>(0)
132  , _buffer(fmt)
133  {
134  this->setBuffer(&_buffer);
135  }
136 
139  explicit ZOStream(std::ostream& os, Format fmt = Format::Zlib)
140  : BasicOStream<char>(0)
141  , _buffer(os, fmt)
142  {
143  this->setBuffer(&_buffer);
144  }
145 
148  {}
149 
153  { return _buffer; }
154 
157  void attach(std::ostream& os)
158  {
159  _buffer.attach(os);
160  }
161 
164  void detach()
165  {
166  _buffer.detach();
167  }
168 
171  void reset()
172  {
173  _buffer.reset();
174  }
175 
178  void reset(std::ostream& os)
179  {
180  _buffer.reset(os);
181  }
182 
185  void finish()
186  {
187  _buffer.finish();
188  }
189 
190  private:
191  ZBuffer _buffer;
192 };
193 
196 class ZIOStream : public BasicIOStream<char>
197 {
198  public:
202 
206  : BasicIOStream<char>(0)
207  {
208  this->setBuffer(&_buffer);
209  }
210 
213  explicit ZIOStream(std::iostream& ios)
214  : BasicIOStream<char>(0)
215  , _buffer(ios)
216  {
217  this->setBuffer(&_buffer);
218  }
219 
222  {}
223 
227  { return _buffer; }
228 
231  void attach(std::iostream& ios)
232  {
233  _buffer.attach(ios);
234  }
235 
238  void detach()
239  {
240  _buffer.detach();
241  }
242 
245  void reset()
246  {
247  _buffer.reset();
248  }
249 
252  void reset(std::iostream& ios)
253  {
254  _buffer.reset(ios);
255  }
256 
259  void finish()
260  {
261  _buffer.finish();
262  }
263 
266  std::size_t zcount() const
267  { return _buffer.zcount(); }
268 
269  private:
270  ZBuffer _buffer;
271 };
272 
273 } // namespace Pt
274 
275 #endif // Pt_ZStream_h
Core module.
Definition: Allocator.h:33
void finish()
Finish and flush remaining data to the target stream.
Definition: ZStream.h:105
~ZIOStream()
Destructor.
Definition: ZStream.h:221
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:87
void reset(std::ostream &os)
Reset to begin new compression/decompression.
Definition: ZStream.h:178
void attach(std::iostream &ios)
Attach to target stream.
Definition: ZStream.h:231
void reset(std::istream &is)
Reset to begin new compression/decompression.
Definition: ZStream.h:98
void detach()
Detach from target stream.
Definition: ZStream.h:164
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:127
ZOStream(Format fmt=Format::Zlib)
Construct with format.
Definition: ZStream.h:130
std::size_t zcount() const
Returns the total number of decompressed bytes produced so far.
Definition: ZStream.h:266
Output stream for zlib compression.
Definition: ZStream.h:122
Stream buffer for zlib compression.
Definition: ZBuffer.h:44
ZIOStream(std::iostream &ios)
Construct with target stream.
Definition: ZStream.h:213
void finish()
Finish and flush remaining data to the target stream.
Definition: ZStream.h:185
void reset()
Detach from target stream, discard the buffer and reset the state.
I/O stream for zlib compression.
Definition: ZStream.h:197
Input/Output stream.
Definition: IOStream.h:142
void reset()
Reset to begin new compression/decompression.
Definition: ZStream.h:91
ZIStream(Format fmt=Format::Zlib)
Construct with format.
Definition: ZStream.h:50
void detach()
Detach from target stream.
Definition: ZStream.h:238
void reset()
Reset to begin new compression/decompression.
Definition: ZStream.h:171
Input stream for zlib compression.
Definition: ZStream.h:42
void detach()
Detach from target stream.
~ZOStream()
Destructor.
Definition: ZStream.h:147
void detach()
Detach from target stream.
Definition: ZStream.h:84
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:175
void reset()
Reset to begin new compression/decompression.
Definition: ZStream.h:245
Input stream.
Definition: IOStream.h:58
~ZIStream()
Destructor.
Definition: ZStream.h:67
void attach(std::ios &target)
Attach to target stream.
ZBuffer & zBuffer()
Returns the compression buffer.
Definition: ZStream.h:72
void finish()
Finish and flush remaining data to the target stream.
Format
Compression/decompression format.
Definition: ZBuffer.h:49
ZBuffer & zBuffer()
Returns the compression buffer.
Definition: ZStream.h:226
ZIStream(std::istream &is, Format fmt=Format::Zlib)
Construct with target stream.
Definition: ZStream.h:59
ZBuffer & zBuffer()
Returns the compression buffer.
Definition: ZStream.h:152
Output stream.
Definition: IOStream.h:102
void attach(std::istream &is)
Attach to target stream.
Definition: ZStream.h:77
void attach(std::ostream &os)
Attach to target stream.
Definition: ZStream.h:157
ZIOStream()
Construct with target stream.
Definition: ZStream.h:205
void reset(std::iostream &ios)
Reset to begin new compression/decompression.
Definition: ZStream.h:252
std::size_t zcount() const
Returns the total number of decompressed bytes produced so far.
Definition: ZStream.h:112
ZOStream(std::ostream &os, Format fmt=Format::Zlib)
Construct with target stream.
Definition: ZStream.h:139
void finish()
Finish and flush remaining data to the target stream.
Definition: ZStream.h:259
std::size_t zcount() const
Returns the total number of decompressed bytes produced so far.
Definition: ZBuffer.h:92