From 9fd9af66cf20cdc3d217f273835410eb2c8c362b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 3 Aug 2012 13:06:13 +0300 Subject: [PATCH] Add support for compressed datafiles --- source/input.cpp | 22 ++++++++++++++++++---- source/input.h | 5 ++++- source/output.cpp | 15 ++++++++++++++- source/output.h | 4 ++++ source/parser.cpp | 2 ++ source/writer.cpp | 10 ++++++++++ source/writer.h | 4 ++++ tool/tool.cpp | 6 +++++- tool/tool.h | 1 + 9 files changed, 62 insertions(+), 7 deletions(-) diff --git a/source/input.cpp b/source/input.cpp index cc82227..a1d462a 100644 --- a/source/input.cpp +++ b/source/input.cpp @@ -1,19 +1,33 @@ +#include #include "input.h" namespace Msp { namespace DataFile { Input::Input(IO::Base &i): - in(i), + in(&i), + compressed(0), line(1), next(-1) { } +Input::~Input() +{ + delete compressed; +} + +void Input::set_decompress() +{ + compressed = new IO::ZlibCompressed(*in); + in = compressed; +} + int Input::get() { int c = next; next = -1; - if(c<0) c = in.get(); + if(c<0) + c = in->get(); if(c=='\n') ++line; @@ -24,13 +38,13 @@ int Input::get() int Input::peek() { if(next<0) - next = in.get(); + next = in->get(); return next; } Input::operator bool() const { - return next>=0 || !in.eof(); + return next>=0 || !in->eof(); } } // namespace DataFile diff --git a/source/input.h b/source/input.h index 9fa4b85..a9c96f4 100644 --- a/source/input.h +++ b/source/input.h @@ -9,13 +9,16 @@ namespace DataFile { class Input { private: - IO::Base ∈ + IO::Base *in; + IO::Base *compressed; unsigned line; int next; public: Input(IO::Base &); + ~Input(); + void set_decompress(); int get(); int peek(); unsigned get_line_number() const { return line; } diff --git a/source/output.cpp b/source/output.cpp index 62886d3..f2e9064 100644 --- a/source/output.cpp +++ b/source/output.cpp @@ -1,3 +1,4 @@ +#include #include "output.h" using namespace std; @@ -6,9 +7,21 @@ namespace Msp { namespace DataFile { Output::Output(IO::Base &o): - out(&o) + out(&o), + compressed(0) { } +Output::~Output() +{ + delete compressed; +} + +void Output::set_compressed() +{ + compressed = new IO::ZlibCompressed(*out); + out = compressed; +} + unsigned Output::put(char c) { return out->put(c); diff --git a/source/output.h b/source/output.h index 453260e..13dc829 100644 --- a/source/output.h +++ b/source/output.h @@ -10,9 +10,13 @@ class Output { private: IO::Base *out; + IO::Base *compressed; public: Output(IO::Base &); + ~Output(); + + void set_compressed(); unsigned put(char); unsigned write(const std::string &); diff --git a/source/parser.cpp b/source/parser.cpp index 443c3c8..5094840 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -43,6 +43,8 @@ Statement Parser::parse() delete mode; mode = new TextParser(in, src); } + else if(st.keyword=="__z") + in.set_decompress(); else if(st.keyword=="__src") { string s = st.args[0].get(); diff --git a/source/writer.cpp b/source/writer.cpp index 6091409..ff4f224 100644 --- a/source/writer.cpp +++ b/source/writer.cpp @@ -1,3 +1,4 @@ +#include #include "binarywriter.h" #include "statement.h" #include "textwriter.h" @@ -46,6 +47,15 @@ void Writer::set_binary(bool b) mode = new TextWriter(out); } +void Writer::set_compressed() +{ + Statement st; + st.keyword = "__z"; + mode->write(st); + + out.set_compressed(); +} + void Writer::set_float_precision(unsigned fp) { mode->set_float_precision(fp); diff --git a/source/writer.h b/source/writer.h index df30826..18fb314 100644 --- a/source/writer.h +++ b/source/writer.h @@ -39,6 +39,10 @@ public: */ void set_binary(bool b); + /** Enables output compression. Once enabled, it won't be possible to + disable compression. */ + void set_compressed(); + /** Sets the precision of floating point numbers in bits. Depending on the mode not all values may be valid, but any value between 16 and 64 that is divisible by 8 is guaranteed to work. */ diff --git a/tool/tool.cpp b/tool/tool.cpp index 5f9febe..b816ee3 100644 --- a/tool/tool.cpp +++ b/tool/tool.cpp @@ -16,13 +16,15 @@ DataTool::DataTool(int argc, char **argv): out_fn("-"), binary(false), compile(false), - float_size(0) + float_size(0), + compress(false) { GetOpt getopt; getopt.add_option('b', "binary", binary, GetOpt::NO_ARG); getopt.add_option('c', "compile", compile, GetOpt::NO_ARG); getopt.add_option('f', "float-size", float_size, GetOpt::REQUIRED_ARG); getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG); + getopt.add_option('z', "compress", compress, GetOpt::NO_ARG); getopt(argc, argv); const vector &args = getopt.get_args(); @@ -49,6 +51,8 @@ int DataTool::main() DataFile::Parser parser(in_buf, in_fn); IO::Buffered out_buf(*out); DataFile::Writer writer(out_buf); + if(compress) + writer.set_compressed(); if(binary) writer.set_binary(true); if(float_size) diff --git a/tool/tool.h b/tool/tool.h index 43c82e1..e649909 100644 --- a/tool/tool.h +++ b/tool/tool.h @@ -12,6 +12,7 @@ private: bool binary; bool compile; unsigned float_size; + bool compress; public: DataTool(int argc, char **argv); -- 2.43.0