X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fpacksource.cpp;h=938a627c05fb9b3685cb25cb53678439e39697f7;hb=461967af6834fb0fccdd39a8d76192d411223148;hp=d22c7dc4235990591fffbf7c9d2dafba0eca3aaa;hpb=e44432ecb5d2c0a288652ac6ed9d06f51a68d395;p=libs%2Fdatafile.git diff --git a/source/packsource.cpp b/source/packsource.cpp index d22c7dc..938a627 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -24,6 +24,16 @@ void PackSource::add_pack_file(const string &fn) } 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::Seekable *io, const string &fn, const string &filter) { Pack *pack = 0; for(list::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i) @@ -31,17 +41,33 @@ void PackSource::add_pack_file(const string &fn, const string &filter) 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); + files.insert(pack_files.begin(), pack_files.end()); for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i) 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 { ObjectMap::const_iterator i = objects.find(name); @@ -96,9 +122,19 @@ 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) { } @@ -128,12 +164,26 @@ PackSource::File::File(const Pack &p, const string &fn): loaded(false) { } -RefPtr PackSource::File::open() const +RefPtr PackSource::File::open() const +{ + 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; + } +} + +PackSource::FileInfo PackSource::File::get_info() const { - 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; + FileInfo info; + info.name = filename; + info.size = length; + return info; } string PackSource::File::get_full_name() const