path_helper.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include <array>
  3. #include <fstream>
  4. #include <string>
  5. #include <sstream>
  6. #include <xlnt/utils/exceptions.hpp>
  7. #include <xlnt/utils/path.hpp>
  8. #define STRING_LITERAL2(a) #a
  9. #define LSTRING_LITERAL2(a) L#a
  10. #define U8STRING_LITERAL2(a) u8#a
  11. #define STRING_LITERAL(a) STRING_LITERAL2(a)
  12. #define LSTRING_LITERAL(a) STRING_LITERAL2(a)
  13. #define U8STRING_LITERAL(a) STRING_LITERAL2(a)
  14. #ifndef XLNT_BENCHMARK_DATA_DIR
  15. #define XLNT_BENCHMARK_DATA_DIR ""
  16. #endif
  17. #ifndef XLNT_SAMPLE_DATA_DIR
  18. #define XLNT_SAMPLE_DATA_DIR ""
  19. #endif
  20. class path_helper
  21. {
  22. public:
  23. static xlnt::path test_data_directory(const std::string &append = "")
  24. {
  25. static const std::string data_dir = STRING_LITERAL(XLNT_TEST_DATA_DIR);
  26. return xlnt::path(data_dir);
  27. }
  28. static xlnt::path test_file(const std::string &filename)
  29. {
  30. return test_data_directory().append(xlnt::path(filename));
  31. }
  32. static xlnt::path benchmark_data_directory(const std::string &append = "")
  33. {
  34. static const std::string data_dir = STRING_LITERAL(XLNT_BENCHMARK_DATA_DIR);
  35. return xlnt::path(data_dir);
  36. }
  37. static xlnt::path benchmark_file(const std::string &filename)
  38. {
  39. return benchmark_data_directory().append(xlnt::path(filename));
  40. }
  41. static xlnt::path sample_data_directory(const std::string &append = "")
  42. {
  43. static const std::string data_dir = STRING_LITERAL(XLNT_SAMPLE_DATA_DIR);
  44. return xlnt::path(data_dir);
  45. }
  46. static xlnt::path sample_file(const std::string &filename)
  47. {
  48. return sample_data_directory().append(xlnt::path(filename));
  49. }
  50. static void copy_file(const xlnt::path &source, const xlnt::path &destination, bool overwrite)
  51. {
  52. if(!overwrite && destination.exists())
  53. {
  54. throw xlnt::exception("destination file already exists and overwrite==false");
  55. }
  56. std::ifstream src(source.string(), std::ios::binary);
  57. std::ofstream dst(destination.string(), std::ios::binary);
  58. dst << src.rdbuf();
  59. }
  60. static void delete_file(const xlnt::path &path)
  61. {
  62. std::remove(path.string().c_str());
  63. }
  64. };