30 #ifndef BOOST_FILESYSTEM_VERSION
31 #define BOOST_FILESYSTEM_VERSION 2
33 #include <boost/filesystem.hpp>
35 #include "exceptions/WFileNotFound.h"
36 #include "exceptions/WFileOpenFailed.h"
39 std::string readFileIntoString(
const std::string& name )
41 return readFileIntoString( boost::filesystem::path( name ) );
44 std::string readFileIntoString(
const boost::filesystem::path& path )
46 std::string filename = path.file_string();
47 std::ifstream input( filename.c_str() );
48 if( !input.is_open() )
50 throw WFileNotFound( std::string(
"The file \"" ) + boost::filesystem::complete( path ).file_string() + std::string(
"\" does not exist." ) );
55 input.seekg( 0, std::ios::end );
56 str.reserve( input.tellg() );
57 input.seekg( 0, std::ios::beg );
59 str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
65 void writeStringIntoFile(
const std::string& name,
const std::string& content )
67 writeStringIntoFile( boost::filesystem::path( name ), content );
70 void writeStringIntoFile(
const boost::filesystem::path& path,
const std::string& content )
72 std::ofstream outfile( path.file_string().c_str() );
73 if( !outfile.is_open() )
75 throw WFileOpenFailed(
"The file \"" + boost::filesystem::complete( path ).file_string() +
"\" could not be opened." );
78 outfile << content << std::flush;
82 boost::filesystem::path tempFileName()
89 return boost::filesystem::path( std::string( std::tmpnam( NULL ) ) );