From: Mikko Rasa Date: Sun, 19 Oct 2014 17:33:05 +0000 (+0300) Subject: Support adding packs from IO::Seekable objects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=461967af6834fb0fccdd39a8d76192d411223148;p=libs%2Fdatafile.git Support adding packs from IO::Seekable objects This allows using any seekable objects as packs, such as Android assets. --- diff --git a/source/packsource.cpp b/source/packsource.cpp index 00af3ad..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,9 +41,16 @@ 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; @@ -115,8 +132,9 @@ IO::Seekable *PackSource::open(const string &fn) const } -PackSource::Pack::Pack(const string &fn): +PackSource::Pack::Pack(IO::Seekable *i, const string &fn): filename(fn), + io(i), base_offset(0) { } @@ -148,10 +166,16 @@ PackSource::File::File(const Pack &p, const string &fn): 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; + 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 diff --git a/source/packsource.h b/source/packsource.h index a599b5c..440b451 100644 --- a/source/packsource.h +++ b/source/packsource.h @@ -48,13 +48,15 @@ private: private: std::string filename; + IO::Seekable *io; unsigned base_offset; std::list files; public: - Pack(const std::string &); + Pack(IO::Seekable *, const std::string &); const std::string &get_filename() const { return filename; } + IO::Seekable *get_io() const { return io; } unsigned get_base_offset() const { return base_offset; } void collect_files(FileMap &, const std::string &) const; @@ -123,6 +125,15 @@ public: read on the first call; subsequent calls will use cached data. */ void add_pack_file(const std::string &, const std::string &); + /** Adds a pack from an existing seekable I/O object. The same object is + used for all accesses to the pack, so it must not be deleted before the + PackSource. */ + void add_pack_io(IO::Seekable &, const std::string & = std::string()); + +private: + void add_pack(IO::Seekable *, const std::string &, const std::string &); + +public: /// Returns information about the files in the pack. std::list list_files() const;