OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
WIOTools.cpp
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 // Use filesystem version 2 for compatibility with newer boost versions.
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     // preallocate space for the string.
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     // REGARDING THE COMPILER WARNING
00085     // 1. mkstemp is only available for POSIX systems
00086     // 2. reason: the warning generated here is due to a race condition
00087     //    while tmpnam invents the fileName it may be created by another process
00088     // 3. file names like "/tmp/pansen" or "C:\pansen" are platform dependent
00089     return boost::filesystem::path( std::string( std::tmpnam( NULL ) ) );
00090 }
00091 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends