blob: b03a2bc96d113afdebb504cb258d8b7b78048dcf (
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
#include "scopedfile.h"
#include <cstdlib>
#include <unistd.h>
#include <cpp11fix.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
struct Pimpl
{
std::string filename;
int fd;
};
ScopedFile::ScopedFile(const std::string& data)
: pimpl(std::make_unique<struct Pimpl>())
{
#ifndef _WIN32
char templ[] = "/tmp/dg-scoped-file-XXXXXX";
pimpl->fd = mkstemp(templ);
#else
char templ[] = "dg-scoped-file-XXXXXX";
_mktemp_s(templ);
pimpl->fd = open(templ);
#endif
pimpl->filename = templ;
auto sz = write(pimpl->fd, data.data(), data.size());
(void)sz;
close(pimpl->fd);
}
ScopedFile::~ScopedFile()
{
unlink(pimpl->filename.data());
}
std::string ScopedFile::filename() const
{
return pimpl->filename;
}
|