~ubuntu-branches/ubuntu/precise/openwalnut/precise

« back to all changes in this revision

Viewing changes to src/core/common/WIOTools.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Eichelbaum
  • Date: 2011-06-21 10:26:54 UTC
  • Revision ID: james.westby@ubuntu.com-20110621102654-rq0zf436q949biih
Tags: upstream-1.2.5
ImportĀ upstreamĀ versionĀ 1.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//---------------------------------------------------------------------------
 
2
//
 
3
// Project: OpenWalnut ( http://www.openwalnut.org )
 
4
//
 
5
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
 
6
// For more information see http://www.openwalnut.org/copying
 
7
//
 
8
// This file is part of OpenWalnut.
 
9
//
 
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.
 
14
//
 
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.
 
19
//
 
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/>.
 
22
//
 
23
//---------------------------------------------------------------------------
 
24
 
 
25
#include <fstream>
 
26
#include <streambuf>
 
27
#include <string>
 
28
 
 
29
// Use filesystem version 2 for compatibility with newer boost versions.
 
30
#ifndef BOOST_FILESYSTEM_VERSION
 
31
    #define BOOST_FILESYSTEM_VERSION 2
 
32
#endif
 
33
#include <boost/filesystem.hpp>
 
34
 
 
35
#include "exceptions/WFileNotFound.h"
 
36
#include "exceptions/WFileOpenFailed.h"
 
37
#include "WIOTools.h"
 
38
 
 
39
std::string readFileIntoString( const std::string& name )
 
40
{
 
41
    return readFileIntoString( boost::filesystem::path( name ) );
 
42
}
 
43
 
 
44
std::string readFileIntoString( const boost::filesystem::path& path )
 
45
{
 
46
    std::string filename = path.file_string();
 
47
    std::ifstream input( filename.c_str() );
 
48
    if( !input.is_open() )
 
49
    {
 
50
        throw WFileNotFound( std::string( "The file \"" ) + boost::filesystem::complete( path ).file_string() + std::string( "\" does not exist." ) );
 
51
    }
 
52
 
 
53
    // preallocate space for the string.
 
54
    std::string str;
 
55
    input.seekg( 0, std::ios::end );
 
56
    str.reserve( input.tellg() );
 
57
    input.seekg( 0, std::ios::beg );
 
58
 
 
59
    str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
 
60
 
 
61
    input.close();
 
62
    return str;
 
63
}
 
64
 
 
65
void writeStringIntoFile( const std::string& name, const std::string& content )
 
66
{
 
67
    writeStringIntoFile( boost::filesystem::path( name ), content );
 
68
}
 
69
 
 
70
void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content )
 
71
{
 
72
    std::ofstream outfile( path.file_string().c_str() );
 
73
    if( !outfile.is_open() )
 
74
    {
 
75
        throw WFileOpenFailed( "The file \"" + boost::filesystem::complete( path ).file_string() + "\" could not be opened." );
 
76
    }
 
77
 
 
78
    outfile << content << std::flush;
 
79
    outfile.close();
 
80
}
 
81
 
 
82
boost::filesystem::path tempFileName()
 
83
{
 
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 ) ) );
 
90
}
 
91