// -*- c++ -*- // Distributed under the BSD 2-Clause License. // See accompanying file LICENSE for details. #pragma once #include #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #else #include #endif 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 _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 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 { return filename; } private: std::string filename; };