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>
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);
Create a temporary directory.
TemporaryDirectory()
Create a temporary subdirectory in the system's temp directory.
void keep(bool b)
Property: Keep directory instead of deleting it.
TemporaryDirectory(const boost::filesystem::path &name)
Create a temporary directory with the specified name.
const boost::filesystem::path & name() const
Path of temporary directory.
~TemporaryDirectory()
Recursively unlink the temporary directory.
bool keep() const
Property: Keep directory instead of deleting it.
Creates a temporary file.
std::ofstream & stream()
Output stream for temporary file.
const boost::filesystem::path & name() const
Path of temporary file.
TemporaryFile()
Create a temporary file in the system temp directory.
TemporaryFile(const boost::filesystem::path &name)
Create a temporary file with the specified name.
~TemporaryFile()
Unlink the temporary file from the filesystem.
void keep(bool b)
Property: Keep file instead of deleting it.
bool keep() const
Property: Keep file instead of deleting it.