80 lines
2.2 KiB
C++
80 lines
2.2 KiB
C++
#include "files.h"
|
|
|
|
#include <dirent.h>
|
|
#include <list>
|
|
#include <set>
|
|
#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<class Container>
|
|
bool mrbesen::Files::scan(const std::string& path, std::insert_iterator<Container> it, 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;
|
|
it = fname;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|