xlnt/tests/helpers/path_helper.hpp

57 lines
1.4 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
#include <detail/include_windows.hpp>
2016-10-19 07:27:26 +08:00
#include <xlnt/utils/path.hpp>
2014-05-19 09:29:19 +08:00
#ifdef __APPLE__
#include <mach-o/dyld.h>
2014-05-21 22:20:30 +08:00
#include <sys/stat.h>
#elif defined(_MSC_VER)
2014-05-21 22:20:30 +08:00
#include <Shlwapi.h>
#elif defined(__linux)
2014-05-31 06:42:25 +08:00
#include <unistd.h>
#include <linux/limits.h>
#include <sys/types.h>
2014-05-22 07:17:56 +08:00
#include <sys/stat.h>
2014-05-19 09:29:19 +08:00
#endif
#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)
class path_helper
2014-05-19 09:29:19 +08:00
{
public:
static xlnt::path 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).append(xlnt::path(append));
2014-05-19 09:29:19 +08:00
}
2014-05-21 22:20:30 +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 std::runtime_error("destination file already exists and overwrite==false");
}
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
};