29 #include <boost/filesystem.hpp>
31 #include "exceptions/WFileNotFound.h"
32 #include "exceptions/WFileOpenFailed.h"
35 std::string readFileIntoString(
const std::string& name )
37 return readFileIntoString( boost::filesystem::path( name ) );
40 std::string readFileIntoString(
const boost::filesystem::path& path )
42 std::string filename = path.string();
43 std::ifstream input( filename.c_str() );
44 if( !input.is_open() )
46 throw WFileNotFound( std::string(
"The file \"" ) + path.string() + std::string(
"\" does not exist." ) );
51 input.seekg( 0, std::ios::end );
52 str.reserve( input.tellg() );
53 input.seekg( 0, std::ios::beg );
55 str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
61 void writeStringIntoFile(
const std::string& name,
const std::string& content )
63 writeStringIntoFile( boost::filesystem::path( name ), content );
66 void writeStringIntoFile(
const boost::filesystem::path& path,
const std::string& content )
68 std::ofstream outfile( path.string().c_str() );
69 if( !outfile.is_open() )
71 throw WFileOpenFailed(
"The file \"" + path.string() +
"\" could not be opened." );
74 outfile << content << std::flush;
78 boost::filesystem::path tempFileName()
80 return tempFilename();
83 boost::filesystem::path tempFilename( boost::filesystem::path model )
85 return boost::filesystem::temp_directory_path() / boost::filesystem::unique_path( model );
Thrown whenever a file was not found.
Thrown whenever a file could not be opened.