]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/packsource.cpp
Cosmetic changes
[libs/datafile.git] / source / packsource.cpp
index d22c7dc4235990591fffbf7c9d2dafba0eca3aaa..0fb64843b305edd8839a4e93bf1bb04c9c3f33de 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/core/algorithm.h>
 #include <msp/io/slice.h>
 #include <msp/strings/format.h>
 #include <msp/strings/regex.h>
@@ -18,6 +19,31 @@ void delete_io(Msp::IO::Base *io)
 namespace Msp {
 namespace DataFile {
 
+PackSource::PackSource(const PackSource &other):
+       packs(other.packs),
+       files(other.files),
+       objects(other.objects)
+{
+       translate_maps(other);
+}
+
+PackSource &PackSource::operator=(const PackSource &other)
+{
+       packs = list<Pack>(other.packs.begin(), other.packs.end());
+       files = other.files;
+       objects = other.objects;
+       translate_maps(other);
+       return *this;
+}
+
+void PackSource::translate_maps(const PackSource &other)
+{
+       for(list<Pack>::const_iterator i=packs.begin(), j=other.packs.begin(); (i!=packs.end() && j!=other.packs.end()); ++i, ++j)
+               i->translate_files(files, *j);
+       for(FileMap::const_iterator i=files.begin(), j=other.files.begin(); (i!=files.end() && j!=other.files.end()); ++i, ++j)
+               i->second->translate_objects(objects, *j->second);
+}
+
 void PackSource::add_pack_file(const string &fn)
 {
        add_pack_file(fn, string());
@@ -25,26 +51,56 @@ void PackSource::add_pack_file(const string &fn)
 
 void PackSource::add_pack_file(const string &fn, const string &filter)
 {
-       Pack *pack = 0;
-       for(list<Pack>::iterator i=packs.begin(); (!pack && i!=packs.end()); ++i)
-               if(i->get_filename()==fn)
-                       pack = &*i;
-       if(!pack)
+       add_pack(nullptr, 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)
+{
+       auto i = find_if(packs, [io, &fn](const Pack &p){ return p.get_filename()==fn || (io && p.get_io()==io); });
+       if(i==packs.end())
        {
-               packs.push_back(Pack(fn));
-               pack = &packs.back();
-               DataFile::load(*pack, fn);
+               packs.push_back(Pack(io, fn));
+               i = prev(packs.end());
+               if(io)
+               {
+                       DataFile::Parser parser(*io, fn);
+                       Pack::Loader loader(*i);
+                       loader.load(parser);
+               }
+               else
+                       DataFile::load(*i, fn);
        }
 
        FileMap pack_files;
-       pack->collect_files(pack_files, filter);
-       for(FileMap::const_iterator i=pack_files.begin(); i!=pack_files.end(); ++i)
-               i->second->collect_objects(objects);
+       i->collect_files(pack_files, filter);
+       for(const auto &kvp: pack_files)
+       {
+               files[kvp.first] = kvp.second;
+               kvp.second->collect_objects(objects);
+       }
+}
+
+list<PackSource::FileInfo> PackSource::list_files() const
+{
+       list<FileInfo> result;
+       for(const auto &kvp: files)
+               result.push_back(kvp.second->get_info());
+       return result;
 }
 
 bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &name) const
 {
-       ObjectMap::const_iterator i = objects.find(name);
+       auto i = objects.find(name);
        if(i==objects.end())
                return false;
 
@@ -58,17 +114,17 @@ bool PackSource::is_loadable(const CollectionItemTypeBase &type, const string &n
 CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &type) const
 {
        NameList names;
-       for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+       for(const auto &kvp: objects)
        {
-               if(!i->second->get_keyword().empty())
+               if(!kvp.second->get_keyword().empty())
                {
-                       if(i->second->get_keyword()!=type.get_keyword())
+                       if(kvp.second->get_keyword()!=type.get_keyword())
                                continue;
                }
-               else if(!type.match_name(i->first))
+               else if(!type.match_name(kvp.first))
                        continue;
 
-               names.push_back(i->first);
+               names.push_back(kvp.first);
        }
 
        return names;
@@ -76,14 +132,11 @@ CollectionSource::NameList PackSource::get_names(const CollectionItemTypeBase &t
 
 void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, const string &name) const
 {
-       ObjectMap::const_iterator i = objects.find(name);
+       auto i = objects.find(name);
        if(i==objects.end())
                return;
 
-       File &file = i->second->get_file();
-       if(file.is_loaded())
-               return;
-       file.set_loaded();
+       const File &file = i->second->get_file();
 
        RefPtr<IO::Base> in = file.open();
        Parser parser(*in, file.get_full_name());
@@ -96,44 +149,93 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons
                type.load_item(coll, parser, name);
 }
 
+IO::Seekable *PackSource::open(const string &fn) const
+{
+       auto i = files.find(fn);
+       if(i!=files.end())
+               return i->second->open().release();
+
+       return nullptr;
+}
+
 
-PackSource::Pack::Pack(const string &fn):
+PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
        filename(fn),
-       base_offset(0)
+       io(i)
 { }
 
+PackSource::Pack::Pack(const Pack &other):
+       filename(other.filename),
+       io(other.io),
+       base_offset(other.base_offset)
+{
+       for(const File &f: other.files)
+               files.push_back(File(f, *this));
+}
+
 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
 {
        if(filter.empty())
        {
-               for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
-                       fm[i->get_filename()] = &*i;
+               for(const File &f: files)
+                       fm[f.get_filename()] = &f;
        }
        else
        {
                Regex re(filter);
-               for(list<File>::const_iterator i=files.begin(); i!=files.end(); ++i)
-                       if(re.match(i->get_filename()))
-                               fm[i->get_filename()] = &*i;
+               for(const File &f: files)
+                       if(re.match(f.get_filename()))
+                               fm[f.get_filename()] = &f;
+       }
+}
+
+void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
+{
+       for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
+       {
+               auto k = fm.find(i->get_filename());
+               if(k!=fm.end() && k->second==&*j)
+                       k->second = &*i;
        }
 }
 
 
 PackSource::File::File(const Pack &p, const string &fn):
        pack(p),
-       filename(fn),
-       offset(0),
-       length(0),
-       collection(false),
-       loaded(false)
+       filename(fn)
 { }
 
-RefPtr<IO::Base> PackSource::File::open() const
+PackSource::File::File(const File &other, const Pack &p):
+       pack(p),
+       filename(other.filename),
+       offset(other.offset),
+       length(other.length),
+       collection(other.collection)
+{
+       for(const Object &o: other.objects)
+               objects.push_back(Object(o, *this));
+}
+
+RefPtr<IO::Seekable> 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
@@ -141,24 +243,35 @@ string PackSource::File::get_full_name() const
        return format("%s/%s", pack.get_filename(), filename);
 }
 
-void PackSource::File::set_loaded()
+void PackSource::File::collect_objects(ObjectMap &objs) const
 {
-       loaded = true;
+       for(const Object &o: objects)
+               objs[o.get_name()] = &o;
 }
 
-void PackSource::File::collect_objects(ObjectMap &objs) const
+void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
 {
-       for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
-               objs[i->get_name()] = &*i;
+       for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
+       {
+               auto k = objs.find(i->get_name());
+               if(k!=objs.end() && k->second==&*j)
+                       k->second = &*i;
+       }
 }
 
 
-PackSource::Object::Object(File &f, const string &n, const string &k):
+PackSource::Object::Object(const File &f, const string &n, const string &k):
        file(f),
        name(n),
        keyword(k)
 { }
 
+PackSource::Object::Object(const Object &other, const File &f):
+       file(f),
+       name(other.name),
+       keyword(other.keyword)
+{ }
+
 
 PackSource::Pack::Loader::Loader(Pack &p):
        ObjectLoader<Pack>(p)