1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
29
// Use filesystem version 2 for compatibility with newer boost versions.
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." ) );
53
// preallocate space for the string.
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()
84
// REGARDING THE COMPILER WARNING
85
// 1. mkstemp is only available for POSIX systems
86
// 2. reason: the warning generated here is due to a race condition
87
// while tmpnam invents the fileName it may be created by another process
88
// 3. file names like "/tmp/pansen" or "C:\pansen" are platform dependent
89
return boost::filesystem::path( std::string( std::tmpnam( NULL ) ) );