X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fdirectorysource.cpp;h=4bf9f5cd3d12b0808f288b9e3cd647a5ee0db682;hb=9b1656018f783eb4aad2fbdc1de1404691e89bb1;hp=b1b19a1faa74e829d2a35dba9adbd39e29170350;hpb=c634c9a6aac3fbedecc6e2c8ef69fc11dabd49d0;p=libs%2Fdatafile.git diff --git a/source/directorysource.cpp b/source/directorysource.cpp index b1b19a1..4bf9f5c 100644 --- a/source/directorysource.cpp +++ b/source/directorysource.cpp @@ -10,25 +10,22 @@ namespace DataFile { 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) + 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 +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