X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpacksource.cpp;h=b9f601668e44f13be8f7b5ae017d5dc13ed5be1d;hb=e1b8089be727f651fc2022d6e7ff775047730e85;hp=d22c7dc4235990591fffbf7c9d2dafba0eca3aaa;hpb=e44432ecb5d2c0a288652ac6ed9d06f51a68d395;p=libs%2Fdatafile.git diff --git a/source/packsource.cpp b/source/packsource.cpp index d22c7dc..b9f6016 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -18,28 +18,86 @@ void delete_io(Msp::IO::Base *io) namespace Msp { namespace DataFile { +PackSource::PackSource(const PackSource &other): + packs(other.packs), + files(other.files), + objects(other.objects) +{ + translate_maps(other); +} + +PackSource &PackSource::operator=(const PackSource &other) +{ + packs = list(other.packs.begin(), other.packs.end()); + files = other.files; + objects = other.objects; + translate_maps(other); + return *this; +} + +void PackSource::translate_maps(const PackSource &other) +{ + for(list::const_iterator i=packs.begin(), j=other.packs.begin(); (i!=packs.end() && j!=other.packs.end()); ++i, ++j) + i->translate_files(files, *j); + for(FileMap::const_iterator i=files.begin(), j=other.files.begin(); (i!=files.end() && j!=other.files.end()); ++i, ++j) + i->second->translate_objects(objects, *j->second); +} + void PackSource::add_pack_file(const string &fn) { add_pack_file(fn, string()); } void PackSource::add_pack_file(const string &fn, const string &filter) +{ + add_pack(0, fn, filter); +} + +void PackSource::add_pack_io(IO::Seekable &io, const string &fn) +{ + add_pack(&io, fn, string()); +} + +void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &filter) +{ + add_pack(&io, fn, filter); +} + +void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filter) { Pack *pack = 0; for(list::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i) - if(i->get_filename()==fn) + if(i->get_filename()==fn || (io && i->get_io()==io)) pack = &*i; if(!pack) { - packs.push_back(Pack(fn)); + packs.push_back(Pack(io, fn)); pack = &packs.back(); - DataFile::load(*pack, fn); + if(io) + { + DataFile::Parser parser(*io, fn); + Pack::Loader loader(*pack); + loader.load(parser); + } + else + DataFile::load(*pack, fn); } FileMap pack_files; pack->collect_files(pack_files, filter); for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i) + { + files[i->first] = i->second; i->second->collect_objects(objects); + } +} + +list PackSource::list_files() const +{ + list result; + for(FileMap::const_iterator i=files.begin(); i!=files.end(); ++i) + result.push_back(i->second->get_info()); + return result; } bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const @@ -80,10 +138,7 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons if(i==objects.end()) return; - File &file = i->second->get_file(); - if(file.is_loaded()) - return; - file.set_loaded(); + const File &file = i->second->get_file(); RefPtr in = file.open(); Parser parser(*in, file.get_full_name()); @@ -96,12 +151,31 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons type.load_item(coll, parser, name); } +IO::Seekable *PackSource::open(const string &fn) const +{ + FileMap::const_iterator i = files.find(fn); + if(i!=files.end()) + return i->second->open().release(); + + return 0; +} -PackSource::Pack::Pack(const string &fn): + +PackSource::Pack::Pack(IO::Seekable *i, const string &fn): filename(fn), + io(i), base_offset(0) { } +PackSource::Pack::Pack(const Pack &other): + filename(other.filename), + io(other.io), + base_offset(other.base_offset) +{ + for(list::const_iterator i=other.files.begin(); i!=other.files.end(); ++i) + files.push_back(File(*i, *this)); +} + void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const { if(filter.empty()) @@ -118,32 +192,61 @@ void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const } } +void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const +{ + for(list::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j) + { + FileMap::iterator k = fm.find(i->get_filename()); + if(k!=fm.end() && k->second==&*j) + k->second = &*i; + } +} + PackSource::File::File(const Pack &p, const string &fn): pack(p), filename(fn), offset(0), length(0), - collection(false), - loaded(false) + collection(false) { } -RefPtr PackSource::File::open() const +PackSource::File::File(const File &other, const Pack &p): + pack(p), + filename(other.filename), + offset(other.offset), + length(other.length), + collection(other.collection) { - IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename()); - IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length); - io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file)); - return io_slice; + for(list::const_iterator i=other.objects.begin(); i!=other.objects.end(); ++i) + objects.push_back(Object(*i, *this)); } -string PackSource::File::get_full_name() const +RefPtr PackSource::File::open() const { - return format("%s/%s", pack.get_filename(), filename); + if(pack.get_io()) + // TODO Performance may be poor without buffering + return new IO::Slice(*pack.get_io(), pack.get_base_offset()+offset, length); + else + { + IO::BufferedFile *io_file = new IO::BufferedFile(pack.get_filename()); + IO::Slice *io_slice = new IO::Slice(*io_file, pack.get_base_offset()+offset, length); + io_slice->signal_deleted.connect(sigc::bind(sigc::ptr_fun(delete_io), io_file)); + return io_slice; + } } -void PackSource::File::set_loaded() +PackSource::FileInfo PackSource::File::get_info() const { - loaded = true; + FileInfo info; + info.name = filename; + info.size = length; + return info; +} + +string PackSource::File::get_full_name() const +{ + return format("%s/%s", pack.get_filename(), filename); } void PackSource::File::collect_objects(ObjectMap &objs) const @@ -152,13 +255,29 @@ void PackSource::File::collect_objects(ObjectMap &objs) const objs[i->get_name()] = &*i; } +void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const +{ + for(list::const_iterator i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j) + { + ObjectMap::iterator k = objs.find(i->get_name()); + if(k!=objs.end() && k->second==&*j) + k->second = &*i; + } +} + -PackSource::Object::Object(File &f, const string &n, const string &k): +PackSource::Object::Object(const File &f, const string &n, const string &k): file(f), name(n), keyword(k) { } +PackSource::Object::Object(const Object &other, const File &f): + file(f), + name(other.name), + keyword(other.keyword) +{ } + PackSource::Pack::Loader::Loader(Pack &p): ObjectLoader(p)