From 593e762e90daebeb51efad9be268e941c73bf2a3 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 18 Nov 2016 02:42:50 +0100 Subject: [PATCH] New function to read files whose size is not known beforehand (cherry picked from commit 4a9ef9cb8ddcbe075a90000c411db131ad39033b) --- stringtools.cpp | 16 ++++++++++++++++ stringtools.h | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/stringtools.cpp b/stringtools.cpp index 0d01dcc8..d1393831 100644 --- a/stringtools.cpp +++ b/stringtools.cpp @@ -218,6 +218,22 @@ std::string getFile(std::string filename) return ret; } +std::string getStreamFile(const std::string& fn) +{ + std::fstream fin(fn.c_str(), std::ios::binary | std::ios::in); + if (!fin.is_open()) + return std::string(); + + std::string ret; + while (!fin.eof()) + { + char buf[512]; + fin.read(buf, sizeof(buf)); + ret.insert(ret.end(), buf, buf + fin.gcount()); + } + return ret; +} + void strupper_utf8(std::string *pStr) { std::wstring tmp; diff --git a/stringtools.h b/stringtools.h index 93d1b364..739db082 100644 --- a/stringtools.h +++ b/stringtools.h @@ -22,7 +22,7 @@ std::string getuntilinc(std::string str,std::string data); std::string getline(int line,const std::string &str); int linecount(const std::string &str); std::string getFile(std::string filename); -std::string getFileUTF8(std::string filename); +std::string getStreamFile(const std::string& fn); std::string ExtractFileName(std::string fulln, std::string separators="/\\"); std::string ExtractFilePath(std::string fulln, std::string separators="/\\"); std::string convert(bool pBool);