diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2024-12-23 12:14:46 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2024-12-25 21:48:03 +0100 |
commit | 3b8b519380651738a9d725353535d8141e7fa7ce (patch) | |
tree | b248f001c1acbe04137efe6b89b06f73467722b5 /test/tmpfile.h | |
parent | 78c5477b3989d67169de2d05665adfb801caab23 (diff) |
WIP
Diffstat (limited to 'test/tmpfile.h')
-rw-r--r-- | test/tmpfile.h | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/test/tmpfile.h b/test/tmpfile.h index 5d114d0..07d7a4a 100644 --- a/test/tmpfile.h +++ b/test/tmpfile.h @@ -4,7 +4,6 @@ #pragma once #include <cstdlib> -#include <unistd.h> #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN @@ -12,6 +11,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 +21,31 @@ 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(); #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(filename.data(), filename.size()); + fd = _open(filename.data(), O_CREAT | O_RDWR); + 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(filename.data()); auto sz = write(fd, data.data(), data.size()); (void)sz; close(fd); - } +#endif + std::cout << "Temporary file: " << filename << "\n"; +} ~tmp_file() { +#ifdef _WIN32 + _unlink(filename.data()); +#else unlink(filename.data()); +#endif } const std::string& get() const |