111 lines
3.1 KiB
C++
111 lines
3.1 KiB
C++
#include "files.h"
|
|
|
|
#include <dirent.h>
|
|
#include <list>
|
|
#include <set>
|
|
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/types.h>
|
|
|
|
#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) {
|
|
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_, "/");
|
|
|
|
::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;
|
|
}
|
|
|
|
bool mrbesen::files::readFile(const std::string& name, std::ostream& output, unsigned int maxsize) {
|
|
int fd = open(name.c_str(), O_RDONLY);
|
|
bool ret = readFile(fd, output, maxsize);
|
|
close(fd);
|
|
return ret;
|
|
}
|
|
|
|
bool mrbesen::files::readFile(int fd, std::ostream& output, unsigned int maxsize) {
|
|
if(fd < 0) return false;
|
|
|
|
const int BUFFERSIZE = 1024;
|
|
ssize_t readc;
|
|
uint32_t readbytes = 0;
|
|
do {
|
|
char b[BUFFERSIZE];
|
|
readc = read(fd, b, BUFFERSIZE);
|
|
if(readc == -1) return false; //error
|
|
output << std::string(b, readc);
|
|
readbytes += readc;
|
|
} while(readc == BUFFERSIZE && (readbytes < maxsize || maxsize == 0));
|
|
|
|
return true;
|
|
}
|
|
|