]> git.tdb.fi Git - libs/datafile.git/blob - source/packsource.cpp
Implement proper copy semantics
[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 PackSource::PackSource(const PackSource &other):
22         packs(other.packs),
23         files(other.files),
24         objects(other.objects)
25 {
26         translate_maps(other);
27 }
28
29 PackSource &PackSource::operator=(const PackSource &other)
30 {
31         packs = list<Pack>(other.packs.begin(), other.packs.end());
32         files = other.files;
33         objects = other.objects;
34         translate_maps(other);
35         return *this;
36 }
37
38 void PackSource::translate_maps(const PackSource &other)
39 {
40         for(list<Pack>::const_iterator i=packs.begin(), j=other.packs.begin(); (i!=packs.end() && j!=other.packs.end()); ++i, ++j)
41                 i->translate_files(files, *j);
42         for(FileMap::const_iterator i=files.begin(), j=other.files.begin(); (i!=files.end() && j!=other.files.end()); ++i, ++j)
43                 i->second->translate_objects(objects, *j->second);
44 }
45
46 void PackSource::add_pack_file(const string &fn)
47 {
48         add_pack_file(fn, string());
49 }
50
51 void PackSource::add_pack_file(const string &fn, const string &filter)
52 {
53         add_pack(0, fn, filter);
54 }
55
56 void PackSource::add_pack_io(IO::Seekable &io, const string &fn)
57 {
58         add_pack(&io, fn, string());
59 }
60
61 void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter)
62 {
63         add_pack(&io, fn, filter);
64 }
65
66 void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter)
67 {
68         Pack *pack = 0;
69         for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
70                 if(i->get_filename()==fn || (io && i->get_io()==io))
71                         pack = &*i;
72         if(!pack)
73         {
74                 packs.push_back(Pack(io, fn));
75                 pack = &packs.back();
76                 if(io)
77                 {
78                         DataFile::Parser parser(*io, fn);
79                         Pack::Loader loader(*pack);
80                         loader.load(parser);
81                 }
82                 else
83                         DataFile::load(*pack, fn);
84         }
85
86         FileMap pack_files;
87         pack->collect_files(pack_files, filter);
88         for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
89         {
90                 files[i->first] = i->second;
91                 i->second->collect_objects(objects);
92         }
93 }
94
95 list<PackSource::FileInfo> PackSource::list_files() const
96 {
97         list<FileInfo> result;
98         for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i)
99                 result.push_back(i->second->get_info());
100         return result;
101 }
102
103 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
104 {
105         ObjectMap::const_iterator i = objects.find(name);
106         if(i==objects.end())
107                 return false;
108
109         // If the object has a keyword, it must match that of the type
110         if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
111                 return false;
112
113         return true;
114 }
115
116 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
117 {
118         NameList names;
119         for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
120         {
121                 if(!i->second->get_keyword().empty())
122                 {
123                         if(i->second->get_keyword()!=type.get_keyword())
124                                 continue;
125                 }
126                 else if(!type.match_name(i->first))
127                         continue;
128
129                 names.push_back(i->first);
130         }
131
132         return names;
133 }
134
135 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
136 {
137         ObjectMap::const_iterator i = objects.find(name);
138         if(i==objects.end())
139                 return;
140
141         const File &file = i->second->get_file();
142
143         RefPtr<IO::Base> in = file.open();
144         Parser parser(*in, file.get_full_name());
145         if(file.is_collection())
146         {
147                 Collection::Loader ldr(coll);
148                 ldr.load(parser);
149         }
150         else
151                 type.load_item(coll, parser, name);
152 }
153
154 IO::Seekable *PackSource::open(const string &fn) const
155 {
156         FileMap::const_iterator i = files.find(fn);
157         if(i!=files.end())
158                 return i->second->open().release();
159
160         return 0;
161 }
162
163
164 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
165         filename(fn),
166         io(i),
167         base_offset(0)
168 { }
169
170 PackSource::Pack::Pack(const Pack &other):
171         filename(other.filename),
172         io(other.io),
173         base_offset(other.base_offset)
174 {
175         for(list<File>::const_iterator i=other.files.begin(); i!=other.files.end(); ++i)
176                 files.push_back(File(*i, *this));
177 }
178
179 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
180 {
181         if(filter.empty())
182         {
183                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
184                         fm[i->get_filename()] = &*i;
185         }
186         else
187         {
188                 Regex re(filter);
189                 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
190                         if(re.match(i->get_filename()))
191                                 fm[i->get_filename()] = &*i;
192         }
193 }
194
195 void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
196 {
197         for(list<File>::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
198         {
199                 FileMap::iterator k = fm.find(i->get_filename());
200                 if(k!=fm.end() && k->second==&*j)
201                         k->second = &*i;
202         }
203 }
204
205
206 PackSource::File::File(const Pack &p, const string &fn):
207         pack(p),
208         filename(fn),
209         offset(0),
210         length(0),
211         collection(false)
212 { }
213
214 PackSource::File::File(const File &other, const Pack &p):
215         pack(p),
216         filename(other.filename),
217         offset(other.offset),
218         length(other.length),
219         collection(other.collection)
220 {
221         for(list<Object>::const_iterator i=other.objects.begin(); i!=other.objects.end(); ++i)
222                 objects.push_back(Object(*i, *this));
223 }
224
225 RefPtr<IO::Seekable> PackSource::File::open() const
226 {
227         if(pack.get_io())
228                 // TODO Performance may be poor without buffering
229                 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
230         else
231         {
232                 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
233                 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
234                 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
235                 return io_slice;
236         }
237 }
238
239 PackSource::FileInfo PackSource::File::get_info() const
240 {
241         FileInfo info;
242         info.name = filename;
243         info.size = length;
244         return info;
245 }
246
247 string PackSource::File::get_full_name() const
248 {
249         return format("%s/%s", pack.get_filename(), filename);
250 }
251
252 void PackSource::File::collect_objects(ObjectMap &objs) const
253 {
254         for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
255                 objs[i->get_name()] = &*i;
256 }
257
258 void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
259 {
260         for(list<Object>::const_iterator i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
261         {
262                 ObjectMap::iterator k = objs.find(i->get_name());
263                 if(k!=objs.end() && k->second==&*j)
264                         k->second = &*i;
265         }
266 }
267
268
269 PackSource::Object::Object(const File &f, const string &n, const string &k):
270         file(f),
271         name(n),
272         keyword(k)
273 { }
274
275 PackSource::Object::Object(const Object &other, const File &f):
276         file(f),
277         name(other.name),
278         keyword(other.keyword)
279 { }
280
281
282 PackSource::Pack::Loader::Loader(Pack &p):
283         ObjectLoader<Pack>(p)
284 {
285         add("file",        &Loader::file);
286         add("base_offset", &Pack::base_offset);
287 }
288
289 void PackSource::Pack::Loader::file(const string &fn)
290 {
291         obj.files.push_back(File(obj, fn));
292         load_sub(obj.files.back());
293 }
294
295
296 PackSource::File::Loader::Loader(File &f):
297         ObjectLoader<File>(f)
298 {
299         add("object", &Loader::object);
300         add("slice",  &File::offset, &File::length);
301 }
302
303 void PackSource::File::Loader::finish()
304 {
305         if(!obj.collection)
306         {
307                 PackSource::Object ob(obj, obj.filename, string());
308                 obj.objects.push_back(ob);
309         }
310 }
311
312 void PackSource::File::Loader::object(const string &name, const string &kwd)
313 {
314         obj.objects.push_back(PackSource::Object(obj, name, kwd));
315         obj.collection = true;
316 }
317
318 } // namespace DataFile
319 } // namespace Msp