#include "files.h" #include #include #include #include "util.h" void mrbesen::files::parent(const std::string& child, std::string& out) { //letzten path trenner finden size_t pos = child.rfind('/', child.length() -2); //das erste Zeichen überspringen (könnte ein / sein) if(pos == std::string::npos) pos = child.rfind('\\'); if(pos != std::string::npos) out = child.substr(0, pos+1); else out = ""; } void mrbesen::files::file(const std::string& path, std::string& out) { //letzten path trenner finden size_t pos = path.rfind('/', path.length() -2); //das erste Zeichen überspringen (könnte ein / sein) if(pos == std::string::npos) pos = path.rfind('\\'); if(pos != std::string::npos) out = path.substr(pos+1); else out = path; } void mrbesen::files::extention(const std::string& path, std::string& ext) { std::string filestr; file(path, filestr); size_t pos = filestr.rfind('.'); if(pos == std::string::npos || pos+1 == filestr.size()) { ext = ""; return; } ext = filestr.substr(pos+1); } template bool mrbesen::files::scan(const std::string& path, std::insert_iterator 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>(const std::string&, std::insert_iterator>, bool, fileNameFilter); template bool mrbesen::files::scan>(const std::string&, std::insert_iterator>, bool, fileNameFilter); template bool mrbesen::files::scan>(const std::string&, std::insert_iterator>, bool, fileNameFilter); bool mrbesen::files::scan(const std::string& path, fileCallback clb, bool prefixdir, fileNameFilter fnf) { std::string path_ = path; mrbesen::util::insertEnd(path_, "/"); ::DIR* dir = opendir(path_.c_str()); if(!dir) { return false; } //read dir struct ::dirent* entr; while((entr = readdir(dir))) { std::string fname = entr->d_name; FileType type = (FileType) entr->d_type; //DT_BLK DT_CHR DT_DIR DT_FIFO DT_LNK DT_REG DT_SOCK DT_UNKNOWN (dirent.h, man readdir) if(fnf) { if(!fnf(fname, type)) { continue; } } if(prefixdir) fname = path_ + fname; clb(fname, type); } closedir(dir); return true; }