xlnt/tests/helpers/path_helper.hpp

84 lines
2.3 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>
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
class path_helper
2014-05-19 09:29:19 +08:00
{
public:
2016-08-03 12:12:18 +08:00
static xlnt::path get_executable_directory()
2015-10-14 12:03:48 +08:00
{
2014-05-19 09:29:19 +08:00
#ifdef __APPLE__
std::array<char, 1024> path;
uint32_t size = static_cast<uint32_t>(path.size());
2014-05-19 09:29:19 +08:00
if (_NSGetExecutablePath(path.data(), &size) == 0)
{
2015-11-11 08:46:57 +08:00
return std::string(path.begin(), std::find(path.begin(), path.end(), '\0') - 9);
2014-05-19 09:29:19 +08:00
}
2014-05-19 09:29:19 +08:00
throw std::runtime_error("buffer too small, " + std::to_string(path.size()) + ", should be: " + std::to_string(size));
#elif defined(_MSC_VER)
2014-05-21 22:20:30 +08:00
2014-05-20 08:47:15 +08:00
std::array<TCHAR, MAX_PATH> buffer;
2014-06-11 05:12:15 +08:00
DWORD result = GetModuleFileName(nullptr, buffer.data(), (DWORD)buffer.size());
2014-05-21 22:20:30 +08:00
2014-05-20 08:47:15 +08:00
if(result == 0 || result == buffer.size())
2014-05-19 09:29:19 +08:00
{
throw std::runtime_error("GetModuleFileName failed or buffer was too small");
}
2016-08-03 12:12:18 +08:00
return xlnt::path(std::string(buffer.begin(), buffer.begin() + result - 13));
2014-05-19 09:29:19 +08:00
#else
char arg1[20];
char exepath[PATH_MAX + 1] = {0};
sprintf(arg1, "/proc/%d/exe", getpid());
auto bytes_written = readlink(arg1, exepath, 1024);
2016-08-03 21:00:58 +08:00
return xlnt::path(std::string(exepath).substr(0, bytes_written - 9));
2016-07-20 10:28:12 +08:00
#endif
}
2014-05-19 09:29:19 +08:00
2016-08-03 12:12:18 +08:00
static xlnt::path get_data_directory(const std::string &append = "")
2014-05-19 09:29:19 +08:00
{
2016-08-03 12:12:18 +08:00
return xlnt::path("../../tests/data")
.make_absolute(get_executable_directory())
.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");
}
2016-08-03 12:12:18 +08:00
std::ifstream src(source.to_string(), std::ios::binary);
std::ofstream dst(destination.to_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
{
2016-08-03 12:12:18 +08:00
std::remove(path.to_string().c_str());
2014-05-21 22:20:30 +08:00
}
2014-05-22 07:17:56 +08:00
};