From 62dc8e4131c4d7784bd500edeaff93ed23807b5f Mon Sep 17 00:00:00 2001 From: Denis Krjuchkov Date: Thu, 5 Dec 2013 12:51:01 +0600 Subject: fs/Charset.cxx: replace \ with / when converting path to UTF-8 on Windows --- src/fs/Charset.cxx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index 3f373a036..dcd291a2d 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -23,9 +23,12 @@ #include "Limits.hxx" #include "system/FatalError.hxx" #include "Log.hxx" +#include "Traits.hxx" #include +#include + #include #include @@ -74,13 +77,29 @@ GetFSCharset() return fs_charset.empty() ? "utf-8" : fs_charset.c_str(); } +static inline void FixSeparators(std::string &s) +{ +#ifdef WIN32 + // For whatever reason GCC can't convert constexpr to value reference. + // This leads to link errors when passing separators directly. + auto from = PathTraitsFS::SEPARATOR; + auto to = PathTraitsUTF8::SEPARATOR; + std::replace(s.begin(), s.end(), from, to); +#else + (void)s; +#endif +} + std::string PathToUTF8(const char *path_fs) { assert(path_fs != nullptr); - if (fs_charset.empty()) - return std::string(path_fs); + if (fs_charset.empty()) { + auto result = std::string(path_fs); + FixSeparators(result); + return result; + } GIConv conv = g_iconv_open("utf-8", fs_charset.c_str()); if (conv == reinterpret_cast(-1)) @@ -101,7 +120,9 @@ PathToUTF8(const char *path_fs) if (ret == static_cast(-1) || in_left > 0) return std::string(); - return std::string(path_utf8, sizeof(path_utf8) - out_left); + auto result_path = std::string(path_utf8, sizeof(path_utf8) - out_left); + FixSeparators(result_path); + return result_path; } char * -- cgit v1.2.3