From: Mikko Rasa Date: Thu, 15 Nov 2012 21:25:59 +0000 (+0200) Subject: Cache filenames in DirectorySource X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a9b55a13d8da2a47ace71afe7d5258cf83aa9813;p=libs%2Fdatafile.git Cache filenames in DirectorySource --- diff --git a/source/directorysource.cpp b/source/directorysource.cpp index b1b19a1..5d70bb5 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; } @@ -45,17 +42,12 @@ void DirectorySource::load(Collection &coll, const CollectionItemTypeBase &type, 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 diff --git a/source/directorysource.h b/source/directorysource.h index 0710225..dfce833 100644 --- a/source/directorysource.h +++ b/source/directorysource.h @@ -13,7 +13,9 @@ A source that load items from files in a directory. class DirectorySource: public CollectionSource { private: - std::list dirs; + typedef std::map ObjectMap; + + ObjectMap objects; public: void add_directory(const FS::Path &);