summaryrefslogtreecommitdiff
path: root/test/tmpfile.h
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2024-12-23 12:14:46 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2024-12-26 15:32:45 +0100
commitc96f8afe3b3e453dcc6d4b8c9e0ae3b143e2188f (patch)
treec5b9c1162cb6086083fbe4d179cacedb5a0e9c2c /test/tmpfile.h
parent78c5477b3989d67169de2d05665adfb801caab23 (diff)
WIP
Diffstat (limited to 'test/tmpfile.h')
-rw-r--r--test/tmpfile.h31
1 files changed, 23 insertions, 8 deletions
diff --git a/test/tmpfile.h b/test/tmpfile.h
index 5d114d0..a5d4043 100644
--- a/test/tmpfile.h
+++ b/test/tmpfile.h
@@ -4,7 +4,7 @@
#pragma once
#include <cstdlib>
-#include <unistd.h>
+#include <cstring>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
@@ -12,6 +12,9 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <io.h>
+#else
+#include <unistd.h>
#endif
struct tmp_file
@@ -19,23 +22,35 @@ struct tmp_file
tmp_file(const std::string& data = {})
{
int fd;
+ auto tmp_dir = std::filesystem::temp_directory_path();
+ auto tmp_file_template = tmp_dir / "ctor_tmp_file-XXXXXX";
+ filename = tmp_file_template.string();
+ auto buf = new char[filename.size() + 2];
+ memcpy(buf, filename.data(), filename.size() + 1);
#ifdef _WIN32
- char templ[] = "ctor_tmp_file-XXXXXX"; // buffer for filename
- _mktemp_s(templ, sizeof(templ));
- fd = open(templ, O_CREAT | O_RDWR);
+ _mktemp_s(buf, filename.size() + 2);
+ filename = buf;
+ fd = _open(filename.data(), O_CREAT | O_RDWR, _S_IWRITE);
+ auto sz = _write(fd, data.data(), data.size());
+ (void)sz;
+ _close(fd);
#else
- char templ[] = "/tmp/ctor_tmp_file-XXXXXX"; // buffer for filename
- fd = mkstemp(templ);
-#endif
- filename = templ;
+ fd = mkstemp(buf);
+ filename = buf;
auto sz = write(fd, data.data(), data.size());
(void)sz;
close(fd);
+#endif
+ delete[] buf;
}
~tmp_file()
{
+#ifdef _WIN32
+ _unlink(filename.data());
+#else
unlink(filename.data());
+#endif
}
const std::string& get() const