diff options
Diffstat (limited to 'test/tmpfile.h')
-rw-r--r-- | test/tmpfile.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/test/tmpfile.h b/test/tmpfile.h new file mode 100644 index 0000000..0f83a20 --- /dev/null +++ b/test/tmpfile.h @@ -0,0 +1,38 @@ +// -*- c++ -*- +// Distributed under the BSD 2-Clause License. +// See accompanying file LICENSE for details. +#pragma once + +#include <cstdio> + +class TmpFile +{ +public: + TmpFile(const std::string& data = {}) + { + auto tmp_dir = std::filesystem::temp_directory_path(); + auto tmp_file_template = tmp_dir / "ctor_tmp_file-"; + std::FILE* fp{nullptr}; + int counter{}; + while(!fp) + { + filename = tmp_file_template.string() + std::to_string(counter++); + fp = std::fopen(filename.data(), "wx"); + } + std::fwrite(data.data(), data.size(), 1, fp); + std::fclose(fp); + } + + ~TmpFile() + { + std::filesystem::remove(filename); + } + + const std::string& get() const + { + return filename; + } + +private: + std::string filename; +}; |