xlnt/tests/helpers/temporary_directory.hpp

49 lines
914 B
C++
Raw Normal View History

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>
2014-05-16 05:12:09 +08:00
2014-05-20 08:47:15 +08:00
#include "PathHelper.h"
class temporary_directory
2014-05-13 07:59:33 +08:00
{
public:
static std::string create()
2014-05-13 07:59:33 +08:00
{
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");
}
2015-11-11 08:46:57 +08:00
std::string directory(buffer.begin(), buffer.begin() + result);
return path_helper::windows_to_universal_path(directory + "xlnt");
2014-05-15 10:07:23 +08:00
#else
return "/tmp/xlsx";
#endif
2014-05-13 07:59:33 +08:00
}
temporary_directory() : filename_(create())
2014-05-13 07:59:33 +08:00
{
}
~temporary_directory()
2014-05-13 07:59:33 +08:00
{
remove(filename_.c_str());
}
std::string get_filename() const { return filename_; }
2014-05-13 07:59:33 +08:00
private:
2015-11-11 08:46:57 +08:00
const std::string filename_;
2014-05-13 07:59:33 +08:00
};