From c709e9f3582989da778dabab0dc376bf30ab3eef Mon Sep 17 00:00:00 2001 From: mrbesen Date: Thu, 8 Oct 2020 10:34:12 +0200 Subject: [PATCH] readfile --- inc/files.h | 3 +++ src/files.cpp | 30 ++++++++++++++++++++++++++++++ tests/filestests.cpp | 32 ++++++++++++++++++++++++++++++++ tests/main.cpp | 3 ++- 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/inc/files.h b/inc/files.h index 1931498..2a6a518 100644 --- a/inc/files.h +++ b/inc/files.h @@ -30,4 +30,7 @@ template bool scan(const std::string& path, std::insert_iterator 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); + } \ No newline at end of file diff --git a/src/files.cpp b/src/files.cpp index 2134f8a..ab2b02c 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -3,6 +3,11 @@ #include #include #include + +#include +#include +#include + #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; +} + diff --git a/tests/filestests.cpp b/tests/filestests.cpp index 8f1ef19..377c5aa 100644 --- a/tests/filestests.cpp +++ b/tests/filestests.cpp @@ -5,6 +5,12 @@ #include #include #include +#include + +#include + +#include +#include 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; +} diff --git a/tests/main.cpp b/tests/main.cpp index 79c311f..7384665 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -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,