]> git.tdb.fi Git - libs/datafile.git/commitdiff
Add a source class for builtin data
authorMikko Rasa <tdb@tdb.fi>
Tue, 15 Nov 2016 15:57:21 +0000 (17:57 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 15 Nov 2016 15:57:21 +0000 (17:57 +0200)
source/builtinsource.cpp [new file with mode: 0644]
source/builtinsource.h [new file with mode: 0644]

diff --git a/source/builtinsource.cpp b/source/builtinsource.cpp
new file mode 100644 (file)
index 0000000..bdccd77
--- /dev/null
@@ -0,0 +1,67 @@
+#include <cstring>
+#include <msp/io/memory.h>
+#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 (file)
index 0000000..244191c
--- /dev/null
@@ -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<std::string, Object> 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