]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
fbe894a8bb4b073c0baa227655bd2d3ecf3781e3
[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 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
15 {
16         ItemMap::iterator i = items.find(name);
17         if(i!=items.end())
18                 return i->second;
19
20         if(type)
21         {
22                 bool loaded = false;
23                 if(type->can_create())
24                 {
25                         type->create_item(*this, name);
26                         loaded = items.count(name);
27                 }
28                 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
29                 {
30                         (*j)->load(*this, *type, name);
31                         loaded = items.count(name);
32                 }
33         }
34
35         return get_item(items, name);
36 }
37
38 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
39 {
40         for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
41                 if(type.check_item_type(i->second))
42                 {
43                         if(vars)
44                                 vars->push_back(&i->second);
45                         if(names)
46                                 names->push_back(i->first);
47                 }
48
49         if(include_sources && names)
50                 gather_names_from_sources(*names, type);
51 }
52
53 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
54 {
55         ItemMap::const_iterator i = items.find(name);
56         if(i==items.end())
57         {
58                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
59                         if((*j)->is_loadable(type, name))
60                                 return 2;
61                 return 0;
62         }
63
64         return type.check_item_type(i->second);
65 }
66
67 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
68 {
69         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
70                 if((*i)->check_item_type(var))
71                         return *i;
72         return 0;
73 }
74
75 void Collection::add_source(CollectionSource &s)
76 {
77         sources.push_back(&s);
78 }
79
80 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
81 {
82         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
83         {
84                 std::list<std::string> available_names = (*i)->get_names(type);
85                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
86                         if(!items.count(*j))
87                                 names.push_back(*j);
88         }
89 }
90
91 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
92 {
93         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
94         {
95                 std::list<std::string> available_names = (*i)->get_names(type);
96                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
97                         if(!items.count(*j))
98                                 (*i)->load(*this, type, *j);
99         }
100 }
101
102
103 Collection::Loader::Loader(Collection &c):
104         coll(c)
105 {       
106         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
107                 (*i)->add_to_loader(*this);
108 }
109
110
111 CollectionItemTypeBase::~CollectionItemTypeBase()
112 {
113         for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
114                 delete *i;
115 }
116
117 void CollectionItemTypeBase::set_keyword(const string &k)
118 {
119         kwd = k;
120         if(suffixes.empty())
121                 add_suffix("."+kwd);
122 }
123
124 void CollectionItemTypeBase::add_suffix(const string &s)
125 {
126         suffixes.push_back(s);
127 }
128
129 bool CollectionItemTypeBase::match_name(const string &name) const
130 {
131         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
132                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
133                         return true;
134         return false;
135 }
136
137 } // namespace DataFile
138 } // namespace Msp