2014-05-12 19:59:33 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
|
2015-11-01 09:47:52 -05:00
|
|
|
#include <detail/include_windows.hpp>
|
2015-11-03 08:46:40 -05:00
|
|
|
#include <helpers/path_helper.hpp>
|
2014-05-19 20:47:15 -04:00
|
|
|
|
2016-08-03 00:12:18 -04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static std::string create_temporary_filename()
|
2014-05-12 19:59:33 -04:00
|
|
|
{
|
2015-11-03 08:46:40 -05:00
|
|
|
#ifdef _MSC_VER
|
2014-05-14 22:07:23 -04:00
|
|
|
std::array<TCHAR, MAX_PATH> buffer;
|
|
|
|
DWORD result = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
|
2016-08-03 00:12:18 -04:00
|
|
|
|
|
|
|
if (result > MAX_PATH)
|
2014-05-14 22:07:23 -04:00
|
|
|
{
|
2016-08-03 00:12:18 -04:00
|
|
|
throw std::runtime_error("buffer is too small");
|
2014-05-14 22:07:23 -04:00
|
|
|
}
|
2016-08-03 00:12:18 -04:00
|
|
|
|
|
|
|
if (result == 0)
|
2014-05-14 22:07:23 -04:00
|
|
|
{
|
2016-08-03 00:12:18 -04:00
|
|
|
throw std::runtime_error("GetTempPath failed");
|
2014-05-14 22:07:23 -04:00
|
|
|
}
|
2016-08-03 00:12:18 -04:00
|
|
|
|
|
|
|
return std::string(buffer.begin(), buffer.begin() + result) + "xlnt.xlsx";
|
2014-05-14 22:07:23 -04:00
|
|
|
#else
|
2014-06-13 11:41:32 -04:00
|
|
|
return "/tmp/xlnt.xlsx";
|
2014-05-14 22:07:23 -04:00
|
|
|
#endif
|
|
|
|
}
|
2014-05-12 19:59:33 -04:00
|
|
|
|
2016-08-03 00:12:18 -04:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class temporary_file
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
temporary_file() : path_(create_temporary_filename())
|
2014-05-12 19:59:33 -04:00
|
|
|
{
|
2016-08-03 00:12:18 -04:00
|
|
|
if(path_.exists())
|
2015-10-14 12:21:18 -04:00
|
|
|
{
|
2016-08-12 00:22:14 -04:00
|
|
|
std::remove(path_.string().c_str());
|
2015-10-14 12:21:18 -04:00
|
|
|
}
|
2014-05-12 19:59:33 -04:00
|
|
|
}
|
|
|
|
|
2016-07-20 19:54:24 -04:00
|
|
|
~temporary_file()
|
2014-05-12 19:59:33 -04:00
|
|
|
{
|
2016-08-12 00:22:14 -04:00
|
|
|
std::remove(path_.string().c_str());
|
2014-05-12 19:59:33 -04:00
|
|
|
}
|
|
|
|
|
2016-08-03 00:12:18 -04:00
|
|
|
xlnt::path get_path() const { return path_; }
|
2014-05-12 19:59:33 -04:00
|
|
|
|
|
|
|
private:
|
2016-08-03 00:12:18 -04:00
|
|
|
const xlnt::path path_;
|
2014-05-14 22:07:23 -04:00
|
|
|
};
|