Skip to content

Commit

Permalink
fstream: Implement read_file_contents
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Aug 6, 2015
1 parent 4c9a6b6 commit c16bb20
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/fc/io/fstream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 14 additions & 2 deletions src/io/fstream.cpp
Original file line number Diff line number Diff line change
@@ -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>
Expand Down Expand Up @@ -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

0 comments on commit c16bb20

Please sign in to comment.