xlnt/source/tests/TemporaryFile.h

49 lines
878 B
C
Raw Normal View History

2014-05-13 07:59:33 +08:00
#pragma once
#include <array>
#include <cstdio>
#include <string>
2014-05-16 05:12:09 +08:00
#ifdef _WIN32
#include <Windows.h>
#endif
2014-05-13 07:59:33 +08:00
class TemporaryFile
{
public:
static std::string CreateTemporaryFilename()
{
2014-05-15 10:07:23 +08:00
#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");
}
2014-05-16 05:12:09 +08:00
if(result == 0)
2014-05-15 10:07:23 +08:00
{
throw std::runtime_error("GetTempPath failed");
}
std::string directory(buffer.begin(), buffer.begin() + result);
2014-05-16 05:12:09 +08:00
return directory + "xlnt.xlsx";
2014-05-15 10:07:23 +08:00
#else
return "/tmp/xlsx.xlnt";
#endif
}
2014-05-13 07:59:33 +08:00
TemporaryFile() : filename_(CreateTemporaryFilename())
{
}
~TemporaryFile()
{
remove(filename_.c_str());
}
std::string GetFilename() const { return filename_; }
private:
const std::string filename_;
2014-05-15 10:07:23 +08:00
};