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"
11 void delete_io(Msp::IO::Base *io)
21 PackSource::PackSource(const PackSource &other):
24 objects(other.objects)
26 translate_maps(other);
29 PackSource &PackSource::operator=(const PackSource &other)
31 packs = list<Pack>(other.packs.begin(), other.packs.end());
33 objects = other.objects;
34 translate_maps(other);
38 void PackSource::translate_maps(const PackSource &other)
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);
46 void PackSource::add_pack_file(const string &fn)
48 add_pack_file(fn, string());
51 void PackSource::add_pack_file(const string &fn, const string &filter)
53 add_pack(0, fn, filter);
56 void PackSource::add_pack_io(IO::Seekable &io, const string &fn)
58 add_pack(&io, fn, string());
61 void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter)
63 add_pack(&io, fn, filter);
66 void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter)
69 for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
70 if(i->get_filename()==fn || (io && i->get_io()==io))
74 packs.push_back(Pack(io, fn));
78 DataFile::Parser parser(*io, fn);
79 Pack::Loader loader(*pack);
83 DataFile::load(*pack, fn);
87 pack->collect_files(pack_files, filter);
88 for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
90 files[i->first] = i->second;
91 i->second->collect_objects(objects);
95 list<PackSource::FileInfo> PackSource::list_files() const
97 list<FileInfo> result;
98 for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i)
99 result.push_back(i->second->get_info());
103 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
105 ObjectMap::const_iterator i = objects.find(name);
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())
116 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
119 for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
121 if(!i->second->get_keyword().empty())
123 if(i->second->get_keyword()!=type.get_keyword())
126 else if(!type.match_name(i->first))
129 names.push_back(i->first);
135 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
137 ObjectMap::const_iterator i = objects.find(name);
141 const File &file = i->second->get_file();
143 RefPtr<IO::Base> in = file.open();
144 Parser parser(*in, file.get_full_name());
145 if(file.is_collection())
147 Collection::Loader ldr(coll);
151 type.load_item(coll, parser, name);
154 IO::Seekable *PackSource::open(const string &fn) const
156 FileMap::const_iterator i = files.find(fn);
158 return i->second->open().release();
164 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
170 PackSource::Pack::Pack(const Pack &other):
171 filename(other.filename),
173 base_offset(other.base_offset)
175 for(list<File>::const_iterator i=other.files.begin(); i!=other.files.end(); ++i)
176 files.push_back(File(*i, *this));
179 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
183 for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
184 fm[i->get_filename()] = &*i;
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;
195 void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
197 for(list<File>::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
199 FileMap::iterator k = fm.find(i->get_filename());
200 if(k!=fm.end() && k->second==&*j)
206 PackSource::File::File(const Pack &p, const string &fn):
214 PackSource::File::File(const File &other, const Pack &p):
216 filename(other.filename),
217 offset(other.offset),
218 length(other.length),
219 collection(other.collection)
221 for(list<Object>::const_iterator i=other.objects.begin(); i!=other.objects.end(); ++i)
222 objects.push_back(Object(*i, *this));
225 RefPtr<IO::Seekable> PackSource::File::open() const
228 // TODO Performance may be poor without buffering
229 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
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));
239 PackSource::FileInfo PackSource::File::get_info() const
242 info.name = filename;
247 string PackSource::File::get_full_name() const
249 return format("%s/%s", pack.get_filename(), filename);
252 void PackSource::File::collect_objects(ObjectMap &objs) const
254 for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
255 objs[i->get_name()] = &*i;
258 void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
260 for(list<Object>::const_iterator i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
262 ObjectMap::iterator k = objs.find(i->get_name());
263 if(k!=objs.end() && k->second==&*j)
269 PackSource::Object::Object(const File &f, const string &n, const string &k):
275 PackSource::Object::Object(const Object &other, const File &f):
278 keyword(other.keyword)
282 PackSource::Pack::Loader::Loader(Pack &p):
283 ObjectLoader<Pack>(p)
285 add("file", &Loader::file);
286 add("base_offset", &Pack::base_offset);
289 void PackSource::Pack::Loader::file(const string &fn)
291 obj.files.push_back(File(obj, fn));
292 load_sub(obj.files.back());
296 PackSource::File::Loader::Loader(File &f):
297 ObjectLoader<File>(f)
299 add("object", &Loader::object);
300 add("slice", &File::offset, &File::length);
303 void PackSource::File::Loader::finish()
307 PackSource::Object ob(obj, obj.filename, string());
308 obj.objects.push_back(ob);
312 void PackSource::File::Loader::object(const string &name, const string &kwd)
314 obj.objects.push_back(PackSource::Object(obj, name, kwd));
315 obj.collection = true;
318 } // namespace DataFile