]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/packsource.cpp
Use C++11 features to manipulate containers
[libs/datafile.git] / source / packsource.cpp
index b9f601668e44f13be8f7b5ae017d5dc13ed5be1d..b2fb17abc5ae9393feabf2f223130b547a3c072e 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>
@@ -65,44 +66,41 @@ void PackSource::add_pack_io(IO::Seekable &io, const string &fn, const string &f
 
 void PackSource::add_pack(IO::Seekable *io, 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 || (io && i->get_io()==io))
-                       pack = &*i;
-       if(!pack)
+       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(io, fn));
-               pack = &packs.back();
+               i = prev(packs.end());
                if(io)
                {
                        DataFile::Parser parser(*io, fn);
-                       Pack::Loader loader(*pack);
+                       Pack::Loader loader(*i);
                        loader.load(parser);
                }
                else
-                       DataFile::load(*pack, fn);
+                       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->collect_files(pack_files, filter);
+       for(const auto &kvp: pack_files)
        {
-               files[i->first] = i->second;
-               i->second->collect_objects(objects);
+               files[kvp.first] = kvp.second;
+               kvp.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());
+       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;
 
@@ -116,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;
@@ -134,7 +132,7 @@ 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;
 
@@ -153,7 +151,7 @@ void PackSource::load(Collection &coll, const CollectionItemTypeBase &type, cons
 
 IO::Seekable *PackSource::open(const string &fn) const
 {
-       FileMap::const_iterator i = files.find(fn);
+       auto i = files.find(fn);
        if(i!=files.end())
                return i->second->open().release();
 
@@ -172,31 +170,31 @@ PackSource::Pack::Pack(const Pack &other):
        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));
+       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(list<File>::const_iterator i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
+       for(auto i=files.begin(), j=source.files.begin(); (i!=files.end() && j!=source.files.end()); ++i, ++j)
        {
-               FileMap::iterator k = fm.find(i->get_filename());
+               auto k = fm.find(i->get_filename());
                if(k!=fm.end() && k->second==&*j)
                        k->second = &*i;
        }
@@ -218,8 +216,8 @@ PackSource::File::File(const File &other, const Pack &p):
        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));
+       for(const Object &o: other.objects)
+               objects.push_back(Object(o, *this));
 }
 
 RefPtr<IO::Seekable> PackSource::File::open() const
@@ -251,15 +249,15 @@ string PackSource::File::get_full_name() const
 
 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;
+       for(const Object &o: objects)
+               objs[o.get_name()] = &o;
 }
 
 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)
+       for(auto i=objects.begin(), j=source.objects.begin(); (i!=objects.end() && j!=source.objects.end()); ++i, ++j)
        {
-               ObjectMap::iterator k = objs.find(i->get_name());
+               auto k = objs.find(i->get_name());
                if(k!=objs.end() && k->second==&*j)
                        k->second = &*i;
        }