]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.cpp
Add an API to open files from a collection's sources
[libs/datafile.git] / source / packsource.cpp
1 #include <msp/io/slice.h>
2 #include <msp/strings/format.h>
3 #include <msp/strings/regex.h>
4 #include "collection.h"
5 #include "packsource.h"
6
7 using namespace std;
8
9 namespace {
10
11 void delete_io(Msp::IO::Base *io)
12 {
13         delete io;
14 }
15
16 }
17
18 namespace Msp {
19 namespace DataFile {
20
21 void PackSource::add_pack_file(const string &fn)
22 {
23         add_pack_file(fn, string());
24 }
25
26 void PackSource::add_pack_file(const string &fn, const string &filter)
27 {
28         Pack *pack = 0;
29         for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
30                 if(i->get_filename()==fn)
31                         pack = &*i;
32         if(!pack)
33         {
34                 packs.push_back(Pack(fn));
35                 pack = &packs.back();
36                 DataFile::load(*pack, fn);
37         }
38
39         FileMap pack_files;
40         pack->collect_files(pack_files, filter);
41         files.insert(pack_files.begin(), pack_files.end());
42         for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
43                 i->second->collect_objects(objects);
44 }
45
46 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
47 {
48         ObjectMap::const_iterator i = objects.find(name);
49         if(i==objects.end())
50                 return false;
51
52         // If the object has a keyword, it must match that of the type
53         if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
54                 return false;
55
56         return true;
57 }
58
59 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
60 {
61         NameList names;
62         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
63         {
64                 if(!i->second->get_keyword().empty())
65                 {
66                         if(i->second->get_keyword()!=type.get_keyword())
67                                 continue;
68                 }
69                 else if(!type.match_name(i->first))
70                         continue;
71
72                 names.push_back(i->first);
73         }
74
75         return names;
76 }
77
78 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
79 {
80         ObjectMap::const_iterator i = objects.find(name);
81         if(i==objects.end())
82                 return;
83
84         File &file = i->second->get_file();
85         if(file.is_loaded())
86                 return;
87         file.set_loaded();
88
89         RefPtr<IO::Base> in = file.open();
90         Parser parser(*in, file.get_full_name());
91         if(file.is_collection())
92         {
93                 Collection::Loader ldr(coll);
94                 ldr.load(parser);
95         }
96         else
97                 type.load_item(coll, parser, name);
98 }
99
100 IO::Seekable *PackSource::open(const string &fn) const
101 {
102         FileMap::const_iterator i = files.find(fn);
103         if(i!=files.end())
104                 return i->second->open().release();
105
106         return 0;
107 }
108
109
110 PackSource::Pack::Pack(const string &fn):
111         filename(fn),
112         base_offset(0)
113 { }
114
115 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
116 {
117         if(filter.empty())
118         {
119                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
120                         fm[i->get_filename()] = &*i;
121         }
122         else
123         {
124                 Regex re(filter);
125                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
126                         if(re.match(i->get_filename()))
127                                 fm[i->get_filename()] = &*i;
128         }
129 }
130
131
132 PackSource::File::File(const Pack &p, const string &fn):
133         pack(p),
134         filename(fn),
135         offset(0),
136         length(0),
137         collection(false),
138         loaded(false)
139 { }
140
141 RefPtr<IO::Seekable> PackSource::File::open() const
142 {
143         IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
144         IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
145         io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
146         return io_slice;
147 }
148
149 string PackSource::File::get_full_name() const
150 {
151         return format("%s/%s", pack.get_filename(), filename);
152 }
153
154 void PackSource::File::set_loaded()
155 {
156         loaded = true;
157 }
158
159 void PackSource::File::collect_objects(ObjectMap &objs) const
160 {
161         for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
162                 objs[i->get_name()] = &*i;
163 }
164
165
166 PackSource::Object::Object(File &f, const string &n, const string &k):
167         file(f),
168         name(n),
169         keyword(k)
170 { }
171
172
173 PackSource::Pack::Loader::Loader(Pack &p):
174         ObjectLoader<Pack>(p)
175 {
176         add("file",        &Loader::file);
177         add("base_offset", &Pack::base_offset);
178 }
179
180 void PackSource::Pack::Loader::file(const string &fn)
181 {
182         obj.files.push_back(File(obj, fn));
183         load_sub(obj.files.back());
184 }
185
186
187 PackSource::File::Loader::Loader(File &f):
188         ObjectLoader<File>(f)
189 {
190         add("object", &Loader::object);
191         add("slice",  &File::offset, &File::length);
192 }
193
194 void PackSource::File::Loader::finish()
195 {
196         if(!obj.collection)
197         {
198                 PackSource::Object ob(obj, obj.filename, string());
199                 obj.objects.push_back(ob);
200         }
201 }
202
203 void PackSource::File::Loader::object(const string &name, const string &kwd)
204 {
205         obj.objects.push_back(PackSource::Object(obj, name, kwd));
206         obj.collection = true;
207 }
208
209 } // namespace DataFile
210 } // namespace Msp