]> git.tdb.fi Git - libs/datafile.git/commitdiff
Recognize and handle raw data files in the data tool
authorMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 14:38:24 +0000 (16:38 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 14:39:06 +0000 (16:39 +0200)
source/rawdata.cpp
source/rawdata.h
tool/packer.cpp
tool/packer.h
tool/tool.h

index b912a56d9b752a29439b00901d195f7b1ef04665..de4d7c22bb6e9d1a195174451a08e4817f1b2c75 100644 (file)
@@ -96,5 +96,30 @@ void RawData::load_into(void *buffer)
        in = nullptr;
 }
 
+void RawData::write_io(IO::Base &io, bool compress)
+{
+       if(!data)
+               throw logic_error("no data");
+
+       io.write(signature, sizeof(signature));
+
+       for(unsigned i=56; i<64; i-=8)
+               io.put((size>>i)&0xFF);
+
+       uint16_t flags = 0;
+       if(compress)
+               flags |= COMPRESSED;
+       io.put((flags>>8)&0xFF);
+       io.put(flags&0xFF);
+
+       if(compress)
+       {
+               IO::ZlibCompressed z(io, IO::M_WRITE);
+               z.write(data, size);
+       }
+       else
+               io.write(data, size);
+}
+
 } // namespace DataFile
 } // namespace Msp
index 2744744b36cb2b3ead8f4ca13128cfc4f056df35..ad265575db30fbdd4bea5fd20d66f4e242570d0b 100644 (file)
@@ -38,6 +38,8 @@ public:
        void load();
        void load_into(void *);
 
+       void write_io(IO::Base &, bool = false);
+
        std::size_t get_size() const { return size; }
        const void *get_data() const { return data; }
 };
index fa4d7e236a943ba3361507cb5ff0b2bcf559415a..2e773a21a04a12dd8139bda0bb1c7acee20d0b15 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/datafile/parser.h>
+#include <msp/datafile/rawdata.h>
 #include <msp/datafile/statement.h>
 #include <msp/datafile/writer.h>
 #include <msp/fs/utils.h>
@@ -57,6 +58,8 @@ void Packer::pack_file(const string &fn)
        ObjectList objects;
        if(detect_data(in))
                transfer_datafile(in, fn, *tmp_file, objects);
+       else if(detect_raw(in))
+               transfer_raw_data(in, fn, *tmp_file);
        else
                transfer_unknown(in, *tmp_file);
 
@@ -91,6 +94,15 @@ bool Packer::detect_data(IO::Seekable &in)
        return data;
 }
 
+bool Packer::detect_raw(IO::Seekable &in)
+{
+       char header[4] = { };
+       IO::SeekOffset offset = in.tell();
+       in.read(header, sizeof(header));
+       in.seek(offset, IO::S_BEG);
+       return DataFile::RawData::detect_signature(string(header, sizeof(header)));
+}
+
 void Packer::transfer_datafile(IO::Base &in, const string &fn, IO::Base &out, ObjectList &objects)
 {
        DataFile::Parser parser(in, fn);
@@ -123,6 +135,14 @@ void Packer::transfer_datafile(IO::Base &in, const string &fn, IO::Base &out, Ob
        delete writer;
 }
 
+void Packer::transfer_raw_data(IO::Base &in, const string &fn, IO::Base &out)
+{
+       DataFile::RawData raw;
+       raw.open_io(in, fn);
+       raw.load();
+       raw.write_io(out, tool.is_compressed());
+}
+
 void Packer::transfer_unknown(IO::Base &in, IO::Base &out)
 {
        while(!in.eof())
index 151947fbba945bd683017cf1389ff5e467278c3d..313ee033216969b9d8931435098848e26d6c9b54 100644 (file)
@@ -34,7 +34,9 @@ public:
        void pack_file(const std::string &);
 private:
        bool detect_data(Msp::IO::Seekable &);
+       bool detect_raw(Msp::IO::Seekable &);
        void transfer_datafile(Msp::IO::Base &, const std::string &, Msp::IO::Base &, ObjectList &);
+       void transfer_raw_data(Msp::IO::Base &, const std::string &, Msp::IO::Base &);
        void transfer_unknown(Msp::IO::Base &, Msp::IO::Base &);
 public:
        void create_pack(const std::string &);
index 0333c0d8845594e51e11a25efe23515cff10cd6f..354456ccab4a51276a3176c1a99be92cef0f00f2 100644 (file)
@@ -34,6 +34,7 @@ private:
        Msp::IO::Base *open_output(const std::string &);
        Msp::IO::Base *open_input(const std::string &);
 public:
+       bool is_compressed() const { return compress; }
        Msp::DataFile::Writer *create_writer(Msp::IO::Base &);
 };