diff options
author | Max Kellermann <max@duempel.org> | 2015-02-25 16:01:46 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2015-03-05 10:15:10 +0100 |
commit | 65ff72cdf8d3fc8664893b55ca47fca284f34d87 (patch) | |
tree | 766eca97378aa4567d7e55aba24c4a785f956f9d /src/fs/Charset.cxx | |
parent | 1da09563310a666095e93d3b4fdc5556a8a7c534 (diff) | |
download | mpd-65ff72cdf8d3fc8664893b55ca47fca284f34d87.tar.gz mpd-65ff72cdf8d3fc8664893b55ca47fca284f34d87.tar.xz mpd-65ff72cdf8d3fc8664893b55ca47fca284f34d87.zip |
fs/Traits: enable _UNICODE on Windows
Use wchar_t for everything on Windows. Solves a lot of filesystem
charset problems.
Diffstat (limited to 'src/fs/Charset.cxx')
-rw-r--r-- | src/fs/Charset.cxx | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/fs/Charset.cxx b/src/fs/Charset.cxx index bc6357297..b25615d42 100644 --- a/src/fs/Charset.cxx +++ b/src/fs/Charset.cxx @@ -25,6 +25,10 @@ #include "lib/icu/Converter.hxx" #include "util/Error.hxx" +#ifdef WIN32 +#include <windows.h> +#endif + #include <algorithm> #include <assert.h> @@ -95,6 +99,24 @@ PathToUTF8(PathTraitsFS::const_pointer path_fs) assert(path_fs != nullptr); #endif +#ifdef WIN32 + int length = WideCharToMultiByte(CP_UTF8, 0, path_fs, -1, nullptr, 0, + nullptr, nullptr); + if (length <= 0) + return PathTraitsUTF8::string(); + + char *buffer = new char[length]; + length = WideCharToMultiByte(CP_UTF8, 0, path_fs, -1, buffer, length, + nullptr, nullptr); + if (length <= 0) { + delete[] buffer; + return PathTraitsUTF8::string(); + } + + PathTraitsUTF8::string result(buffer); + delete[] buffer; + return FixSeparators(std::move(result)); +#else #ifdef HAVE_FS_CHARSET if (fs_converter == nullptr) #endif @@ -103,9 +125,10 @@ PathToUTF8(PathTraitsFS::const_pointer path_fs) return FixSeparators(fs_converter->ToUTF8(path_fs)); #endif +#endif } -#ifdef HAVE_FS_CHARSET +#if defined(HAVE_FS_CHARSET) || defined(WIN32) PathTraitsFS::string PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8) @@ -115,10 +138,29 @@ PathFromUTF8(PathTraitsUTF8::const_pointer path_utf8) assert(path_utf8 != nullptr); #endif +#ifdef WIN32 + int length = MultiByteToWideChar(CP_UTF8, 0, path_utf8, -1, + nullptr, 0); + if (length <= 0) + return PathTraitsFS::string(); + + wchar_t *buffer = new wchar_t[length]; + length = MultiByteToWideChar(CP_UTF8, 0, path_utf8, -1, + buffer, length); + if (length <= 0) { + delete[] buffer; + return PathTraitsFS::string(); + } + + PathTraitsFS::string result(buffer); + delete[] buffer; + return std::move(result); +#else if (fs_converter == nullptr) return path_utf8; return fs_converter->FromUTF8(path_utf8); +#endif } #endif |