#pragma once
#include <filesystem>
#include <fstream>
#include <string>
namespace whiteout::common {
inline std::filesystem::path utf8_to_path(const std::string& utf8) {
const auto* begin = reinterpret_cast<const char8_t*>(utf8.data());
return std::filesystem::path(begin, begin + utf8.size());
}
}
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
namespace whiteout::common {
inline std::wstring utf8_to_wide(const std::string& utf8) {
if (utf8.empty())
return {};
const int wlen =
::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.size()), nullptr, 0);
if (wlen <= 0)
return {};
std::wstring wide(static_cast<size_t>(wlen), L'\0');
::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), static_cast<int>(utf8.size()), wide.data(),
wlen);
return wide;
}
inline std::ifstream open_ifstream(const std::string& utf8Path,
std::ios::openmode mode = std::ios::in) {
return std::ifstream(std::filesystem::path(utf8_to_wide(utf8Path)), mode);
}
inline std::ofstream open_ofstream(const std::string& utf8Path,
std::ios::openmode mode = std::ios::out) {
return std::ofstream(std::filesystem::path(utf8_to_wide(utf8Path)), mode);
}
}
#else
namespace whiteout::common {
inline std::ifstream open_ifstream(const std::string& utf8Path,
std::ios::openmode mode = std::ios::in) {
return std::ifstream(utf8Path, mode);
}
inline std::ofstream open_ofstream(const std::string& utf8Path,
std::ios::openmode mode = std::ios::out) {
return std::ofstream(utf8Path, mode);
}
}
#endif