X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdirectorysource.cpp;h=e9f95fc830c9686997bc13088460c0da38847642;hb=256b44a5009467171af53316141277027bcc0ba4;hp=b1b19a1faa74e829d2a35dba9adbd39e29170350;hpb=c634c9a6aac3fbedecc6e2c8ef69fc11dabd49d0;p=libs%2Fdatafile.git diff --git a/source/directorysource.cpp b/source/directorysource.cpp index b1b19a1..e9f95fc 100644 --- a/source/directorysource.cpp +++ b/source/directorysource.cpp @@ -8,27 +8,24 @@ using namespace std; namespace Msp { namespace DataFile { -void DirectorySource::add_directory(const FS::Path &d) +void DirectorySource::add_directory(const FS::Path &d, bool replace) { - 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::const_iterator i=dirs.begin(); i!=dirs.end(); ++i) - { - list files = FS::list_files(*i); - for(list::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; } @@ -43,19 +40,23 @@ void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type, } } +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::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