OpenWalnut
1.4.0
|
00001 //--------------------------------------------------------------------------- 00002 // 00003 // Project: OpenWalnut ( http://www.openwalnut.org ) 00004 // 00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS 00006 // For more information see http://www.openwalnut.org/copying 00007 // 00008 // This file is part of OpenWalnut. 00009 // 00010 // OpenWalnut is free software: you can redistribute it and/or modify 00011 // it under the terms of the GNU Lesser General Public License as published by 00012 // the Free Software Foundation, either version 3 of the License, or 00013 // (at your option) any later version. 00014 // 00015 // OpenWalnut is distributed in the hope that it will be useful, 00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 // GNU Lesser General Public License for more details. 00019 // 00020 // You should have received a copy of the GNU Lesser General Public License 00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>. 00022 // 00023 //--------------------------------------------------------------------------- 00024 00025 #include <fstream> 00026 #include <streambuf> 00027 #include <string> 00028 00029 #include <boost/filesystem.hpp> 00030 00031 #include "exceptions/WFileNotFound.h" 00032 #include "exceptions/WFileOpenFailed.h" 00033 #include "WIOTools.h" 00034 00035 std::string readFileIntoString( const std::string& name ) 00036 { 00037 return readFileIntoString( boost::filesystem::path( name ) ); 00038 } 00039 00040 std::string readFileIntoString( const boost::filesystem::path& path ) 00041 { 00042 std::string filename = path.string(); 00043 std::ifstream input( filename.c_str() ); 00044 if( !input.is_open() ) 00045 { 00046 throw WFileNotFound( std::string( "The file \"" ) + path.string() + std::string( "\" does not exist." ) ); 00047 } 00048 00049 // preallocate space for the string. 00050 std::string str; 00051 input.seekg( 0, std::ios::end ); 00052 str.reserve( input.tellg() ); 00053 input.seekg( 0, std::ios::beg ); 00054 00055 str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() ); 00056 00057 input.close(); 00058 return str; 00059 } 00060 00061 void writeStringIntoFile( const std::string& name, const std::string& content ) 00062 { 00063 writeStringIntoFile( boost::filesystem::path( name ), content ); 00064 } 00065 00066 void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content ) 00067 { 00068 std::ofstream outfile( path.string().c_str() ); 00069 if( !outfile.is_open() ) 00070 { 00071 throw WFileOpenFailed( "The file \"" + path.string() + "\" could not be opened." ); 00072 } 00073 00074 outfile << content << std::flush; 00075 outfile.close(); 00076 } 00077 00078 boost::filesystem::path tempFileName() 00079 { 00080 return tempFilename(); 00081 } 00082 00083 boost::filesystem::path tempFilename( boost::filesystem::path model ) 00084 { 00085 return boost::filesystem::temp_directory_path() / boost::filesystem::unique_path( model ); 00086 } 00087