2014-05-13 07:59:33 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
2015-11-01 22:47:52 +08:00
|
|
|
#include <detail/include_windows.hpp>
|
2015-11-03 21:46:40 +08:00
|
|
|
#include <helpers/path_helper.hpp>
|
2014-05-20 08:47:15 +08:00
|
|
|
|
2014-05-13 07:59:33 +08:00
|
|
|
class TemporaryFile
|
|
|
|
{
|
|
|
|
public:
|
2015-11-05 07:45:03 +08:00
|
|
|
static xlnt::string CreateTemporaryFilename()
|
2014-05-13 07:59:33 +08:00
|
|
|
{
|
2015-11-03 21:46:40 +08:00
|
|
|
#ifdef _MSC_VER
|
2014-05-15 10:07:23 +08:00
|
|
|
std::array<TCHAR, MAX_PATH> buffer;
|
|
|
|
DWORD result = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
|
2015-11-03 21:46:40 +08:00
|
|
|
|
2014-05-15 10:07:23 +08:00
|
|
|
if(result > MAX_PATH)
|
|
|
|
{
|
|
|
|
throw std::runtime_error("buffer is too small");
|
|
|
|
}
|
2015-11-03 21:46:40 +08:00
|
|
|
|
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");
|
|
|
|
}
|
2015-11-03 21:46:40 +08:00
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
xlnt::string directory(buffer.begin(), buffer.begin() + result);
|
2015-11-03 21:46:40 +08:00
|
|
|
|
2014-05-20 08:47:15 +08:00
|
|
|
return PathHelper::WindowsToUniversalPath(directory + "xlnt.xlsx");
|
2014-05-15 10:07:23 +08:00
|
|
|
#else
|
2014-06-13 23:41:32 +08:00
|
|
|
return "/tmp/xlnt.xlsx";
|
2014-05-15 10:07:23 +08:00
|
|
|
#endif
|
|
|
|
}
|
2014-05-13 07:59:33 +08:00
|
|
|
|
|
|
|
TemporaryFile() : filename_(CreateTemporaryFilename())
|
|
|
|
{
|
2015-10-15 00:21:18 +08:00
|
|
|
if(PathHelper::FileExists(GetFilename()))
|
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
std::remove(filename_.data());
|
2015-10-15 00:21:18 +08:00
|
|
|
}
|
2014-05-13 07:59:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
~TemporaryFile()
|
|
|
|
{
|
2015-11-05 07:45:03 +08:00
|
|
|
std::remove(filename_.data());
|
2014-05-13 07:59:33 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 07:45:03 +08:00
|
|
|
xlnt::string GetFilename() const { return filename_; }
|
2014-05-13 07:59:33 +08:00
|
|
|
|
|
|
|
private:
|
2015-11-05 07:45:03 +08:00
|
|
|
const xlnt::string filename_;
|
2014-05-15 10:07:23 +08:00
|
|
|
};
|