Refactor streamcompress.hpp
This commit is contained in:
parent
4f0e636b9b
commit
32f2e2ce81
5 changed files with 50 additions and 46 deletions
|
@ -11,12 +11,14 @@
|
|||
#include "minmax.hpp"
|
||||
#include <cstring>
|
||||
|
||||
std::map<std::string, std::string> stream_compressor_parse_attributes(const std::string& val);
|
||||
namespace streamcompress
|
||||
{
|
||||
std::map<std::string, std::string> parse_attributes(const std::string& val);
|
||||
|
||||
class stream_compressor_base
|
||||
class base
|
||||
{
|
||||
public:
|
||||
virtual ~stream_compressor_base();
|
||||
virtual ~base();
|
||||
/**
|
||||
* Compress data.
|
||||
*
|
||||
|
@ -30,13 +32,13 @@ public:
|
|||
virtual bool process(uint8_t*& in, size_t& insize, uint8_t*& out, size_t& outsize, bool final) = 0;
|
||||
|
||||
static std::set<std::string> get_compressors();
|
||||
static stream_compressor_base* create_compressor(const std::string& name, const std::string& args);
|
||||
static base* create_compressor(const std::string& name, const std::string& args);
|
||||
static void do_register(const std::string& name,
|
||||
std::function<stream_compressor_base*(const std::string&)> ctor);
|
||||
std::function<base*(const std::string&)> ctor);
|
||||
static void do_unregister(const std::string& name);
|
||||
};
|
||||
|
||||
class iostream_compressor
|
||||
class iostream
|
||||
{
|
||||
public:
|
||||
typedef char char_type;
|
||||
|
@ -44,7 +46,7 @@ public:
|
|||
/**
|
||||
* Createa a new compressing stream.
|
||||
*/
|
||||
iostream_compressor(stream_compressor_base* _compressor)
|
||||
iostream(base* _compressor)
|
||||
{
|
||||
compressor = _compressor;
|
||||
inbuf_use = 0;
|
||||
|
@ -103,7 +105,7 @@ public:
|
|||
{
|
||||
}
|
||||
private:
|
||||
stream_compressor_base* compressor;
|
||||
base* compressor;
|
||||
uint8_t inbuffer[4096];
|
||||
uint8_t outbuffer[4096];
|
||||
size_t inbuf_use;
|
||||
|
@ -112,5 +114,6 @@ private:
|
|||
bool oeof_flag;
|
||||
size_t emitted;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
@ -66,17 +66,17 @@ namespace
|
|||
|
||||
void compress(std::vector<char>& buf, std::string& output, std::string& compression)
|
||||
{
|
||||
stream_compressor_base* X = NULL;
|
||||
streamcompress::base* X = NULL;
|
||||
try {
|
||||
if(!X) {
|
||||
X = stream_compressor_base::create_compressor("xz", "level=7,extreme=true");
|
||||
X = streamcompress::base::create_compressor("xz", "level=7,extreme=true");
|
||||
compression = "xz";
|
||||
}
|
||||
} catch(...) {
|
||||
}
|
||||
try {
|
||||
if(!X) {
|
||||
X = stream_compressor_base::create_compressor("gzip", "level=7");
|
||||
X = streamcompress::base::create_compressor("gzip", "level=7");
|
||||
compression = "gzip";
|
||||
}
|
||||
} catch(...) {
|
||||
|
@ -84,7 +84,7 @@ namespace
|
|||
|
||||
std::vector<char> out;
|
||||
boost::iostreams::filtering_istream* s = new boost::iostreams::filtering_istream();
|
||||
if(X) s->push(iostream_compressor(X));
|
||||
if(X) s->push(streamcompress::iostream(X));
|
||||
s->push(boost::iostreams::array_source(&buf[0], buf.size()));
|
||||
boost::iostreams::back_insert_device<std::vector<char>> rd(out);
|
||||
boost::iostreams::copy(*s, rd);
|
||||
|
|
|
@ -24,9 +24,9 @@ namespace
|
|||
free(ptr);
|
||||
}
|
||||
|
||||
struct stream_compressor_gzip : public stream_compressor_base
|
||||
struct gzip : public streamcompress::base
|
||||
{
|
||||
stream_compressor_gzip(unsigned level)
|
||||
gzip(unsigned level)
|
||||
{
|
||||
memset(&strm, 0, sizeof(z_stream));
|
||||
strm.zalloc = zalloc;
|
||||
|
@ -39,7 +39,7 @@ namespace
|
|||
trl = 0;
|
||||
data_output = false;
|
||||
}
|
||||
~stream_compressor_gzip()
|
||||
~gzip()
|
||||
{
|
||||
deflateEnd(&strm);
|
||||
}
|
||||
|
@ -96,16 +96,16 @@ namespace
|
|||
|
||||
struct foo {
|
||||
foo() {
|
||||
stream_compressor_base::do_register("gzip", [](const std::string& v) ->
|
||||
stream_compressor_base* {
|
||||
auto a = stream_compressor_parse_attributes(v);
|
||||
streamcompress::base::do_register("gzip", [](const std::string& v) ->
|
||||
streamcompress::base* {
|
||||
auto a = streamcompress::parse_attributes(v);
|
||||
unsigned compression = 7;
|
||||
if(a.count("level")) compression = parse_value<unsigned>(a["level"]);
|
||||
return new stream_compressor_gzip(compression);
|
||||
return new gzip(compression);
|
||||
});
|
||||
}
|
||||
~foo() {
|
||||
stream_compressor_base::do_unregister("gzip");
|
||||
streamcompress::base::do_unregister("gzip");
|
||||
}
|
||||
} _foo;
|
||||
|
||||
|
@ -143,9 +143,9 @@ namespace
|
|||
int main()
|
||||
{
|
||||
std::vector<char> out;
|
||||
stream_compressor_base* X = stream_compressor_base::create_compressor("gzip", "level=7");
|
||||
streamcompress::base* X = streamcompress::base::create_compressor("gzip", "level=7");
|
||||
boost::iostreams::filtering_istream* s = new boost::iostreams::filtering_istream();
|
||||
s->push(iostream_compressor(X));
|
||||
s->push(streamcompress::iostream(X));
|
||||
s->push(stdin_input());
|
||||
boost::iostreams::back_insert_device<std::vector<char>> rd(out);
|
||||
boost::iostreams::copy(*s, rd);
|
||||
|
|
|
@ -23,9 +23,9 @@ namespace
|
|||
lzma_options_lzma lzmaopts;
|
||||
};
|
||||
|
||||
struct stream_compressor_lzma : public stream_compressor_base
|
||||
struct lzma : public streamcompress::base
|
||||
{
|
||||
stream_compressor_lzma(lzma_options& opts)
|
||||
lzma(lzma_options& opts)
|
||||
{
|
||||
memset(&strm, 0, sizeof(strm));
|
||||
lzma_ret r;
|
||||
|
@ -45,7 +45,7 @@ namespace
|
|||
throw std::runtime_error("Unknown error");
|
||||
}
|
||||
}
|
||||
~stream_compressor_lzma()
|
||||
~lzma()
|
||||
{
|
||||
lzma_end(&strm);
|
||||
}
|
||||
|
@ -79,10 +79,9 @@ namespace
|
|||
|
||||
struct foo {
|
||||
foo() {
|
||||
stream_compressor_base::do_register("lzma", [](const std::string& v) ->
|
||||
stream_compressor_base* {
|
||||
streamcompress::base::do_register("lzma", [](const std::string& v) -> streamcompress::base* {
|
||||
lzma_options opts;
|
||||
auto a = stream_compressor_parse_attributes(v);
|
||||
auto a = streamcompress::parse_attributes(v);
|
||||
unsigned level = 7;
|
||||
bool extreme = false;
|
||||
if(a.count("level")) level = parse_value<unsigned>(a["level"]);
|
||||
|
@ -90,12 +89,11 @@ namespace
|
|||
if(a.count("extreme")) extreme = parse_value<bool>(a["level"]);
|
||||
opts.xz = false;
|
||||
lzma_lzma_preset(&opts.lzmaopts, level | (extreme ? LZMA_PRESET_EXTREME : 0));
|
||||
return new stream_compressor_lzma(opts);
|
||||
return new lzma(opts);
|
||||
});
|
||||
stream_compressor_base::do_register("xz", [](const std::string& v) ->
|
||||
stream_compressor_base* {
|
||||
streamcompress::base::do_register("xz", [](const std::string& v) -> streamcompress::base* {
|
||||
lzma_options opts;
|
||||
auto a = stream_compressor_parse_attributes(v);
|
||||
auto a = streamcompress::parse_attributes(v);
|
||||
unsigned level = 7;
|
||||
bool extreme = false;
|
||||
lzma_options_lzma opt_lzma2;
|
||||
|
@ -110,12 +108,12 @@ namespace
|
|||
opts.fchain = filterchain;
|
||||
opts.check = LZMA_CHECK_CRC64;
|
||||
opts.xz = true;
|
||||
return new stream_compressor_lzma(opts);
|
||||
return new lzma(opts);
|
||||
});
|
||||
}
|
||||
~foo() {
|
||||
stream_compressor_base::do_unregister("lzma");
|
||||
stream_compressor_base::do_unregister("xz");
|
||||
streamcompress::base::do_unregister("lzma");
|
||||
streamcompress::base::do_unregister("xz");
|
||||
}
|
||||
} _foo;
|
||||
|
||||
|
@ -153,9 +151,9 @@ namespace
|
|||
int main()
|
||||
{
|
||||
std::vector<char> out;
|
||||
stream_compressor_base* X = stream_compressor_base::create_compressor("xz", "level=7,extreme=true");
|
||||
streamcompress::base* X = streamcompress::base::create_compressor("xz", "level=7,extreme=true");
|
||||
boost::iostreams::filtering_istream* s = new boost::iostreams::filtering_istream();
|
||||
s->push(iostream_compressor(X));
|
||||
s->push(streamcompress::iostream(X));
|
||||
s->push(stdin_input());
|
||||
boost::iostreams::back_insert_device<std::vector<char>> rd(out);
|
||||
boost::iostreams::copy(*s, rd);
|
||||
|
|
|
@ -2,20 +2,22 @@
|
|||
#include "string.hpp"
|
||||
#include <stdexcept>
|
||||
|
||||
namespace streamcompress
|
||||
{
|
||||
namespace
|
||||
{
|
||||
std::map<std::string, std::function<stream_compressor_base*(const std::string&)>>& compressors()
|
||||
std::map<std::string, std::function<base*(const std::string&)>>& compressors()
|
||||
{
|
||||
static std::map<std::string, std::function<stream_compressor_base*(const std::string&)>> x;
|
||||
static std::map<std::string, std::function<base*(const std::string&)>> x;
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
stream_compressor_base::~stream_compressor_base()
|
||||
base::~base()
|
||||
{
|
||||
}
|
||||
|
||||
std::set<std::string> stream_compressor_base::get_compressors()
|
||||
std::set<std::string> base::get_compressors()
|
||||
{
|
||||
std::set<std::string> r;
|
||||
for(auto& i : compressors())
|
||||
|
@ -23,7 +25,7 @@ std::set<std::string> stream_compressor_base::get_compressors()
|
|||
return r;
|
||||
}
|
||||
|
||||
stream_compressor_base* stream_compressor_base::create_compressor(const std::string& name,
|
||||
base* base::create_compressor(const std::string& name,
|
||||
const std::string& args)
|
||||
{
|
||||
if(!compressors().count(name))
|
||||
|
@ -31,18 +33,18 @@ stream_compressor_base* stream_compressor_base::create_compressor(const std::str
|
|||
compressors()[name](args);
|
||||
}
|
||||
|
||||
void stream_compressor_base::do_register(const std::string& name,
|
||||
std::function<stream_compressor_base*(const std::string&)> ctor)
|
||||
void base::do_register(const std::string& name,
|
||||
std::function<base*(const std::string&)> ctor)
|
||||
{
|
||||
compressors()[name] = ctor;
|
||||
}
|
||||
|
||||
void stream_compressor_base::do_unregister(const std::string& name)
|
||||
void base::do_unregister(const std::string& name)
|
||||
{
|
||||
compressors().erase(name);
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> stream_compressor_parse_attributes(const std::string& val)
|
||||
std::map<std::string, std::string> parse_attributes(const std::string& val)
|
||||
{
|
||||
std::map<std::string, std::string> r;
|
||||
std::string v = val;
|
||||
|
@ -53,3 +55,4 @@ std::map<std::string, std::string> stream_compressor_parse_attributes(const std:
|
|||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue