xlnt/tests/helpers/path_helper.hpp

80 lines
2.1 KiB
C++
Raw Normal View History

2014-05-19 09:29:19 +08:00
#pragma once
2014-05-20 08:47:15 +08:00
#include <array>
2014-05-21 22:20:30 +08:00
#include <fstream>
2015-10-14 12:03:48 +08:00
#include <string>
#include <sstream>
2014-05-20 08:47:15 +08:00
2017-09-28 05:35:35 +08:00
#include <xlnt/utils/exceptions.hpp>
2016-10-19 07:27:26 +08:00
#include <xlnt/utils/path.hpp>
#define STRING_LITERAL2(a) #a
#define LSTRING_LITERAL2(a) L#a
#define U8STRING_LITERAL2(a) u8#a
#define STRING_LITERAL(a) STRING_LITERAL2(a)
#define LSTRING_LITERAL(a) STRING_LITERAL2(a)
#define U8STRING_LITERAL(a) STRING_LITERAL2(a)
#ifndef XLNT_BENCHMARK_DATA_DIR
#define XLNT_BENCHMARK_DATA_DIR ""
#endif
#ifndef XLNT_SAMPLE_DATA_DIR
#define XLNT_SAMPLE_DATA_DIR ""
#endif
class path_helper
2014-05-19 09:29:19 +08:00
{
public:
static xlnt::path test_data_directory(const std::string &append = "")
2014-05-19 09:29:19 +08:00
{
static const std::string data_dir = STRING_LITERAL(XLNT_TEST_DATA_DIR);
return xlnt::path(data_dir);
}
static xlnt::path test_file(const std::string &filename)
{
return test_data_directory().append(xlnt::path(filename));
}
static xlnt::path benchmark_data_directory(const std::string &append = "")
{
static const std::string data_dir = STRING_LITERAL(XLNT_BENCHMARK_DATA_DIR);
return xlnt::path(data_dir);
}
static xlnt::path benchmark_file(const std::string &filename)
{
return benchmark_data_directory().append(xlnt::path(filename));
}
static xlnt::path sample_data_directory(const std::string &append = "")
{
static const std::string data_dir = STRING_LITERAL(XLNT_SAMPLE_DATA_DIR);
return xlnt::path(data_dir);
}
static xlnt::path sample_file(const std::string &filename)
{
return sample_data_directory().append(xlnt::path(filename));
2014-05-19 09:29:19 +08:00
}
2016-08-03 12:12:18 +08:00
static void copy_file(const xlnt::path &source, const xlnt::path &destination, bool overwrite)
2014-05-21 22:20:30 +08:00
{
2016-08-03 12:12:18 +08:00
if(!overwrite && destination.exists())
2014-05-21 22:20:30 +08:00
{
throw xlnt::exception("destination file already exists and overwrite==false");
2014-05-21 22:20:30 +08:00
}
std::ifstream src(source.string(), std::ios::binary);
std::ofstream dst(destination.string(), std::ios::binary);
2014-05-21 22:20:30 +08:00
dst << src.rdbuf();
}
2014-06-05 06:42:17 +08:00
2016-08-03 12:12:18 +08:00
static void delete_file(const xlnt::path &path)
2014-06-05 06:42:17 +08:00
{
std::remove(path.string().c_str());
2014-05-21 22:20:30 +08:00
}
2014-05-22 07:17:56 +08:00
};