diff options
Diffstat (limited to 'include')
| -rw-r--r-- | include/io/izlibstream.h | 7 | ||||
| -rw-r--r-- | include/io/ozlibstream.h | 8 | ||||
| -rw-r--r-- | include/io/zlib_error.h | 21 | ||||
| -rw-r--r-- | include/io/zlib_streambuf.h | 45 |
4 files changed, 50 insertions, 31 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 |
