00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <fstream>
00026 #include <streambuf>
00027 #include <string>
00028
00029
00030 #ifndef BOOST_FILESYSTEM_VERSION
00031 #define BOOST_FILESYSTEM_VERSION 2
00032 #endif
00033 #include <boost/filesystem.hpp>
00034
00035 #include "exceptions/WFileNotFound.h"
00036 #include "exceptions/WFileOpenFailed.h"
00037 #include "WIOTools.h"
00038
00039 std::string readFileIntoString( const std::string& name )
00040 {
00041 return readFileIntoString( boost::filesystem::path( name ) );
00042 }
00043
00044 std::string readFileIntoString( const boost::filesystem::path& path )
00045 {
00046 std::string filename = path.file_string();
00047 std::ifstream input( filename.c_str() );
00048 if( !input.is_open() )
00049 {
00050 throw WFileNotFound( std::string( "The file \"" ) + boost::filesystem::complete( path ).file_string() + std::string( "\" does not exist." ) );
00051 }
00052
00053
00054 std::string str;
00055 input.seekg( 0, std::ios::end );
00056 str.reserve( input.tellg() );
00057 input.seekg( 0, std::ios::beg );
00058
00059 str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
00060
00061 input.close();
00062 return str;
00063 }
00064
00065 void writeStringIntoFile( const std::string& name, const std::string& content )
00066 {
00067 writeStringIntoFile( boost::filesystem::path( name ), content );
00068 }
00069
00070 void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content )
00071 {
00072 std::ofstream outfile( path.file_string().c_str() );
00073 if( !outfile.is_open() )
00074 {
00075 throw WFileOpenFailed( "The file \"" + boost::filesystem::complete( path ).file_string() + "\" could not be opened." );
00076 }
00077
00078 outfile << content << std::flush;
00079 outfile.close();
00080 }
00081
00082 boost::filesystem::path tempFileName()
00083 {
00084
00085
00086
00087
00088
00089 return boost::filesystem::path( std::string( std::tmpnam( NULL ) ) );
00090 }
00091