This commit is contained in:
mrbesen 2020-10-08 10:34:12 +02:00
parent 4967f73ca1
commit c709e9f358
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
4 changed files with 67 additions and 1 deletions

View File

@ -30,4 +30,7 @@ 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));
bool readFile(const std::string& name, std::ostream& output, unsigned int maxsize = 0); //reads file name and writes it line for line into output, it stops at maxsize (it might be a bit more, its not a hard limit - see it more as a hint), if maxsize is 0 it reads unlimited
bool readFile(int fd, std::ostream& output, unsigned int maxsize = 0);
}

View File

@ -3,6 +3,11 @@
#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) {
@ -78,3 +83,28 @@ bool mrbesen::files::scan(const std::string& path, fileCallback clb, bool prefix
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;
}

View File

@ -5,6 +5,12 @@
#include <list>
#include <set>
#include <string>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
using namespace mrbesen;
@ -121,3 +127,29 @@ int testFiles_scan() {
return TESTGOOD;
}
int testFiles_readFile() {
std::ostringstream buf;
ASSERT(!files::readFile("tests/blabla.exe", buf), "this file should not be read"); //try to read non existing file
bool r = files::readFile(std::string("tests/filestests.cpp"), buf);
ASSERT(r, "failed to read");
//alternative read
int fd = open("tests/filestests.cpp", O_RDONLY);
const unsigned int size = 1024*1024; //1MB
char* buf2 = new char[size];
ssize_t rc = read(fd, buf2, size);
ASSERT(rc > 0, rc);
close(fd);
//compare
ASSERT(rc == buf.tellp(), "rc: " << rc << " buf.tellp(): " << buf.tellp());
ASSERT(std::memcmp(buf2, buf.str().c_str(), rc) == 0, "")
delete[] buf2;
return TESTGOOD;
}

View File

@ -6,6 +6,7 @@ int testFiles_parent();
int testFiles_file();
int testFiles_extention();
int testFiles_scan();
int testFiles_readFile();
int testUtil_Count();
int testUtil_equalsIgnoreCase();
@ -21,7 +22,7 @@ int testUtilSplit();
int testGetDoy();
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan,
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan, testFiles_readFile,
testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_base,
testStringSpliterator, testUtilSplit,
testGetDoy,