// -*- 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; #ifdef _WIN32 char templ[] = "ctor_tmp_file-XXXXXX"; // buffer for filename _mktemp_s(templ, sizeof(templ)); fd = _open(templ, O_CREAT | O_RDWR); filename = templ; 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); filename = templ; auto sz = write(fd, data.data(), data.size()); (void)sz; close(fd); #endif } ~tmp_file() { #ifdef _WIN32 _unlink(filename.data()); #else unlink(filename.data()); #endif } const std::string& get() const { return filename; } private: std::string filename; };