ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
TempFile.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <filesystem>
8#include <fstream>
9#include <sstream>
10#include <string>
11#include <vector>
12
13#ifdef _WIN32
14#include <io.h>
15#include <windows.h>
16#else
17#include <unistd.h>
18#endif
19
20namespace ppforest2::io {
28 class TempFile {
29 public:
30 TempFile(std::string const& suffix = ".json") {
31#ifdef _WIN32
32 char tmp_dir[MAX_PATH];
33 GetTempPathA(MAX_PATH, tmp_dir);
34 char tmp_file[MAX_PATH];
35 GetTempFileNameA(tmp_dir, "ppt", 0, tmp_file);
36 std::string base = tmp_file;
37 std::remove(base.c_str());
38 path_ = base + suffix;
39 std::ofstream touch(path_);
40#else
41 std::string tmpl = "/tmp/ppforest2_XXXXXX" + suffix;
42 std::vector<char> tmpl_buf(tmpl.begin(), tmpl.end());
43 tmpl_buf.push_back('\0');
44
45 int fd = mkstemps(tmpl_buf.data(), static_cast<int>(suffix.size()));
46
47 if (fd != -1) {
48 path_ = tmpl_buf.data();
49 close(fd);
50 }
51
52#endif
53 }
54
56 if (!path_.empty()) {
57 std::remove(path_.c_str());
58 }
59 }
60
61 TempFile(TempFile const&) = delete;
62 TempFile& operator=(TempFile const&) = delete;
63
64 TempFile(TempFile&& other) noexcept
65 : path_(std::move(other.path_)) {
66 other.path_.clear();
67 }
68
69 TempFile& operator=(TempFile&& other) noexcept {
70 if (this != &other) {
71 if (!path_.empty())
72 std::remove(path_.c_str());
73
74 path_ = std::move(other.path_);
75 other.path_.clear();
76 }
77
78 return *this;
79 }
80
81 std::string const& path() const { return path_; }
82
84 void clear() const { std::remove(path_.c_str()); }
85
87 std::string read() const {
88 std::ifstream in(path_);
89 std::stringstream ss;
90 ss << in.rdbuf();
91 return ss.str();
92 }
93
94 private:
95 std::string path_;
96 };
97
104 class TempDir {
105 public:
107#ifdef _WIN32
108 char tmp_dir[MAX_PATH];
109 GetTempPathA(MAX_PATH, tmp_dir);
110 char tmp_file[MAX_PATH];
111 GetTempFileNameA(tmp_dir, "ppd", 0, tmp_file);
112 // GetTempFileNameA creates a file; replace it with a directory
113 std::remove(tmp_file);
114 path_ = tmp_file;
115 std::filesystem::create_directories(path_);
116#else
117 path_ = "/tmp/ppforest2_dir_XXXXXX";
118 std::vector<char> buf(path_.begin(), path_.end());
119 buf.push_back('\0');
120 char* result = mkdtemp(buf.data());
121
122 if (result) {
123 path_ = result;
124 }
125
126#endif
127 }
128
130 if (!path_.empty()) {
131 std::filesystem::remove_all(path_);
132 }
133 }
134
135 TempDir(TempDir const&) = delete;
136 TempDir& operator=(TempDir const&) = delete;
137
138 std::string const& path() const { return path_; }
139
141 std::string file(std::string const& name) const { return (std::filesystem::path(path_) / name).string(); }
142
143 private:
144 std::string path_;
145 };
146}
std::string file(std::string const &name) const
Return a path inside this directory (file need not exist yet).
Definition TempFile.hpp:141
TempDir & operator=(TempDir const &)=delete
~TempDir()
Definition TempFile.hpp:129
TempDir(TempDir const &)=delete
std::string const & path() const
Definition TempFile.hpp:138
TempDir()
Definition TempFile.hpp:106
TempFile & operator=(TempFile const &)=delete
TempFile(std::string const &suffix=".json")
Definition TempFile.hpp:30
TempFile & operator=(TempFile &&other) noexcept
Definition TempFile.hpp:69
std::string read() const
Read the entire file contents as a string.
Definition TempFile.hpp:87
TempFile(TempFile &&other) noexcept
Definition TempFile.hpp:64
void clear() const
Remove the file so the path can be used as a fresh output target.
Definition TempFile.hpp:84
std::string const & path() const
Definition TempFile.hpp:81
~TempFile()
Definition TempFile.hpp:55
TempFile(TempFile const &)=delete
Definition Color.hpp:31