Add a source class for builtin data
[libs/datafile.git] / source / builtinsource.cpp
1 #include <cstring>
2 #include <msp/io/memory.h>
3 #include "builtinsource.h"
4 #include "collection.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace DataFile {
10
11 void BuiltinSource::add_object(const string &name, const char *content, unsigned size)
12 {
13         objects[name] = Object(content, size);
14 }
15
16 void BuiltinSource::add_object(const string &name, const char *content)
17 {
18         add_object(name, content, strlen(content));
19 }
20
21 bool BuiltinSource::is_loadable(const CollectionItemTypeBase &, const string &name) const
22 {
23         return objects.count(name);
24 }
25
26 CollectionSource::NameList BuiltinSource::get_names(const CollectionItemTypeBase &type) const
27 {
28         NameList names;
29         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
30                 if(type.match_name(i->first))
31                         names.push_back(i->first);
32         return names;
33 }
34
35 void BuiltinSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
36 {
37         ObjectMap::const_iterator i = objects.find(name);
38         if(i!=objects.end())
39         {
40                 IO::Memory in(i->second.data, i->second.size);
41                 Parser parser(in, name);
42                 type.load_item(coll, parser, name);
43         }
44 }
45
46 IO::Seekable *BuiltinSource::open(const string &name) const
47 {
48         ObjectMap::const_iterator i = objects.find(name);
49         if(i!=objects.end())
50                 return new IO::Memory(i->second.data, i->second.size);
51
52         return 0;
53 }
54
55
56 BuiltinSource::Object::Object():
57         data(0),
58         size(0)
59 { }
60
61 BuiltinSource::Object::Object(const char *d, unsigned s):
62         data(d),
63         size(s)
64 { }
65
66 } // namespace DataFile
67 } // namespace Msp