]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/rawdata.cpp
Add a function to detect raw data signature
[libs/datafile.git] / source / rawdata.cpp
index 7a90c9df3870c835a134494975a3b1e5343073d5..b912a56d9b752a29439b00901d195f7b1ef04665 100644 (file)
@@ -1,3 +1,4 @@
+#include <cstring>
 #include <stdexcept>
 #include <msp/io/zlibcompressed.h>
 #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;