From 9f86b59d1d998aef6170b9492b0487b5379e0105 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 27 Mar 2018 16:36:53 +0200 Subject: [PATCH] Add a function to get the content of a file. --- include/common/pathTools.h | 1 + src/common/pathTools.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/include/common/pathTools.h b/include/common/pathTools.h index 82f41e737..5d3c065e7 100644 --- a/include/common/pathTools.h +++ b/include/common/pathTools.h @@ -51,6 +51,7 @@ string appendToDirectory(const string& directoryPath, const string& filename); unsigned int getFileSize(const string& path); string getFileSizeAsString(const string& path); +string getFileContent(const string& path); bool fileExists(const string& path); bool makeDirectory(const string& path); bool copyFile(const string& sourcePath, const string& destPath); diff --git a/src/common/pathTools.cpp b/src/common/pathTools.cpp index 1d46bf87a..cfdc64d79 100644 --- a/src/common/pathTools.cpp +++ b/src/common/pathTools.cpp @@ -188,6 +188,20 @@ string getFileSizeAsString(const string& path) return convert.str(); } +string getFileContent(const string& path) +{ + std::ifstream f(path, std::ios::in|std::ios::ate); + std::string content; + if (f.is_open()) { + auto size = f.tellg(); + content.reserve(size); + f.seekg(0, std::ios::beg); + content.assign((std::istreambuf_iterator(f)), + std::istreambuf_iterator()); + } + return content; +} + bool fileExists(const string& path) { #ifdef _WIN32