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