ROSE 0.11.145.192
Sawyer/FileSystem.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_FileSystem_H
9#define Sawyer_FileSystem_H
10
11#include <Sawyer/Sawyer.h>
12#include <boost/filesystem.hpp>
13#include <boost/noncopyable.hpp>
14#include <fstream>
15
16namespace Sawyer {
17
19namespace FileSystem {
20
25class SAWYER_EXPORT TemporaryFile: private boost::noncopyable {
26 boost::filesystem::path name_;
27 std::ofstream stream_;
28 bool keep_;
29
30public:
32 TemporaryFile(): keep_(false) {
33 name_ = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
34 stream_.open(name_.string().c_str());
35 }
36
38 explicit TemporaryFile(const boost::filesystem::path &name): keep_(false) {
39 name_ = name;
40 stream_.open(name.string().c_str());
41 }
42
47 stream_.close();
48 if (!keep_)
49 boost::filesystem::remove(name_);
50 }
51
53 const boost::filesystem::path& name() const { return name_; }
54
56 std::ofstream& stream() { return stream_; }
57
61 bool keep() const { return keep_; }
62 void keep(bool b) { keep_ = b; }
64};
65
70class SAWYER_EXPORT TemporaryDirectory: private boost::noncopyable {
71 boost::filesystem::path name_;
72 bool keep_;
73
74public:
79 : name_(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()), keep_(false) {
80 createOrThrow();
81 }
82
87 explicit TemporaryDirectory(const boost::filesystem::path &name)
88 : name_(name), keep_(false) {
89 createOrThrow();
90 }
91
97 if (!keep_)
98 boost::filesystem::remove_all(name_);
99 }
100
102 const boost::filesystem::path& name() const { return name_; }
103
107 bool keep() const { return keep_; }
108 void keep(bool b) { keep_ = b; }
111private:
112 // Create directory or throw exception
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);
117 }
118};
119
120} // namespace
121} // namespace
122
123#endif
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.
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.
Sawyer support library.