namespace Msp {
namespace DataFile {
-DirectorySource::DirectorySource()
+void DirectorySource::add_directory(const FS::Path &d, bool replace)
{
- set_directory(".");
-}
-
-void DirectorySource::set_directory(const FS::Path &d)
-{
- dirs.clear();
- add_directory(d);
-}
-
-void DirectorySource::add_directory(const FS::Path &d)
-{
- dirs.push_back(d);
+ for(const string &f: FS::list_files(d))
+ if(!objects.count(f) || replace)
+ objects[f] = d/f;
}
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(const auto &kvp: objects)
+ if(type.match_name(kvp.first))
+ names.push_back(kvp.first);
return names;
}
}
}
+IO::Seekable *DirectorySource::open(const string &name) const
+{
+ FS::Path file;
+ if(lookup_file(name, file))
+ return new IO::BufferedFile(file.str());
+
+ return 0;
+}
+
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