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):
167 PackSource::Pack::Pack(const Pack &other):
168 filename(other.filename),
170 base_offset(other.base_offset)
172 for(const File &f: other.files)
173 files.push_back(File(f, *this));
176 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
180 for(const File &f: files)
181 fm[f.get_filename()] = &f;
186 for(const File &f: files)
187 if(re.match(f.get_filename()))
188 fm[f.get_filename()] = &f;
192 void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
194 for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
196 auto k = fm.find(i->get_filename());
197 if(k!=fm.end() && k->second==&*j)
203 PackSource::File::File(const Pack &p, const string &fn):
208 PackSource::File::File(const File &other, const Pack &p):
210 filename(other.filename),
211 offset(other.offset),
212 length(other.length),
213 collection(other.collection)
215 for(const Object &o: other.objects)
216 objects.push_back(Object(o, *this));
219 RefPtr<IO::Seekable> PackSource::File::open() const
222 // TODO Performance may be poor without buffering
223 return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length);
226 IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename());
227 IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length);
228 io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file));
233 PackSource::FileInfo PackSource::File::get_info() const
236 info.name = filename;
241 string PackSource::File::get_full_name() const
243 return format("%s/%s", pack.get_filename(), filename);
246 void PackSource::File::collect_objects(ObjectMap &objs) const
248 for(const Object &o: objects)
249 objs[o.get_name()] = &o;
252 void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
254 for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
256 auto k = objs.find(i->get_name());
257 if(k!=objs.end() && k->second==&*j)
263 PackSource::Object::Object(const File &f, const string &n, const string &k):
269 PackSource::Object::Object(const Object &other, const File &f):
272 keyword(other.keyword)
276 PackSource::Pack::Loader::Loader(Pack &p):
277 ObjectLoader<Pack>(p)
279 add("file", &Loader::file);
280 add("base_offset", &Pack::base_offset);
283 void PackSource::Pack::Loader::file(const string &fn)
285 obj.files.push_back(File(obj, fn));
286 load_sub(obj.files.back());
290 PackSource::File::Loader::Loader(File &f):
291 ObjectLoader<File>(f)
293 add("object", &Loader::object);
294 add("slice", &File::offset, &File::length);
297 void PackSource::File::Loader::finish()
301 PackSource::Object ob(obj, obj.filename, string());
302 obj.objects.push_back(ob);
306 void PackSource::File::Loader::object(const string &name, const string &kwd)
308 obj.objects.push_back(PackSource::Object(obj, name, kwd));
309 obj.collection = true;
312 } // namespace DataFile