Compare commits

...

2 Commits

Author SHA1 Message Date
mrbesen 4a12cb7394
trimOnce 2020-10-08 18:55:55 +02:00
mrbesen c709e9f358
readfile 2020-10-08 10:34:12 +02:00
7 changed files with 120 additions and 11 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

@ -24,7 +24,8 @@ bool insertStart(std::string& str, const std::string& start);
unsigned int split(const std::string& str, const std::string& token, std::string* out, unsigned int count);
void trim(std::string& str, char c = ' ');
bool trim(std::string& str, char c = ' '); //return true if the string was changed
bool trimOnce(std::string& str, char c = ' ');
std::string bytesToBase16(char* buffer, unsigned int len);
std::string bytesToBase64(char* buffer, unsigned int len);

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

@ -119,14 +119,24 @@ unsigned int mrbesen::util::split(const std::string& str, const std::string& tok
return count;
}
void mrbesen::util::trim(std::string& str, char c) {
bool mrbesen::util::trim(std::string& str, char c) {
size_t first = str.find_first_not_of(c);
if(first == std::string::npos) {
bool changed = !str.empty();
str = "";
return;
return changed;
}
size_t last = str.find_last_not_of(c)+1;
str = str.substr(first, last-first);
bool changed = (last-first) < str.size();
if(changed)
str = str.substr(first, last-first);
return changed;
}
bool mrbesen::util::trimOnce(std::string& str, char c) {
bool a = removeStart(str, std::string(1, c));
bool b = removeEnd(str, std::string(1, c));
return a || b;
}
std::string mrbesen::util::bytesToBase16(char* buffer, unsigned int len) {

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();
@ -14,6 +15,7 @@ int testUtil_start_endWith();
int testUtil_removeStart_End();
int testUtil_insertStart_End();
int testUtil_trim();
int testUtil_trimOnce();
int testUtil_base();
int testStringSpliterator();
@ -21,8 +23,8 @@ int testUtilSplit();
int testGetDoy();
test_t tests[] = {testFiles_parent, testFiles_file, testFiles_extention, testFiles_scan,
testUtil_Count, testUtil_equalsIgnoreCase, testUtil_toLower, testUtil_start_endWith, testUtil_removeStart_End, testUtil_insertStart_End, testUtil_trim, testUtil_base,
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_trimOnce, testUtil_base,
testStringSpliterator, testUtilSplit,
testGetDoy,
NULL};

View File

@ -152,19 +152,50 @@ int testUtil_insertStart_End() {
int testUtil_trim() {
std::string a = " abc def ";
trim(a);
ASSERT(trim(a), "");
ASSERT(a == "abc def", a);
trim(a, '_');
ASSERT(!trim(a, '_'), "");
ASSERT(a == "abc def", a);
trim(a, 'f');
ASSERT(trim(a, 'f'), "");
ASSERT(a == "abc de", a);
a = " ";
trim(a);
ASSERT(trim(a), "");
ASSERT(a == "", a);
trim(a);
ASSERT(!trim(a), "");
ASSERT(a == "", a);
return TESTGOOD;
}
int testUtil_trimOnce() {
std::string a = " abcd ";
ASSERT(!trimOnce(a, 'a'), "");
ASSERT(a == " abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == " abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "abcd ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "abcd", a);
ASSERT(!trimOnce(a), "");
ASSERT(a == "abcd", a);
ASSERT(!trimOnce(a, 'b'), "");
ASSERT(a == "abcd", a);
ASSERT(trimOnce(a, 'a'), "");
ASSERT(a == "bcd", a);
a = " ";
ASSERT(trimOnce(a), "");
ASSERT(a == " ", a);
ASSERT(trimOnce(a), "");
ASSERT(a == "", a);
ASSERT(!trimOnce(a), "");
ASSERT(a == "", a);
ASSERT(!trimOnce(a, 'a'), "");
ASSERT(a == "", a);
return TESTGOOD;