X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fpacksource.cpp;h=1accdb0fc3a384c456f4535db23e164a89efb5b7;hp=d054e1d4ee9e50ef99c8d9c136aee978237113bf;hb=b39ce68f12c30eedb272b65fe78baec5864d89ca;hpb=835fcd2f79e8848fa0d92be667c0e02952e23436 diff --git a/source/packsource.cpp b/source/packsource.cpp index d054e1d..1accdb0 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -1,3 +1,4 @@ +#include #include #include #include "collection.h" @@ -5,6 +6,15 @@ using namespace std; +namespace { + +void delete_io(Msp::IO::Base *io) +{ + delete io; +} + +} + namespace Msp { namespace DataFile { @@ -14,24 +24,55 @@ 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(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); + 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); @@ -71,9 +112,6 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons return; File &file = i->second->get_file(); - if(file.is_loaded()) - return; - file.set_loaded(); RefPtr in = file.open(); Parser parser(*in, file.get_full_name()); @@ -86,9 +124,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) { } @@ -114,25 +162,34 @@ PackSource::File::File(const Pack &p, const string &fn): filename(fn), offset(0), length(0), - collection(false), - loaded(false) + collection(false) { } -RefPtr PackSource::File::open() const +RefPtr PackSource::File::open() const { - RefPtr io_file = new IO::BufferedFile(pack.get_filename()); - io_file->seek(pack.get_base_offset()+offset, IO::S_BEG); - return io_file; + 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; + } } -string PackSource::File::get_full_name() const +PackSource::FileInfo PackSource::File::get_info() const { - return format("%s/%s", pack.get_filename(), filename); + FileInfo info; + info.name = filename; + info.size = length; + return info; } -void PackSource::File::set_loaded() +string PackSource::File::get_full_name() const { - loaded = true; + return format("%s/%s", pack.get_filename(), filename); } void PackSource::File::collect_objects(ObjectMap &objs) const