temporary_directory.hpp 908 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include <array>
  3. #include <cstdio>
  4. #include <string>
  5. #include <detail/include_windows.hpp>
  6. #include "PathHelper.h"
  7. class temporary_directory
  8. {
  9. public:
  10. static std::string create()
  11. {
  12. #ifdef _WIN32
  13. std::array<TCHAR, MAX_PATH> buffer;
  14. DWORD result = GetTempPath(static_cast<DWORD>(buffer.size()), buffer.data());
  15. if(result > MAX_PATH)
  16. {
  17. throw xlnt::exception("buffer is too small");
  18. }
  19. if(result == 0)
  20. {
  21. throw xlnt::exception("GetTempPath failed");
  22. }
  23. std::string directory(buffer.begin(), buffer.begin() + result);
  24. return path_helper::windows_to_universal_path(directory + "xlnt");
  25. #else
  26. return "/tmp/xlsx";
  27. #endif
  28. }
  29. temporary_directory() : filename_(create())
  30. {
  31. }
  32. ~temporary_directory()
  33. {
  34. remove(filename_.c_str());
  35. }
  36. std::string get_filename() const { return filename_; }
  37. private:
  38. const std::string filename_;
  39. };