From c16bb206a39b09b03423088c831bc193de89056c Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Tue, 4 Aug 2015 11:50:56 -0400 Subject: [PATCH] fstream: Implement read_file_contents --- include/fc/io/fstream.hpp | 7 +++++++ src/io/fstream.cpp | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/fc/io/fstream.hpp b/include/fc/io/fstream.hpp index ddf2536..8344ce9 100644 --- a/include/fc/io/fstream.hpp +++ b/include/fc/io/fstream.hpp @@ -47,4 +47,11 @@ namespace fc { fc::shared_ptr my; }; + /** + * Grab the full contents of a file into a string object. + * NB reading a full file into memory is a poor choice + * if the file may be very large. + */ + void read_file_contents( const fc::path& filename, std::string& result ); + } // namespace fc diff --git a/src/io/fstream.cpp b/src/io/fstream.cpp index 71af81d..a40bbeb 100644 --- a/src/io/fstream.cpp +++ b/src/io/fstream.cpp @@ -1,7 +1,10 @@ -#include -#include + #include +#include + +#include #include +#include #include #include @@ -93,5 +96,14 @@ namespace fc { bool ifstream::eof()const { return !my->ifs.good(); } + void read_file_contents( const fc::path& filename, std::string& result ) + { + const boost::filesystem::path& bfp = filename; + boost::filesystem::ifstream f( bfp, std::ios::in | std::ios::binary ); + // don't use fc::stringstream here as we need something with override for << rdbuf() + std::stringstream ss; + ss << f.rdbuf(); + result = ss.str(); + } } // namespace fc