Add an unpack option to the data tool
authorMikko Rasa <tdb@tdb.fi>
Mon, 7 Sep 2015 09:26:58 +0000 (12:26 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 7 Sep 2015 09:26:58 +0000 (12:26 +0300)
tool/tool.cpp
tool/tool.h

index d58853e..73c49f7 100644 (file)
@@ -2,6 +2,7 @@
 #include <msp/io/buffered.h>
 #include <msp/io/console.h>
 #include <msp/io/file.h>
+#include <msp/datafile/packsource.h>
 #include <msp/datafile/parser.h>
 #include <msp/datafile/statement.h>
 #include "compiler.h"
@@ -18,6 +19,7 @@ DataTool::DataTool(int argc, char **argv):
        float_size(0),
        compress(false),
        pack(false),
+       unpack(false),
        debug(false)
 {
        GetOpt getopt;
@@ -27,21 +29,34 @@ DataTool::DataTool(int argc, char **argv):
        getopt.add_option('g', "debug", debug, GetOpt::NO_ARG).set_help("Display control statements");
        getopt.add_option('o', "output", out_fn, GetOpt::REQUIRED_ARG).set_help("Output to a file instead of stdout", "FILE");
        getopt.add_option('p', "pack", pack, GetOpt::NO_ARG).set_help("Create a pack from multiple files");
+       getopt.add_option('u', "unpack", unpack, GetOpt::NO_ARG).set_help("Unpacks files from packs into the current directory");
        getopt.add_option('z', "compress", compress, GetOpt::NO_ARG).set_help("Produce a compressed datafile");
        getopt.add_argument("infile", in_fns, GetOpt::OPTIONAL_ARG).set_help("Files to process");
        getopt(argc, argv);
 
-       if(in_fns.empty())
-               in_fns.push_back("-");
+       if(compile+pack+unpack>1)
+               throw usage_error("Only one of -c, -p and -u may be specified");
 
        if(pack && out_fn=="-")
                throw usage_error("Can't write pack to stdout");
+
+       if(in_fns.empty())
+               in_fns.push_back("-");
+
+       if(unpack)
+       {
+               for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
+                       if(*i=="-")
+                               throw usage_error("Can't unpack from stdout");
+       }
 }
 
 int DataTool::main()
 {
        if(pack)
                do_pack();
+       else if(unpack)
+               do_unpack();
        else if(compile)
                do_compile();
        else
@@ -100,6 +115,30 @@ void DataTool::do_pack()
        packer.create_pack(out_fn);
 }
 
+void DataTool::do_unpack()
+{
+       DataFile::PackSource source;
+       for(list<string>::const_iterator i=in_fns.begin(); i!=in_fns.end(); ++i)
+               source.add_pack_file(*i);
+
+       list<DataFile::PackSource::FileInfo> files = source.list_files();
+       for(list<DataFile::PackSource::FileInfo>::const_iterator i=files.begin(); i!=files.end(); ++i)
+       {
+               IO::Seekable *in = source.open(i->name);
+               IO::Base *out = open_output(i->name);
+               char buf[16384];
+               while(1)
+               {
+                       unsigned len = in->read(buf, sizeof(buf));
+                       out->write(buf, len);
+                       if(len<sizeof(buf))
+                               break;
+               }
+               delete in;
+               delete out;
+       }
+}
+
 IO::Base *DataTool::open_output(const string &fn)
 {
        if(fn=="-")
index e33c954..88f849b 100644 (file)
@@ -15,6 +15,7 @@ private:
        unsigned float_size;
        bool compress;
        bool pack;
+       bool unpack;
        bool debug;
 
 public:
@@ -25,6 +26,7 @@ private:
        void do_transfer();
        void do_compile();
        void do_pack();
+       void do_unpack();
        Msp::IO::Base *open_output(const std::string &);
        Msp::IO::Base *open_input(const std::string &);
 public: