xlnt/tests/helpers/path_helper.hpp

141 lines
3.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>
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:
static xlnt::string read_file(const xlnt::string &filename)
2015-10-14 12:03:48 +08:00
{
std::ifstream f(filename.data());
2015-10-14 12:03:48 +08:00
std::ostringstream ss;
ss << f.rdbuf();
return ss.str().c_str();
2015-10-14 12:03:48 +08:00
}
static xlnt::string WindowsToUniversalPath(const xlnt::string &windows_path)
2014-05-20 08:47:15 +08:00
{
xlnt::string fixed;
std::stringstream ss(windows_path.data());
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 == "")
{
fixed = part.c_str();
2014-05-20 08:47:15 +08:00
}
else
{
fixed += "/" + xlnt::string(part.c_str());
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
static xlnt::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)
{
return xlnt::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");
}
return WindowsToUniversalPath(xlnt::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 xlnt::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 xlnt::string GetDataDirectory(const xlnt::string &append = "")
2014-05-19 09:29:19 +08:00
{
return GetExecutableDirectory() + "../tests/data" + append;
2014-05-19 09:29:19 +08:00
}
2014-05-21 22:20:30 +08:00
static void CopyFile(const xlnt::string &source, const xlnt::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");
}
std::ifstream src(source.data(), std::ios::binary);
std::ofstream dst(destination.data(), std::ios::binary);
2014-05-21 22:20:30 +08:00
dst << src.rdbuf();
}
2014-06-05 06:42:17 +08:00
static void DeleteFile(const xlnt::string &path)
2014-06-05 06:42:17 +08:00
{
std::remove(path.data());
2014-06-05 06:42:17 +08:00
}
2014-05-21 22:20:30 +08:00
static bool FileExists(const xlnt::string &path)
{
#ifdef _MSC_VER
std::string path_stdstring = path.data();
std::wstring path_wide(path_stdstring.begin(), path_stdstring.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;
if (stat(path.data(), &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
};