X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=blobdiff_plain;f=source%2Fpacksource.cpp;h=00af3ad129079aa71d7d415a39f16a67bf54b5b6;hp=c06b270e30d8ecb4ef4fed14840989ae85e5e47f;hb=f8820db0e2d1de85752de809f0a8e24394a33f61;hpb=b1bc25649c1f22abf940a807d934f1e9bb780c28 diff --git a/source/packsource.cpp b/source/packsource.cpp index c06b270..00af3ad 100644 --- a/source/packsource.cpp +++ b/source/packsource.cpp @@ -1,19 +1,54 @@ +#include #include +#include #include "collection.h" #include "packsource.h" using namespace std; +namespace { + +void delete_io(Msp::IO::Base *io) +{ + delete io; +} + +} + namespace Msp { namespace DataFile { void PackSource::add_pack_file(const string &fn) { - packs.push_back(Pack(fn)); - Pack &pack = packs.back(); - DataFile::load(pack, fn); + add_pack_file(fn, string()); +} + +void PackSource::add_pack_file(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) + pack = &*i; + if(!pack) + { + packs.push_back(Pack(fn)); + pack = &packs.back(); + 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); +} - pack.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 @@ -70,16 +105,35 @@ 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): filename(fn), base_offset(0) { } -void PackSource::Pack::collect_objects(ObjectMap &objs) const +void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const { - for(list::const_iterator i=files.begin(); i!=files.end(); ++i) - i->collect_objects(objs); + if(filter.empty()) + { + for(list::const_iterator i=files.begin(); i!=files.end(); ++i) + fm[i->get_filename()] = &*i; + } + else + { + Regex re(filter); + for(list::const_iterator i=files.begin(); i!=files.end(); ++i) + if(re.match(i->get_filename())) + fm[i->get_filename()] = &*i; + } } @@ -92,11 +146,20 @@ PackSource::File::File(const Pack &p, const string &fn): loaded(false) { } -RefPtr PackSource::File::open() const +RefPtr PackSource::File::open() 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; +} + +PackSource::FileInfo PackSource::File::get_info() const { - RefPtr io_file = new IO::BufferedFile(pack.get_filename()); - io_file->seek(pack.get_base_offset()+offset, IO::S_BEG); - return io_file; + FileInfo info; + info.name = filename; + info.size = length; + return info; } string PackSource::File::get_full_name() const