OpenWalnut  1.5.0dev
WIOTools.cpp
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 #include <boost/filesystem.hpp>
30 
31 #include "exceptions/WFileNotFound.h"
32 #include "exceptions/WFileOpenFailed.h"
33 #include "WIOTools.h"
34 
35 std::string readFileIntoString( const std::string& name )
36 {
37  return readFileIntoString( boost::filesystem::path( name ) );
38 }
39 
40 std::string readFileIntoString( const boost::filesystem::path& path )
41 {
42  std::string filename = path.string();
43  std::ifstream input( filename.c_str() );
44  if( !input.is_open() )
45  {
46  throw WFileNotFound( std::string( "The file \"" ) + path.string() + std::string( "\" does not exist." ) );
47  }
48 
49  // preallocate space for the string.
50  std::string str;
51  input.seekg( 0, std::ios::end );
52  str.reserve( input.tellg() );
53  input.seekg( 0, std::ios::beg );
54 
55  str.assign( ( std::istreambuf_iterator< char >( input ) ), std::istreambuf_iterator< char >() );
56 
57  input.close();
58  return str;
59 }
60 
61 void writeStringIntoFile( const std::string& name, const std::string& content )
62 {
63  writeStringIntoFile( boost::filesystem::path( name ), content );
64 }
65 
66 void writeStringIntoFile( const boost::filesystem::path& path, const std::string& content )
67 {
68  std::ofstream outfile( path.string().c_str() );
69  if( !outfile.is_open() )
70  {
71  throw WFileOpenFailed( "The file \"" + path.string() + "\" could not be opened." );
72  }
73 
74  outfile << content << std::flush;
75  outfile.close();
76 }
77 
78 boost::filesystem::path tempFileName()
79 {
80  return tempFilename();
81 }
82 
83 boost::filesystem::path tempFilename( boost::filesystem::path model )
84 {
85  return boost::filesystem::temp_directory_path() / boost::filesystem::unique_path( model );
86 }
87 
Thrown whenever a file was not found.
Definition: WFileNotFound.h:37
Thrown whenever a file could not be opened.