scan with callback

This commit is contained in:
mrbesen 2020-10-02 17:35:48 +02:00
parent 14d0678fe5
commit 90f99a75d5
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
2 changed files with 12 additions and 9 deletions

View File

@ -25,7 +25,9 @@ enum class FileType : unsigned char {
};
typedef std::function<bool(const std::string&, FileType type)> fileNameFilter;
typedef std::function<void(const std::string&, FileType type)> fileCallback;
template<class Container>
bool scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
bool scan(const std::string& path, fileCallback clb, bool prefixdir = false, fileNameFilter fnf = fileNameFilter(nullptr));
}

View File

@ -42,6 +42,15 @@ void mrbesen::Files::extention(const std::string& path, std::string& ext) {
template<class Container>
bool mrbesen::Files::scan(const std::string& path, std::insert_iterator<Container> it, bool prefixdir, fileNameFilter fnf) {
return scan(path, [&](const std::string& p, FileType t){ it = p; }, prefixdir, fnf);
}
//curently only these are supported, because iterators may break on others upon insertion
template bool mrbesen::Files::scan<std::list<std::string>>(const std::string&, std::insert_iterator<std::list<std::string>>, bool, fileNameFilter);
template bool mrbesen::Files::scan<std::set<std::string>>(const std::string&, std::insert_iterator<std::set<std::string>>, bool, fileNameFilter);
template bool mrbesen::Files::scan<std::multiset<std::string>>(const std::string&, std::insert_iterator<std::multiset<std::string>>, bool, fileNameFilter);
bool mrbesen::Files::scan(const std::string& path, fileCallback clb, bool prefixdir, fileNameFilter fnf) {
std::string path_ = path;
mrbesen::Util::insertEnd(path_, "/");
@ -62,18 +71,10 @@ bool mrbesen::Files::scan(const std::string& path, std::insert_iterator<Containe
}
if(prefixdir)
fname = path_ + fname;
it = fname;
clb(fname, type);
}
closedir(dir);
return true;
}
//curently only these are supported, because iterators may break on others upon insertion
template bool mrbesen::Files::scan<std::list<std::string>>(const std::string&, std::insert_iterator<std::list<std::string>>, bool, fileNameFilter);
template bool mrbesen::Files::scan<std::set<std::string>>(const std::string&, std::insert_iterator<std::set<std::string>>, bool, fileNameFilter);
template bool mrbesen::Files::scan<std::multiset<std::string>>(const std::string&, std::insert_iterator<std::multiset<std::string>>, bool, fileNameFilter);