summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa <ljfa-ag@web.de>2015-09-21 13:26:04 +0200
committerljfa <ljfa-ag@web.de>2015-09-21 13:26:04 +0200
commit76dcc4a9d1c44f7078780dea2756dff3a75d234f (patch)
tree16e6b8fbb630de92f11b3b17d067ceb7ae311837
parentf76d4d228b32e72c7de250cb37af264d38ee7e79 (diff)
downloadProject-Tick-76dcc4a9d1c44f7078780dea2756dff3a75d234f.tar.gz
Project-Tick-76dcc4a9d1c44f7078780dea2756dff3a75d234f.zip
Create zlib_streambuf base class
-rw-r--r--include/io/izlibstream.h7
-rw-r--r--include/io/ozlibstream.h8
-rw-r--r--include/io/zlib_error.h21
-rw-r--r--include/io/zlib_streambuf.h45
-rw-r--r--src/io/izlibstream.cpp7
-rw-r--r--src/io/ozlibstream.cpp7
-rw-r--r--test/zlibstream_test.h1
7 files changed, 54 insertions, 42 deletions
diff --git a/include/io/izlibstream.h b/include/io/izlibstream.h
index 43d2d2c02e..d3c4faa14e 100644
--- a/include/io/izlibstream.h
+++ b/include/io/izlibstream.h
@@ -20,8 +20,8 @@
#ifndef IZLIBSTREAM_H_INCLUDED
#define IZLIBSTREAM_H_INCLUDED
+#include "io/zlib_streambuf.h"
#include <istream>
-#include <vector>
#include <zlib.h>
namespace zlib
@@ -31,7 +31,7 @@ namespace zlib
* @brief Stream buffer used by zlib::izlibstream
* @sa izlibstream
*/
-class inflate_streambuf : public std::streambuf
+class inflate_streambuf : public zlib_streambuf
{
public:
/**
@@ -53,9 +53,6 @@ public:
private:
std::istream& is;
- std::vector<char> in;
- std::vector<char> out;
- z_stream zstr;
bool stream_end;
int_type underflow() override;
diff --git a/include/io/ozlibstream.h b/include/io/ozlibstream.h
index bd9faea381..ef93944c94 100644
--- a/include/io/ozlibstream.h
+++ b/include/io/ozlibstream.h
@@ -20,8 +20,8 @@
#ifndef OZLIBSTREAM_H_INCLUDED
#define OZLIBSTREAM_H_INCLUDED
+#include "io/zlib_streambuf.h"
#include <ostream>
-#include <vector>
#include <zlib.h>
namespace zlib
@@ -31,7 +31,7 @@ namespace zlib
* @brief Stream buffer used by zlib::ozlibstream
* @sa ozlibstream
*/
-class deflate_streambuf : public std::streambuf
+class deflate_streambuf : public zlib_streambuf
{
public:
/**
@@ -46,15 +46,13 @@ public:
explicit deflate_streambuf(std::ostream& output, size_t bufsize = 32768, int level = Z_DEFAULT_COMPRESSION, int window_bits = 15, int mem_level = 8, int strategy = Z_DEFAULT_STRATEGY);
~deflate_streambuf() noexcept;
+ ///@return the wrapped ostream
std::ostream& get_ostr() const { return os; }
///Finishes compression and writes all pending data to the output
void close();
private:
std::ostream& os;
- std::vector<char> in;
- std::vector<char> out;
- z_stream zstr;
void deflate_chunk(int flush = Z_NO_FLUSH);
diff --git a/include/io/zlib_error.h b/include/io/zlib_error.h
deleted file mode 100644
index 1ecb50e553..0000000000
--- a/include/io/zlib_error.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef ZLIB_ERROR_H_INCLUDED
-#define ZLIB_ERROR_H_INCLUDED
-
-#include <stdexcept>
-#include <zlib.h>
-
-///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)
- {}
-};
-
-#endif // ZLIB_ERROR_H_INCLUDED
diff --git a/include/io/zlib_streambuf.h b/include/io/zlib_streambuf.h
new file mode 100644
index 0000000000..36062192b7
--- /dev/null
+++ b/include/io/zlib_streambuf.h
@@ -0,0 +1,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
diff --git a/src/io/izlibstream.cpp b/src/io/izlibstream.cpp
index 6b7a75189a..85e04a189a 100644
--- a/src/io/izlibstream.cpp
+++ b/src/io/izlibstream.cpp
@@ -18,17 +18,14 @@
* along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
*/
#include "io/izlibstream.h"
-#include "io/zlib_error.h"
+#include "io/zlib_streambuf.h"
namespace zlib
{
inflate_streambuf::inflate_streambuf(std::istream& input, size_t bufsize, int window_bits):
- is(input), in(bufsize), out(bufsize), stream_end(false)
+ zlib_streambuf(bufsize), is(input), stream_end(false)
{
- zstr.zalloc = Z_NULL;
- zstr.zfree = Z_NULL;
- zstr.opaque = Z_NULL;
zstr.next_in = Z_NULL;
zstr.avail_in = 0;
int ret = inflateInit2(&zstr, window_bits);
diff --git a/src/io/ozlibstream.cpp b/src/io/ozlibstream.cpp
index 36c6b42d61..73f1057c67 100644
--- a/src/io/ozlibstream.cpp
+++ b/src/io/ozlibstream.cpp
@@ -18,17 +18,14 @@
* along with libnbt++. If not, see <http://www.gnu.org/licenses/>.
*/
#include "io/ozlibstream.h"
-#include "io/zlib_error.h"
+#include "io/zlib_streambuf.h"
namespace zlib
{
deflate_streambuf::deflate_streambuf(std::ostream& output, size_t bufsize, int level, int window_bits, int mem_level, int strategy):
- os(output), in(bufsize), out(bufsize)
+ zlib_streambuf(bufsize), os(output)
{
- zstr.zalloc = Z_NULL;
- zstr.zfree = Z_NULL;
- zstr.opaque = Z_NULL;
int ret = deflateInit2(&zstr, level, Z_DEFLATED, window_bits, mem_level, strategy);
if(ret != Z_OK)
throw zlib_error(zstr.msg, ret);
diff --git a/test/zlibstream_test.h b/test/zlibstream_test.h
index 5f2ad64c55..f8308cf7c5 100644
--- a/test/zlibstream_test.h
+++ b/test/zlibstream_test.h
@@ -20,7 +20,6 @@
#include <cxxtest/TestSuite.h>
#include "io/izlibstream.h"
#include "io/ozlibstream.h"
-#include "io/zlib_error.h"
#include <fstream>
#include <sstream>