1 #include <msp/core/algorithm.h>
2 #include <msp/io/slice.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/regex.h>
5 #include "collection.h"
6 #include "packsource.h"
12 void delete_io(Msp::IO::Base *io)
22 PackSource::PackSource(const PackSource &other):
25 objects(other.objects)
27 translate_maps(other);
30 PackSource &PackSource::operator=(const PackSource &other)
32 packs = list<Pack>(other.packs.begin(), other.packs.end());
34 objects = other.objects;
35 translate_maps(other);
39 void PackSource::translate_maps(const PackSource &other)
41 for(list<Pack>::const_iterator i=packs.begin(), j=other.packs.begin(); (i!=packs.end() && j!=other.packs.end()); ++i, ++j)
42 i->translate_files(files, *j);
43 for(FileMap::const_iterator i=files.begin(), j=other.files.begin(); (i!=files.end() && j!=other.files.end()); ++i, ++j)
44 i->second->translate_objects(objects, *j->second);
47 void PackSource::add_pack_file(const string &fn)
49 add_pack_file(fn, string());
52 void PackSource::add_pack_file(const string &fn, const string &filter)
54 add_pack(nullptr, fn, filter);
57 void PackSource::add_pack_io(IO::Seekable &io, const string &fn)
59 add_pack(&io, fn, string());
62 void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter)
64 add_pack(&io, fn, filter);
67 void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter)
69 auto i = find_if(packs, [io, &fn](const Pack &p){ return p.get_filename()==fn || (io && p.get_io()==io); });
72 packs.push_back(Pack(io, fn));
73 i = prev(packs.end());
76 DataFile::Parser parser(*io, fn);
77 Pack::Loader loader(*i);
81 DataFile::load(*i, fn);
85 i->collect_files(pack_files, filter);
86 for(const auto &kvp: pack_files)
88 files[kvp.first] = kvp.second;
89 kvp.second->collect_objects(objects);
93 list<PackSource::FileInfo> PackSource::list_files() const
95 list<FileInfo> result;
96 for(const auto &kvp: files)
97 result.push_back(kvp.second->get_info());
101 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
103 auto i = objects.find(name);
107 // If the object has a keyword, it must match that of the type
108 if(!i->second->get_keyword().empty() && i->second->get_keyword()!=type.get_keyword())
114 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
117 for(const auto &kvp: objects)
119 if(!kvp.second->get_keyword().empty())
121 if(kvp.second->get_keyword()!=type.get_keyword())
124 else if(!type.match_name(kvp.first))
127 names.push_back(kvp.first);
133 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
135 auto i = objects.find(name);
139 const File &file = i->second->get_file();
141 RefPtr<IO::Base> in = file.open();
142 Parser parser(*in, file.get_full_name());
143 if(file.is_collection())
145 Collection::Loader ldr(coll);
149 type.load_item(coll, parser, name);
152 IO::Seekable *PackSource::open(const string &fn) const
154 auto i = files.find(fn);
156 return i->second->open().release();
162 PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
168 PackSource::Pack::Pack(const Pack &other):
169 filename(other.filename),
171 base_offset(other.base_offset)
173 for(const File &f: other.files)
174 files.push_back(File(f, *this));
177 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
181 for(const File &f: files)
182 fm[f.get_filename()] = &f;
187 for(const File &f: files)
188 if(re.match(f.get_filename()))
189 fm[f.get_filename()] = &f;
193 void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
195 for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
197 auto k = fm.find(i->get_filename());
198 if(k!=fm.end() && k->second==&*j)
204 PackSource::File::File(const Pack &p, const string &fn):
212 PackSource::File::File(const File &other, const Pack &p):
214 filename(other.filename),
215 offset(other.offset),
216 length(other.length),
217 collection(other.collection)
219 for(const Object &o: other.objects)
220 objects.push_back(Object(o, *this));
223 RefPtr<IO::Seekable> PackSource::File::open() const
226 // TODO Performance may be poor without buffering
227 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
230 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
231 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
232 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
237 PackSource::FileInfo PackSource::File::get_info() const
240 info.name = filename;
245 string PackSource::File::get_full_name() const
247 return format("%s/%s", pack.get_filename(), filename);
250 void PackSource::File::collect_objects(ObjectMap &objs) const
252 for(const Object &o: objects)
253 objs[o.get_name()] = &o;
256 void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
258 for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
260 auto k = objs.find(i->get_name());
261 if(k!=objs.end() && k->second==&*j)
267 PackSource::Object::Object(const File &f, const string &n, const string &k):
273 PackSource::Object::Object(const Object &other, const File &f):
276 keyword(other.keyword)
280 PackSource::Pack::Loader::Loader(Pack &p):
281 ObjectLoader<Pack>(p)
283 add("file", &Loader::file);
284 add("base_offset", &Pack::base_offset);
287 void PackSource::Pack::Loader::file(const string &fn)
289 obj.files.push_back(File(obj, fn));
290 load_sub(obj.files.back());
294 PackSource::File::Loader::Loader(File &f):
295 ObjectLoader<File>(f)
297 add("object", &Loader::object);
298 add("slice", &File::offset, &File::length);
301 void PackSource::File::Loader::finish()
305 PackSource::Object ob(obj, obj.filename, string());
306 obj.objects.push_back(ob);
310 void PackSource::File::Loader::object(const string &name, const string &kwd)
312 obj.objects.push_back(PackSource::Object(obj, name, kwd));
313 obj.collection = true;
316 } // namespace DataFile