]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/packsource.cpp
Implement proper copy semantics
[libs/datafile.git] / source / packsource.cpp
index fe9e67db6eabae37ae1b38278d68104b305f2739..b9f601668e44f13be8f7b5ae017d5dc13ed5be1d 100644 (file)
@@ -18,6 +18,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());
@@ -60,9 +85,11 @@ void PackSource::add_pack(IO::Seekable *io, const string &fn, const string &filt
 
        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)
+       {
+               files[i->first] = i->second;
                i->second->collect_objects(objects);
+       }
 }
 
 list<PackSource::FileInfo> PackSource::list_files() const
@@ -111,10 +138,7 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons
        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());
@@ -143,6 +167,15 @@ PackSource::Pack::Pack(IO::Seekable *i, const string &fn):
        base_offset(0)
 { }
 
+PackSource::Pack::Pack(const Pack &other):
+       filename(other.filename),
+       io(other.io),
+       base_offset(other.base_offset)
+{
+       for(list<File>::const_iterator i=other.files.begin(); i!=other.files.end(); ++i)
+               files.push_back(File(*i, *this));
+}
+
 void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
 {
        if(filter.empty())
@@ -159,16 +192,36 @@ void PackSource::Pack::collect_files(FileMap &fm, const string &filter) const
        }
 }
 
+void PackSource::Pack::translate_files(FileMap &fm, const Pack &source) const
+{
+       for(list<File>::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
+       {
+               FileMap::iterator 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)
+       collection(false)
 { }
 
+PackSource::File::File(const File &other, const Pack &p):
+       pack(p),
+       filename(other.filename),
+       offset(other.offset),
+       length(other.length),
+       collection(other.collection)
+{
+       for(list<Object>::const_iterator i=other.objects.begin(); i!=other.objects.end(); ++i)
+               objects.push_back(Object(*i, *this));
+}
+
 RefPtr<IO::Seekable> PackSource::File::open() const
 {
        if(pack.get_io())
@@ -196,24 +249,35 @@ string PackSource::File::get_full_name() const
        return format("%s/%s", pack.get_filename(), filename);
 }
 
-void PackSource::File::set_loaded()
-{
-       loaded = true;
-}
-
 void PackSource::File::collect_objects(ObjectMap &objs) const
 {
        for(list<Object>::const_iterator i=objects.begin(); i!=objects.end(); ++i)
                objs[i->get_name()] = &*i;
 }
 
+void PackSource::File::translate_objects(ObjectMap &objs, const File &source) const
+{
+       for(list<Object>::const_iterator i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
+       {
+               ObjectMap::iterator 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)