void DirectorySource::add_directory(const FS::Path &d)
{
- dirs.push_back(d);
+ list<string> files = FS::list_files(d);
+ for(list<string>::const_iterator i=files.begin(); i!=files.end(); ++i)
+ objects[*i] = d / *i;
}
bool DirectorySource::is_loadable(const CollectionItemTypeBase &, const string &name) const
{
- FS::Path path;
- return lookup_file(name, path);
+ return objects.count(name);
}
CollectionSource::NameList DirectorySource::get_names(const CollectionItemTypeBase &type) const
{
NameList names;
- for(list<FS::Path>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
- {
- list<string> files = FS::list_files(*i);
- for(list<string>::const_iterator j=files.begin(); j!=files.end(); ++j)
- if(type.match_name(*j))
- names.push_back(*j);
- }
+ for(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i)
+ if(type.match_name(i->first))
+ names.push_back(i->first);
return names;
}
bool DirectorySource::lookup_file(const string &name, FS::Path &result) const
{
- for(list<FS::Path>::const_iterator i=dirs.begin(); i!=dirs.end(); ++i)
- {
- FS::Path file_path = *i/name;
- if(FS::exists(file_path))
- {
- result = file_path;
- return true;
- }
- }
+ ObjectMap::const_iterator i = objects.find(name);
+ if(i==objects.end())
+ return false;
- return false;
+ result = i->second;
+ return true;
}
} // namespace DataFile
class DirectorySource: public CollectionSource
{
private:
- std::list<FS::Path> dirs;
+ typedef std::map<std::string, FS::Path> ObjectMap;
+
+ ObjectMap objects;
public:
void add_directory(const FS::Path &);