8 #ifndef Sawyer_FileSystem_H
9 #define Sawyer_FileSystem_H
11 #include <Sawyer/Sawyer.h>
12 #include <boost/filesystem.hpp>
13 #include <boost/noncopyable.hpp>
19 namespace FileSystem {
26 boost::filesystem::path name_;
27 std::ofstream stream_;
33 name_ = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
34 stream_.open(name_.string().c_str());
38 explicit TemporaryFile(
const boost::filesystem::path &name): keep_(false) {
40 stream_.open(name.string().c_str());
49 boost::filesystem::remove(name_);
53 const boost::filesystem::path&
name()
const {
return name_; }
56 std::ofstream&
stream() {
return stream_; }
61 bool keep()
const {
return keep_; }
62 void keep(
bool b) { keep_ = b; }
71 boost::filesystem::path name_;
79 : name_(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()), keep_(false) {
88 : name_(name), keep_(false) {
98 boost::filesystem::remove_all(name_);
102 const boost::filesystem::path&
name()
const {
return name_; }
107 bool keep()
const {
return keep_; }
108 void keep(
bool b) { keep_ = b; }
113 void createOrThrow() {
114 boost::system::error_code ec;
115 if (!boost::filesystem::create_directory(name_, ec))
116 throw boost::filesystem::filesystem_error(
"cannot create directory", name_, ec);