diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index 33c28ecde..b4381789a 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -54,6 +54,25 @@ #define PATH_MAX 1024 #endif + +#ifdef _WIN32 +std::string WideToUtf8(const std::wstring& wstr) +{ + auto needed_size = WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr.size(), NULL, 0, NULL, NULL); + std::string ret(needed_size, 0); + WideCharToMultiByte(CP_UTF8, 0, wstr.data(), wstr.size(), &ret[0], needed_size, NULL, NULL); + return ret; +} + +std::wstring Utf8ToWide(const std::string& str) +{ + auto needed_size = MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), NULL, 0); + std::wstring ret(needed_size, 0); + MultiByteToWideChar(CP_UTF8, 0, str.data(), str.size(), &ret[0], needed_size); + return ret; +} +#endif + bool isRelativePath(const std::string& path) { #ifdef _WIN32 @@ -77,7 +96,9 @@ std::vector normalizeParts(std::vector parts, bool abs } #endif + size_t index = 0; for (auto& part: parts) { + index++; if (part == "..") { if (absolute) { // We try to remove as far as possible. @@ -97,7 +118,7 @@ std::vector normalizeParts(std::vector parts, bool abs } if (part == "") { #ifndef _WIN32 - if (ret.empty()) { + if (ret.empty() && (absolute || index extMimeTypes = { diff --git a/test/pathTools.cpp b/test/pathTools.cpp index 6b3b48329..458d7e10e 100644 --- a/test/pathTools.cpp +++ b/test/pathTools.cpp @@ -43,6 +43,10 @@ #define A5(a, b, c, d, e) A1(P5(a, b, c, d, e)) std::vector normalizeParts(std::vector parts, bool absolute); +#ifdef _WIN32 +std::wstring Utf8ToWide(const std::string& str); +std::string WideToUtf8(const std::wstring& wstr); +#endif namespace { @@ -79,7 +83,7 @@ TEST(pathTools, normalizePartsRelative) { #define N(...) normalizeParts(__VA_ARGS__, false) ASSERT_EQ(N({}), V({})); - ASSERT_EQ(N({""}), V({""})); + ASSERT_EQ(N({""}), V({})); ASSERT_EQ(N({"a"}), V({"a"})); ASSERT_EQ(N({"a", "b"}), V({"a", "b"})); ASSERT_EQ(N({"a", "b", ".."}), V({"a"})); @@ -205,6 +209,20 @@ TEST(pathTools, dirChange) ASSERT_EQ(abs_path, p2); ASSERT_EQ(computeAbsolutePath(p1, "..\\..\\..\\..\\..\\d:\\d\\e\\foo.xml"), p2); } + +TEST(pathTools, Utf8ToWide) +{ + ASSERT_EQ(Utf8ToWide(u8""), L""); + ASSERT_EQ(Utf8ToWide(u8"test"), L"test"); + ASSERT_EQ(Utf8ToWide(u8"testé`œà"), L"testé`œà"); +} + +TEST(pathTools, WideToUtf8) +{ + ASSERT_EQ(WideToUtf8(L""), u8""); + ASSERT_EQ(WideToUtf8(L"test"), u8"test"); + ASSERT_EQ(WideToUtf8(L"testé`œà"), u8"testé`œà"); +} #endif