xlnt/tests/helpers/path_helper.hpp

141 lines
3.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 PathHelper
{
public:
2015-11-11 08:46:57 +08:00
static std::string read_file(const std::string &filename)
2015-10-14 12:03:48 +08:00
{
2015-11-11 08:46:57 +08:00
std::ifstream f(filename);
2015-10-14 12:03:48 +08:00
std::ostringstream ss;
ss << f.rdbuf();
2015-11-11 08:46:57 +08:00
return ss.str();
2015-10-14 12:03:48 +08:00
}
2015-11-11 08:46:57 +08:00
static std::string WindowsToUniversalPath(const std::string &windows_path)
2014-05-20 08:47:15 +08:00
{
2015-11-11 08:46:57 +08:00
std::string fixed;
std::stringstream ss(windows_path);
2014-05-20 08:47:15 +08:00
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 == "")
{
2015-11-11 08:46:57 +08:00
fixed = part;
2014-05-20 08:47:15 +08:00
}
else
{
2015-11-11 08:46:57 +08:00
fixed += "/" + part;
2014-05-20 08:47:15 +08:00
}
}
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
2015-11-11 08:46:57 +08:00
static std::string GetExecutableDirectory()
2014-05-19 09:29:19 +08:00
{
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)
{
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-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
#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");
}
2015-11-11 08:46:57 +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());
2015-11-21 13:43:32 +08:00
auto bytes_written = readlink(arg1, exepath, 1024);
return std::string(exepath).substr(0, bytes_written - 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
}
2015-11-11 08:46:57 +08:00
static std::string GetDataDirectory(const std::string &append = "")
2014-05-19 09:29:19 +08:00
{
2016-01-17 13:07:02 +08:00
return GetExecutableDirectory() + "../../tests/data" + append;
2014-05-19 09:29:19 +08:00
}
2014-05-21 22:20:30 +08:00
2015-11-11 08:46:57 +08:00
static void CopyFile(const std::string &source, const std::string &destination, bool overwrite)
2014-05-21 22:20:30 +08:00
{
if(!overwrite && FileExists(destination))
{
throw std::runtime_error("destination file already exists and overwrite==false");
}
2015-11-11 08:46:57 +08:00
std::ifstream src(source, std::ios::binary);
std::ofstream dst(destination, std::ios::binary);
2014-05-21 22:20:30 +08:00
dst << src.rdbuf();
}
2014-06-05 06:42:17 +08:00
2015-11-11 08:46:57 +08:00
static void DeleteFile(const std::string &path)
2014-06-05 06:42:17 +08:00
{
2015-11-11 08:46:57 +08:00
std::remove(path.c_str());
2014-06-05 06:42:17 +08:00
}
2014-05-21 22:20:30 +08:00
2015-11-11 08:46:57 +08:00
static bool FileExists(const std::string &path)
{
#ifdef _MSC_VER
2015-11-11 08:46:57 +08:00
std::wstring path_wide(path.begin(), path.end());
2014-05-21 22:20:30 +08:00
return PathFileExists(path_wide.c_str()) && !PathIsDirectory(path_wide.c_str());
#else
2014-06-06 05:42:15 +08:00
try
{
struct stat fileAtt;
2015-11-11 08:46:57 +08:00
if (stat(path.c_str(), &fileAtt) == 0)
2014-06-06 05:42:15 +08:00
{
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
};