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