X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdirectorysource.cpp;h=207f30d0e31716c310f0927b82f5ba14fe456b6a;hb=b39ce68f12c30eedb272b65fe78baec5864d89ca;hp=4ee3b80657eb014bf9bfee540d365879c27fc8c2;hpb=b1bc25649c1f22abf940a807d934f1e9bb780c28;p=libs%2Fdatafile.git diff --git a/source/directorysource.cpp b/source/directorysource.cpp index 4ee3b80..207f30d 100644 --- a/source/directorysource.cpp +++ b/source/directorysource.cpp @@ -8,38 +8,27 @@ using namespace std; 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); + 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; } @@ -54,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