Files
mrw-cxx/mrw/file.hpp

48 lines
1.2 KiB
C++
Raw Normal View History

2005-01-07 00:35:17 +00:00
/** @file
$Id$
$Date$
$Author$
@copy © Marc Wäckerlin
@license LGPL, see file <a href="license.html">COPYING</a>
$Log$
Revision 1.1 2005/01/07 00:31:38 marc
initial version
1 2 3 4 5 6 7 8
5678901234567890123456789012345678901234567890123456789012345678901234567890
*/
#ifndef __MRW_FILE_HPP__
#define __MRW_FILE_HPP__
#include <mrw/exception.hpp>
#include <string>
#include <fstream>
namespace mrw {
class File {
public:
static void copy(std::string from, std::string to) throw (std::exception) {
std::ifstream is(from.c_str());
if (!is)
throw mrw::invalid_argument("Cannot read file: '"+from+"'");
is.seekg(0, std::ios::end);
int size(is.tellg());
std::string contents(size, 0); // make buffer long enough to store all
is.seekg(0, std::ios::beg);
is.read(contents.begin().operator->(), size); // hack to get the buffer
if (!is.good() && is.eof())
throw mrw::invalid_argument("Cannot read file: '"+from+"'");
std::ofstream os(to.c_str());
os<<contents;
if (!os) throw mrw::invalid_argument("Cannot write file: '"+to+"'");
}
};
}
#endif