blob: 4a02ad6f85a79244db43c801f874a0756fb29ccd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include "tempfile.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <iostream>
namespace fs = std::filesystem;
using namespace std::string_literals;
fs::path Reichwein::Tempfile::getPath() const
{
return m_path;
}
Reichwein::Tempfile::Tempfile(const std::filesystem::path& extension)
{
try {
fs::path path { fs::temp_directory_path() / "tempfileXXXXXX"};
if (!extension.empty())
path += extension;
fs::path::string_type name{path.native()};
int fd = mkstemps(name.data(), extension.native().size());
if (fd == -1)
std::runtime_error("mkstemps: "s + strerror(errno));
close(fd);
m_path = std::string{name};
} catch (const std::exception& ex) {
throw std::runtime_error("Tempfile error: "s + ex.what());
}
}
Reichwein::Tempfile::~Tempfile()
{
try {
fs::remove_all(m_path);
} catch (const std::exception& ex) {
std::cerr << "Warning: Couldn't remove temporary file " << m_path << std::endl;
}
}
|