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