mirror of
https://github.com/tfussell/xlnt.git
synced 2024-03-22 13:11:17 +08:00
55 lines
1.0 KiB
C++
55 lines
1.0 KiB
C++
#pragma once
|
|
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
#ifdef _WIN32
|
|
#define NOMINMAX
|
|
#include <Windows.h>
|
|
#endif
|
|
|
|
#include "path_helper.hpp"
|
|
|
|
class TemporaryFile
|
|
{
|
|
public:
|
|
static std::string CreateTemporaryFilename()
|
|
{
|
|
#ifdef _WIN32
|
|
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 PathHelper::WindowsToUniversalPath(directory + "xlnt.xlsx");
|
|
#else
|
|
return "/tmp/xlnt.xlsx";
|
|
#endif
|
|
}
|
|
|
|
TemporaryFile() : filename_(CreateTemporaryFilename())
|
|
{
|
|
if(PathHelper::FileExists(GetFilename()))
|
|
{
|
|
std::remove(filename_.c_str());
|
|
}
|
|
}
|
|
|
|
~TemporaryFile()
|
|
{
|
|
std::remove(filename_.c_str());
|
|
}
|
|
|
|
std::string GetFilename() const { return filename_; }
|
|
|
|
private:
|
|
const std::string filename_;
|
|
};
|