]> git.tdb.fi Git - libs/datafile.git/commitdiff
Add support for compressed datafiles
authorMikko Rasa <tdb@tdb.fi>
Fri, 3 Aug 2012 10:06:13 +0000 (13:06 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 3 Aug 2012 10:06:13 +0000 (13:06 +0300)
source/input.cpp
source/input.h
source/output.cpp
source/output.h
source/parser.cpp
source/writer.cpp
source/writer.h
tool/tool.cpp
tool/tool.h

index cc82227281563a195fa3a88c5d6e78ef4c44ffc1..a1d462a770d320f0a13c59175459fc8c131186b4 100644 (file)
@@ -1,19 +1,33 @@
+#include <msp/io/zlibcompressed.h>
 #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
index 9fa4b853a668640736fb4a37f97bf1c71ef23fd3..a9c96f48abce9cdf9e533e640f3913c8849e0d7f 100644 (file)
@@ -9,13 +9,16 @@ namespace DataFile {
 class Input
 {   
 private:
-       IO::Base &in;
+       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; }
index 62886d3d2971191881867092ceb4f371629d4b7c..f2e90640077c9f6ed0f906b0d119322e7468c354 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/io/zlibcompressed.h>
 #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);
index 453260e340a78c46b78eec67b8ec14dd307bef03..13dc829adaf7d3968f5f0ac78b60387e7b5db418 100644 (file)
@@ -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 &);
index 443c3c8c3d72a4d0951e076f260b6262d511c25f..50948404921bd9903031946a8cc7f537ab37d1bc 100644 (file)
@@ -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<string>();
index 60914090f742acaa97b90f1f1f9c58fd03756523..ff4f2248bee7e7d3035c3a4174e44a52251d2450 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/io/zlibcompressed.h>
 #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);
index df308266d976810217be152efec93d8f2a75b77a..18fb3141b2c8c83edb526da13ef76c981b0c4c1e 100644 (file)
@@ -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. */
index 5f9febeedda26d87a749551de4035d7ee6e07a30..b816ee33cd1e8d916d65c2b7d9315a7d8773db70 100644 (file)
@@ -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<string> &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)
index 43c82e121740b01689231f08a4919bb310065b5a..e649909c0f9be925ce599c6b20c46b79b62675e0 100644 (file)
@@ -12,6 +12,7 @@ private:
        bool binary;
        bool compile;
        unsigned float_size;
+       bool compress;
 
 public:
        DataTool(int argc, char **argv);