X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdirectorysource.cpp;h=207f30d0e31716c310f0927b82f5ba14fe456b6a;hb=b39ce68f12c30eedb272b65fe78baec5864d89ca;hp=b1b19a1faa74e829d2a35dba9adbd39e29170350;hpb=c634c9a6aac3fbedecc6e2c8ef69fc11dabd49d0;p=libs%2Fdatafile.git diff --git a/source/directorysource.cpp b/source/directorysource.cpp index b1b19a1..207f30d 100644 --- a/source/directorysource.cpp +++ b/source/directorysource.cpp @@ -8,27 +8,27 @@ 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); + list files = FS::list_files(d); + for(list::const_iterator i=files.begin(); i!=files.end(); ++i) + { + if(!objects.count(*i) || replace) + 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::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(ObjectMap::const_iterator i=objects.begin(); i!=objects.end(); ++i) + if(type.match_name(i->first)) + names.push_back(i->first); return names; } @@ -43,19 +43,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