xlnt/tests/helpers/path_helper.hpp

129 lines
3.0 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>
2014-05-20 08:47:15 +08:00
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>
2014-05-19 09:29:19 +08:00
#elif defined(_WIN32)
2014-05-22 05:48:51 +08:00
#define NOMINMAX
2014-05-21 22:20:30 +08:00
#include <Shlwapi.h>
2014-05-19 09:29:19 +08:00
#include <Windows.h>
2014-05-22 07:17:56 +08:00
#else
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 PathHelper
{
public:
2014-05-20 08:47:15 +08:00
static std::string WindowsToUniversalPath(const std::string &windows_path)
{
std::string fixed;
std::stringstream ss(windows_path);
std::string part;
2014-05-21 22:20:30 +08:00
2014-05-20 08:47:15 +08:00
while(std::getline(ss, part, '\\'))
{
if(fixed == "")
{
fixed = part;
}
else
{
fixed += "/" + part;
}
}
2014-05-21 22:20:30 +08:00
2014-05-20 08:47:15 +08:00
return fixed;
}
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
static std::string GetExecutableDirectory()
{
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
#ifdef __APPLE__
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
std::array<char, 1024> path;
uint32_t size = static_cast<uint32_t>(path.size());
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
if (_NSGetExecutablePath(path.data(), &size) == 0)
{
return std::string(path.begin(), std::find(path.begin(), path.end(), '\0') - 9);
}
2014-05-21 22:20:30 +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));
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
#elif defined(_WIN32)
2014-05-21 22:20:30 +08:00
2014-05-20 08:47:15 +08:00
std::array<TCHAR, MAX_PATH> buffer;
2014-05-19 09:29:19 +08:00
DWORD result = GetModuleFileName(nullptr, buffer.data(), 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");
}
2014-05-20 08:47:15 +08:00
return WindowsToUniversalPath(std::string(buffer.begin(), buffer.begin() + result - 13)) + "/";
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
#else
2014-05-22 07:17:56 +08:00
char arg1[20];
char exepath[PATH_MAX + 1] = {0};
sprintf(arg1, "/proc/%d/exe", getpid());
readlink(arg1, exepath, 1024);
return std::string(exepath).substr(0, std::strlen(exepath) - 9);
2014-05-19 09:29:19 +08:00
#endif
2014-05-21 22:20:30 +08:00
2014-05-19 09:29:19 +08:00
}
static std::string GetDataDirectory()
{
2014-05-22 05:53:17 +08:00
return GetExecutableDirectory() + "../tests/test_data";
2014-05-19 09:29:19 +08:00
}
2014-05-21 22:20:30 +08:00
static void CopyFile(const std::string &source, const std::string &destination, bool overwrite)
{
if(!overwrite && FileExists(destination))
{
throw std::runtime_error("destination file already exists and overwrite==false");
}
std::ifstream src(source, std::ios::binary);
std::ofstream dst(destination, std::ios::binary);
dst << src.rdbuf();
}
2014-06-05 06:42:17 +08:00
static void DeleteFile(const std::string &path)
{
std::remove(path.c_str());
}
2014-05-21 22:20:30 +08:00
static bool FileExists(const std::string &path)
{
#ifdef _WIN32
std::wstring path_wide(path.begin(), path.end());
return PathFileExists(path_wide.c_str()) && !PathIsDirectory(path_wide.c_str());
#else
2014-06-06 05:42:15 +08:00
try
{
struct stat fileAtt;
if (stat(path.c_str(), &fileAtt) == 0)
{
return S_ISREG(fileAtt.st_mode);
}
}
catch(...) {}
return false;
2014-05-21 22:20:30 +08:00
#endif
}
2014-05-22 07:17:56 +08:00
};