]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/packsource.cpp
Export file metadata from PackSource
[libs/datafile.git] / source / packsource.cpp
index d054e1d4ee9e50ef99c8d9c136aee978237113bf..00af3ad129079aa71d7d415a39f16a67bf54b5b6 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/io/slice.h>
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
 #include "collection.h"
@@ -5,6 +6,15 @@
 
 using namespace std;
 
+namespace {
+
+void delete_io(Msp::IO::Base *io)
+{
+       delete io;
+}
+
+}
+
 namespace Msp {
 namespace DataFile {
 
@@ -28,10 +38,19 @@ void PackSource::add_pack_file(const string &fn, const string &filter)
 
        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::FileInfo> PackSource::list_files() const
+{
+       list<FileInfo> 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);
@@ -86,6 +105,15 @@ 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),
@@ -118,11 +146,20 @@ PackSource::File::File(const Pack &p, const string &fn):
        loaded(false)
 { }
 
-RefPtr<IO::Base> PackSource::File::open() const
+RefPtr<IO::Seekable> 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::BufferedFile> 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