ROSE  0.9.9.139
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://github.com/matzke1/sawyer.
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 <fstream>
14 
15 namespace Sawyer {
16 
18 namespace FileSystem {
19 
24 class SAWYER_EXPORT TemporaryFile {
25  boost::filesystem::path name_;
26  std::ofstream stream_;
27 
28 private:
29  // Don't depend on C++11's explicit delete of member functions
30  TemporaryFile(const TemporaryFile&) { ASSERT_not_reachable("no copy constructor"); }
31  TemporaryFile& operator=(const TemporaryFile&) { ASSERT_not_reachable("no assignment operator"); }
32 
33 public:
36  name_ = boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
37  stream_.open(name_.native().c_str());
38  }
39 
41  explicit TemporaryFile(const boost::filesystem::path &name) {
42  name_ = name;
43  stream_.open(name.native().c_str());
44  }
45 
50  stream_.close();
51  boost::filesystem::remove(name_);
52  }
53 
55  const boost::filesystem::path& name() const { return name_; }
56 
58  std::ofstream& stream() { return stream_; }
59 };
60 
65 class SAWYER_EXPORT TemporaryDirectory {
66  boost::filesystem::path name_;
67 
68 private:
69  // Don't depend on C++11's explicit delete of member functions
70  TemporaryDirectory(const TemporaryDirectory&) { ASSERT_not_reachable("no copy constructor"); }
71  TemporaryDirectory& operator=(const TemporaryDirectory&) { ASSERT_not_reachable("no assignment operator"); }
72 
73 public:
78  : name_(boost::filesystem::temp_directory_path() / boost::filesystem::unique_path()) {
79  createOrThrow();
80  }
81 
86  explicit TemporaryDirectory(const boost::filesystem::path &name)
87  : name_(name) {
88  createOrThrow();
89  }
90 
96  boost::filesystem::remove_all(name_);
97  }
98 
100  const boost::filesystem::path& name() const { return name_; }
101 
102 private:
103  // Create directory or throw exception
104  void createOrThrow() {
105  boost::system::error_code ec;
106  if (!boost::filesystem::create_directory(name_, ec))
107  throw boost::filesystem::filesystem_error("cannot create directory", name_, ec);
108  }
109 };
110 
111 } // namespace
112 } // namespace
113 
114 #endif
const boost::filesystem::path & name() const
Path of temporary directory.
std::ofstream & stream()
Output stream for temporary file.
Creates a temporary file.
TemporaryDirectory()
Create a temporary subdirectory in the system's temp directory.
Name space for the entire library.
Definition: Access.h:11
TemporaryFile(const boost::filesystem::path &name)
Create a temporary file with the specified name.
~TemporaryFile()
Unlink the temporary file from the filesystem.
const boost::filesystem::path & name() const
Path of temporary file.
TemporaryDirectory(const boost::filesystem::path &name)
Create a temporary directory with the specified name.
TemporaryFile()
Create a temporary file in the system temp directory.
Create a temporary directory.
~TemporaryDirectory()
Recursively unlink the temporary directory.