ROSE  0.11.96.11
util/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 <boost/noncopyable.hpp>
14 #include <fstream>
15 
16 namespace Sawyer {
17 
19 namespace FileSystem {
20 
25 class SAWYER_EXPORT TemporaryFile: private boost::noncopyable {
26  boost::filesystem::path name_;
27  std::ofstream stream_;
28  bool keep_;
29 
30 public:
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 
70 class SAWYER_EXPORT TemporaryDirectory: private boost::noncopyable {
71  boost::filesystem::path name_;
72  bool keep_;
73 
74 public:
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; }
111 private:
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
Sawyer::FileSystem::TemporaryDirectory
Create a temporary directory.
Definition: util/Sawyer/FileSystem.h:70
Sawyer::FileSystem::TemporaryFile::TemporaryFile
TemporaryFile()
Create a temporary file in the system temp directory.
Definition: util/Sawyer/FileSystem.h:32
Sawyer::FileSystem::TemporaryFile::keep
void keep(bool b)
Property: Keep file instead of deleting it.
Definition: util/Sawyer/FileSystem.h:62
Sawyer::FileSystem::TemporaryDirectory::name
const boost::filesystem::path & name() const
Path of temporary directory.
Definition: util/Sawyer/FileSystem.h:102
Sawyer::FileSystem::TemporaryFile::TemporaryFile
TemporaryFile(const boost::filesystem::path &name)
Create a temporary file with the specified name.
Definition: util/Sawyer/FileSystem.h:38
Sawyer::FileSystem::TemporaryDirectory::keep
bool keep() const
Property: Keep directory instead of deleting it.
Definition: util/Sawyer/FileSystem.h:107
Sawyer::FileSystem::TemporaryDirectory::TemporaryDirectory
TemporaryDirectory(const boost::filesystem::path &name)
Create a temporary directory with the specified name.
Definition: util/Sawyer/FileSystem.h:87
Sawyer::FileSystem::TemporaryDirectory::~TemporaryDirectory
~TemporaryDirectory()
Recursively unlink the temporary directory.
Definition: util/Sawyer/FileSystem.h:96
Sawyer::FileSystem::TemporaryFile::keep
bool keep() const
Property: Keep file instead of deleting it.
Definition: util/Sawyer/FileSystem.h:61
Sawyer::FileSystem::TemporaryFile::stream
std::ofstream & stream()
Output stream for temporary file.
Definition: util/Sawyer/FileSystem.h:56
Sawyer::FileSystem::TemporaryFile::~TemporaryFile
~TemporaryFile()
Unlink the temporary file from the filesystem.
Definition: util/Sawyer/FileSystem.h:46
Sawyer
Name space for the entire library.
Definition: Access.h:13
Sawyer::FileSystem::TemporaryDirectory::TemporaryDirectory
TemporaryDirectory()
Create a temporary subdirectory in the system's temp directory.
Definition: util/Sawyer/FileSystem.h:78
Sawyer::FileSystem::TemporaryFile
Creates a temporary file.
Definition: util/Sawyer/FileSystem.h:25
Sawyer::FileSystem::TemporaryFile::name
const boost::filesystem::path & name() const
Path of temporary file.
Definition: util/Sawyer/FileSystem.h:53
Sawyer::FileSystem::TemporaryDirectory::keep
void keep(bool b)
Property: Keep directory instead of deleting it.
Definition: util/Sawyer/FileSystem.h:108