2024-06-15 17:57:33 +08:00
|
|
|
#include "FsPathPatch.hpp"
|
|
|
|
|
|
|
|
#include "EncodingHelper.hpp"
|
|
|
|
#include <string>
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
namespace YYCC::FsPathPatch {
|
|
|
|
|
2024-06-28 15:46:58 +08:00
|
|
|
std::filesystem::path FromUTF8Path(const yycc_char8_t* u8_path) {
|
2024-06-15 17:57:33 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
|
|
|
|
|
|
|
// convert path to wchar
|
|
|
|
std::wstring wpath;
|
|
|
|
if (!YYCC::EncodingHelper::UTF8ToWchar(u8_path, wpath))
|
|
|
|
throw std::invalid_argument("Fail to convert given UTF8 string.");
|
|
|
|
|
2024-06-28 15:46:58 +08:00
|
|
|
// return path with wchar_t ctor
|
2024-06-15 17:57:33 +08:00
|
|
|
return std::filesystem::path(wpath);
|
2024-06-28 15:46:58 +08:00
|
|
|
|
2024-06-15 17:57:33 +08:00
|
|
|
#else
|
2024-06-28 15:46:58 +08:00
|
|
|
return std::filesystem::path(EncodingHelper::ToNative(u8_path));
|
2024-06-15 17:57:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-06-28 15:46:58 +08:00
|
|
|
yycc_u8string ToUTF8Path(const std::filesystem::path& path) {
|
2024-06-15 17:57:33 +08:00
|
|
|
#if YYCC_OS == YYCC_OS_WINDOWS
|
|
|
|
|
|
|
|
// get and convert to utf8
|
2024-06-28 15:46:58 +08:00
|
|
|
yycc_u8string u8_path;
|
2024-06-15 17:57:33 +08:00
|
|
|
if (!YYCC::EncodingHelper::WcharToUTF8(path.c_str(), u8_path))
|
|
|
|
throw std::invalid_argument("Fail to convert to UTF8 string.");
|
|
|
|
|
|
|
|
// return utf8 path
|
|
|
|
return u8_path;
|
|
|
|
|
|
|
|
#else
|
2024-06-28 15:46:58 +08:00
|
|
|
return EncodingHelper::ToUTF8(path.string());
|
2024-06-15 17:57:33 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|