]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
Give DirectoryCollection the ability to add files as future objects
[libs/datafile.git] / source / collection.cpp
1 #include "collection.h"
2
3 using namespace std;
4
5 namespace Msp {
6 namespace DataFile {
7
8 Collection::~Collection()
9 {
10         for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
11                 delete *i;
12 }
13
14 void Collection::add_future(const std::string &name)
15 {
16         if(items.count(name))
17                 throw key_error(typeid(ItemMap));
18
19         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
20                 if((*i)->match_name(name))
21                 {
22                         items.insert(ItemMap::value_type(name, (*i)->create_future()));
23                         return;
24                 }
25
26         /* XXX throw something?  If we do, DirectoryCollection needs some way to
27         check if a name matches any item type. */
28 }
29
30
31 Collection::Loader::Loader(Collection &c):
32         coll(c)
33 {       
34         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
35                 (*i)->add_to_loader(*this);
36 }
37
38
39 CollectionItemTypeBase::CollectionItemTypeBase():
40         tag(0)
41 { }
42
43 CollectionItemTypeBase::~CollectionItemTypeBase()
44 {
45         delete tag;
46 }
47
48 void CollectionItemTypeBase::set_keyword(const string &k)
49 {
50         kwd = k;
51         if(suffixes.empty())
52                 add_suffix("."+kwd);
53 }
54
55 void CollectionItemTypeBase::add_suffix(const string &s)
56 {
57         suffixes.push_back(s);
58 }
59
60 bool CollectionItemTypeBase::match_name(const string &name) const
61 {
62         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
63                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
64                         return true;
65         return false;
66 }
67
68 } // namespace DataFile
69 } // namespace Msp