fstream: Implement read_file_contents

This commit is contained in:
theoreticalbts 2015-08-04 11:50:56 -04:00
parent 4c9a6b6dab
commit c16bb206a3
2 changed files with 21 additions and 2 deletions

View file

@ -47,4 +47,11 @@ namespace fc {
fc::shared_ptr<impl> 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

View file

@ -1,7 +1,10 @@
#include <fc/io/fstream.hpp>
#include <fc/filesystem.hpp>
#include <fstream>
#include <sstream>
#include <fc/filesystem.hpp>
#include <fc/exception/exception.hpp>
#include <fc/io/fstream.hpp>
#include <fc/log/logger.hpp>
#include <boost/filesystem/path.hpp>
@ -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