From: Mikko Rasa Date: Tue, 15 Nov 2016 15:57:21 +0000 (+0200) Subject: Add a source class for builtin data X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=1133d979d230f7899cf24a4250ac006cbf91527b;p=libs%2Fdatafile.git Add a source class for builtin data --- diff --git a/source/builtinsource.cpp b/source/builtinsource.cpp new file mode 100644 index 0000000..bdccd77 --- /dev/null +++ b/source/builtinsource.cpp @@ -0,0 +1,67 @@ +#include +#include +#include "builtinsource.h" +#include "collection.h" + +using namespace std; + +namespace Msp { +namespace DataFile { + +void BuiltinSource::add_object(const string &name, const char *content, unsigned size) +{ + objects[name] = Object(content, size); +} + +void BuiltinSource::add_object(const string &name, const char *content) +{ + add_object(name, content, strlen(content)); +} + +bool BuiltinSource::is_loadable(const CollectionItemTypeBase &, const string &name) const +{ + return objects.count(name); +} + +CollectionSource::NameList BuiltinSource::get_names(const CollectionItemTypeBase &type) const +{ + NameList names; + for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + if(type.match_name(i->first)) + names.push_back(i->first); + return names; +} + +void BuiltinSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const +{ + ObjectMap::const_iterator i = objects.find(name); + if(i!=objects.end()) + { + IO::Memory in(i->second.data, i->second.size); + Parser parser(in, name); + type.load_item(coll, parser, name); + } +} + +IO::Seekable *BuiltinSource::open(const string &name) const +{ + ObjectMap::const_iterator i = objects.find(name); + if(i!=objects.end()) + return new IO::Memory(i->second.data, i->second.size); + + return 0; +} + + +BuiltinSource::Object::Object(): + data(0), + size(0) +{ } + +BuiltinSource::Object::Object(const char *d, unsigned s): + data(d), + size(s) +{ } + +} // namespace DataFile +} // namespace Msp diff --git a/source/builtinsource.h b/source/builtinsource.h new file mode 100644 index 0000000..244191c --- /dev/null +++ b/source/builtinsource.h @@ -0,0 +1,38 @@ +#ifndef MSP_DATAFILE_BUILTINSOURCE_H_ +#define MSP_DATAFILE_BUILTINSOURCE_H_ + +#include "collectionsource.h" + +namespace Msp { +namespace DataFile { + +class BuiltinSource: public CollectionSource +{ +private: + struct Object + { + const char *data; + unsigned size; + + Object(); + Object(const char *, unsigned); + }; + + typedef std::map ObjectMap; + + ObjectMap objects; + +public: + void add_object(const std::string &, const char *, unsigned); + void add_object(const std::string &, const char *); + + virtual bool is_loadable(const CollectionItemTypeBase &, const std::string &) const; + virtual NameList get_names(const CollectionItemTypeBase &) const; + virtual void load(Collection &, const CollectionItemTypeBase &, const std::string &) const; + virtual IO::Seekable *open(const std::string &) const; +}; + +} // namespace DataFile +} // namespace Msp + +#endif