]> git.tdb.fi Git - libs/datafile.git/blob - source/collection.cpp
3e9e4f18d7c676e1ee3008c858e14d4735568e2a
[libs/datafile.git] / source / collection.cpp
1 #include <set>
2 #include "collection.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace DataFile {
8
9 Collection::Collection():
10         fallback(0)
11 { }
12
13 Collection::~Collection()
14 {
15         for(TypeList::iterator i = types.begin(); i!=types.end(); ++i)
16                 delete *i;
17 }
18
19 const Variant &Collection::get_var(const string &name, const CollectionItemTypeBase *type)
20 {
21         ItemMap::iterator i = items.find(name);
22         if(i!=items.end())
23                 return i->second;
24
25         if(type)
26         {
27                 bool loaded = false;
28                 if(type->can_create())
29                 {
30                         type->create_item(*this, name);
31                         loaded = items.count(name);
32                 }
33                 for(SourceList::iterator j=sources.begin(); (!loaded && j!=sources.end()); ++j)
34                 {
35                         (*j)->load(*this, *type, name);
36                         loaded = items.count(name);
37                 }
38                 if(!loaded && fallback)
39                         if(CollectionItemTypeBase *fb_type = fallback->get_type(*type))
40                                 if(fallback->get_status(name, *fb_type))
41                                         return fallback->get_var(name, fb_type);
42         }
43
44         return get_item(items, name);
45 }
46
47 void Collection::gather_items(list<const Variant *> *vars, list<string> *names, const CollectionItemTypeBase &type, bool include_sources) const
48 {
49         for(ItemMap::const_iterator i=items.begin(); i!=items.end(); ++i)
50                 if(type.check_item_type(i->second))
51                 {
52                         if(vars)
53                                 vars->push_back(&i->second);
54                         if(names)
55                                 names->push_back(i->first);
56                 }
57
58         if(include_sources && names)
59                 gather_names_from_sources(*names, type);
60 }
61
62 unsigned Collection::get_status(const string &name, const CollectionItemTypeBase &type) const
63 {
64         ItemMap::const_iterator i = items.find(name);
65         if(i==items.end())
66         {
67                 for(SourceList::const_iterator j=sources.begin(); j!=sources.end(); ++j)
68                         if((*j)->is_loadable(type, name))
69                                 return 2;
70                 if(fallback)
71                         if(CollectionItemTypeBase *fb_type = fallback->get_type(type))
72                                 return fallback->get_status(name, *fb_type);
73                 return 0;
74         }
75
76         return type.check_item_type(i->second);
77 }
78
79 CollectionItemTypeBase *Collection::get_type(const CollectionItemTypeBase &type) const
80 {
81         for(TypeList::const_iterator j=types.begin(); j!=types.end(); ++j)
82                 if((*j)->is_same_type(type))
83                         return *j;
84         return 0;
85 }
86
87 CollectionItemTypeBase *Collection::get_type_for_item(const Variant &var) const
88 {
89         for(TypeList::const_iterator i=types.begin(); i!=types.end(); ++i)
90                 if((*i)->check_item_type(var))
91                         return *i;
92         return 0;
93 }
94
95 void Collection::add_source(CollectionSource &s)
96 {
97         sources.push_back(&s);
98 }
99
100 IO::Seekable *Collection::open_raw(const string &name) const
101 {
102         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
103                 if(IO::Seekable *io = (*i)->open(name))
104                         return io;
105
106         return 0;
107 }
108
109 void Collection::gather_names_from_sources(list<string> &names, const CollectionItemTypeBase &type) const
110 {
111         set<string> new_names;
112         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
113         {
114                 std::list<std::string> available_names = (*i)->get_names(type);
115                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
116                         if(!items.count(*j))
117                                 new_names.insert(*j);
118         }
119         names.insert(names.end(), new_names.begin(), new_names.end());
120 }
121
122 void Collection::load_items_from_sources(const CollectionItemTypeBase &type)
123 {
124         for(SourceList::const_iterator i=sources.begin(); i!=sources.end(); ++i)
125         {
126                 std::list<std::string> available_names = (*i)->get_names(type);
127                 for(std::list<std::string>::iterator j=available_names.begin(); j!=available_names.end(); ++j)
128                         if(!items.count(*j))
129                         {
130                                 bool loaded = false;
131                                 if(type.can_create())
132                                 {
133                                         type.create_item(*this, *j);
134                                         loaded = items.count(*j);
135                                 }
136                                 if(!loaded)
137                                         (*i)->load(*this, type, *j);
138                         }
139         }
140 }
141
142 void Collection::set_fallback(Collection *f)
143 {
144         fallback = f;
145 }
146
147
148 Collection::Loader::Loader(Collection &c):
149         coll(c)
150 {       
151         for(TypeList::const_iterator i = coll.types.begin(); i!=coll.types.end(); ++i)
152                 (*i)->add_to_loader(*this);
153 }
154
155
156 CollectionItemTypeBase::~CollectionItemTypeBase()
157 {
158         for(vector<ExtractorBase *>::iterator i=extractors.begin(); i!=extractors.end(); ++i)
159                 delete *i;
160 }
161
162 void CollectionItemTypeBase::set_keyword(const string &k)
163 {
164         kwd = k;
165         if(suffixes.empty())
166                 add_suffix("."+kwd);
167 }
168
169 void CollectionItemTypeBase::add_suffix(const string &s)
170 {
171         suffixes.push_back(s);
172 }
173
174 bool CollectionItemTypeBase::match_name(const string &name) const
175 {
176         for(vector<string>::const_iterator i=suffixes.begin(); i!=suffixes.end(); ++i)
177                 if(name.size()>i->size() && !name.compare(name.size()-i->size(), string::npos, *i))
178                         return true;
179         return false;
180 }
181
182 } // namespace DataFile
183 } // namespace Msp