+#include <msp/core/algorithm.h>
#include <msp/io/slice.h>
#include <msp/strings/format.h>
#include <msp/strings/regex.h>
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;
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;
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;
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();
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;
}
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
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;
}