xlnt/tests/helpers/temporary_file.hpp
2016-07-20 19:54:24 -04:00

54 lines
1.0 KiB
C++

#pragma once
#include <array>
#include <cstdio>
#include <string>
#include <detail/include_windows.hpp>
#include <helpers/path_helper.hpp>
class temporary_file
{
public:
static std::string create()
{
#ifdef _MSC_VER
std::array<TCHAR, MAX_PATH> buffer;
DWORD result = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
if(result > MAX_PATH)
{
throw std::runtime_error("buffer is too small");
}
if(result == 0)
{
throw std::runtime_error("GetTempPath failed");
}
std::string directory(buffer.begin(), buffer.begin() + result);
return path_helper::windows_to_universal_path(directory + "xlnt.xlsx");
#else
return "/tmp/xlnt.xlsx";
#endif
}
temporary_file() : filename_(create())
{
if(path_helper::file_exists(get_filename()))
{
std::remove(filename_.c_str());
}
}
~temporary_file()
{
std::remove(filename_.c_str());
}
std::string get_filename() const { return filename_; }
private:
const std::string filename_;
};