X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Frawdata.cpp;h=de4d7c22bb6e9d1a195174451a08e4817f1b2c75;hb=83971ed7f55794a61e1d249c59867184a6eb97ba;hp=7a90c9df3870c835a134494975a3b1e5343073d5;hpb=c80b23de8b70776e37f37e4b8fc2003c553448d6;p=libs%2Fdatafile.git diff --git a/source/rawdata.cpp b/source/rawdata.cpp index 7a90c9d..de4d7c2 100644 --- a/source/rawdata.cpp +++ b/source/rawdata.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "collection.h" @@ -9,6 +10,8 @@ using namespace std; namespace Msp { namespace DataFile { +const char RawData::signature[4] = { 'M', 'D', 'R', 1 }; + RawData::~RawData() { delete owned_data; @@ -17,6 +20,11 @@ RawData::~RawData() delete in; } +bool RawData::detect_signature(const std::string &sig) +{ + return !sig.compare(0, string::npos, signature, sizeof(signature)); +} + void RawData::open_file(Collection &coll, const string &fn) { if(in) @@ -40,7 +48,7 @@ void RawData::open_io(IO::Base &i, const string &fn) unsigned len = i.read(header, sizeof(header)); if(len!=sizeof(header)) throw data_error(fn, 0, "Missing header"); - if(header[0]!='M' || header[1]!='D' || header[2]!='R' || header[3]!=1) + if(memcmp(header, signature, sizeof(signature))) throw data_error(fn, 0, "Bad header"); size = 0; @@ -88,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