30 explicit TempFile(std::string
const& suffix =
".json") {
33 char tmp_dir[MAX_PATH];
34 GetTempPathA(MAX_PATH, tmp_dir);
35 char tmp_file[MAX_PATH];
36 GetTempFileNameA(tmp_dir,
"ppforest2", 0, tmp_file);
38 path_ = sentinel_ + suffix;
39 std::ofstream touch(path_);
41 std::string tmpl =
"/tmp/ppforest2_XXXXXX" + suffix;
42 std::vector<char> tmpl_buf(tmpl.begin(), tmpl.end());
43 tmpl_buf.push_back(
'\0');
45 int fd = mkstemps(tmpl_buf.data(),
static_cast<int>(suffix.size()));
48 path_ = tmpl_buf.data();
57 std::remove(path_.c_str());
62 if (!sentinel_.empty()) {
63 std::remove(sentinel_.c_str());
73 : path_(std::move(other.path_)) {
77 sentinel_ = std::move(other.sentinel_);
78 other.sentinel_.clear();
86 std::remove(path_.c_str());
91 if (!sentinel_.empty()) {
92 std::remove(sentinel_.c_str());
94 sentinel_ = std::move(other.sentinel_);
95 other.sentinel_.clear();
99 path_ = std::move(other.path_);
106 std::string
const&
path()
const {
return path_; }
109 void clear()
const { std::remove(path_.c_str()); }
113 std::ifstream in(path_);
114 std::stringstream ss;
123 std::string sentinel_;
140 char tmp_dir[MAX_PATH];
141 GetTempPathA(MAX_PATH, tmp_dir);
142 char tmp_file[MAX_PATH];
143 GetTempFileNameA(tmp_dir,
"ppd", 0, tmp_file);
145 std::remove(tmp_file);
147 std::filesystem::create_directories(path_);
151 path_ =
"/tmp/ppforest2_dir_XXXXXX";
152 std::vector<char> buf(path_.begin(), path_.end());
154 char* result = mkdtemp(buf.data());
156 if (result !=
nullptr) {
165 if (!path_.empty()) {
166 std::filesystem::remove_all(path_);
173 std::string
const&
path()
const {
return path_; }
176 std::string
file(std::string
const& name)
const {
return (std::filesystem::path(path_) / name).string(); }
std::string file(std::string const &name) const
Return a path inside this directory (file need not exist yet).
Definition TempFile.hpp:176
TempDir & operator=(TempDir const &)=delete
~TempDir()
Definition TempFile.hpp:164
TempDir(TempDir const &)=delete
std::string const & path() const
Definition TempFile.hpp:173
TempDir()
Definition TempFile.hpp:136
TempFile & operator=(TempFile const &)=delete
TempFile(std::string const &suffix=".json")
Definition TempFile.hpp:30
TempFile & operator=(TempFile &&other) noexcept
Definition TempFile.hpp:83
std::string read() const
Read the entire file contents as a string.
Definition TempFile.hpp:112
TempFile(TempFile &&other) noexcept
Definition TempFile.hpp:72
void clear() const
Remove the file so the path can be used as a fresh output target.
Definition TempFile.hpp:109
std::string const & path() const
Definition TempFile.hpp:106
~TempFile()
Definition TempFile.hpp:55
TempFile(TempFile const &)=delete