forked from MrBesen/libmrbesen
scan with callback
This commit is contained in:
parent
14d0678fe5
commit
90f99a75d5
|
@ -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));
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue