From 8a412367448c55916380ce7040ddbd0a75fa722e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 2 Nov 2021 11:57:28 +0200 Subject: [PATCH] Add a function to detect raw data signature --- source/rawdata.cpp | 10 +++++++++- source/rawdata.h | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/source/rawdata.cpp b/source/rawdata.cpp index 7a90c9d..b912a56 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; diff --git a/source/rawdata.h b/source/rawdata.h index 2db390b..2744744 100644 --- a/source/rawdata.h +++ b/source/rawdata.h @@ -26,9 +26,13 @@ private: char *data = nullptr; char *owned_data = nullptr; + static const char signature[4]; + public: ~RawData(); + static bool detect_signature(const std::string &); + void open_file(Collection &, const std::string &); void open_io(IO::Base &, const std::string &); void load(); -- 2.45.2