From ed6ff7ca9a18ca6bf416156849f27172d7726054 Mon Sep 17 00:00:00 2001 From: Davide Beatrici Date: Sat, 10 Aug 2019 02:05:00 +0200 Subject: plugins: convert UTF-16 to UTF-8 using C++11 features --- plugins/mumble_plugin_utils.h | 8 ++++++++ plugins/ut99/ut99.cpp | 13 ++----------- 2 files changed, 10 insertions(+), 11 deletions(-) (limited to 'plugins') diff --git a/plugins/mumble_plugin_utils.h b/plugins/mumble_plugin_utils.h index 0f7742f87..307dc76ff 100644 --- a/plugins/mumble_plugin_utils.h +++ b/plugins/mumble_plugin_utils.h @@ -6,6 +6,14 @@ #ifndef MUMBLE_MUMBLE_PLUGIN_UTILS_H_ #define MUMBLE_MUMBLE_PLUGIN_UTILS_H_ +#include +#include + +static inline std::string utf16ToUtf8(const std::wstring &wstr) { + std::wstring_convert> conv; + return conv.to_bytes(wstr); +} + // escape lossily converts the given // string to ASCII, replacing any // character not within the printable diff --git a/plugins/ut99/ut99.cpp b/plugins/ut99/ut99.cpp index 827ff9310..9b5a11a00 100644 --- a/plugins/ut99/ut99.cpp +++ b/plugins/ut99/ut99.cpp @@ -35,20 +35,12 @@ */ #include "../mumble_plugin_main.h" +#include "../mumble_plugin_utils.h" procptr_t posptr; procptr_t frtptr; procptr_t topptr; -static void wcsToMultibyteStdString(wchar_t *wcs, std::string &str) { - const int size = WideCharToMultiByte(CP_UTF8, 0, wcs, -1, NULL, 0, NULL, NULL); - if (size == 0) return; - - str.resize(size); - - WideCharToMultiByte(CP_UTF8, 0, wcs, -1, &str[0], size, NULL, NULL); -} - static bool cross(float *a, float *b, float *c) { if (a == 0 || b == 0 || c == 0) return false; @@ -161,8 +153,7 @@ static int fetch(float *avatar_pos, float *avatar_front, float *avatar_top, floa wservername[sizeof(wservername)/sizeof(wservername[0]) - 1] = '\0'; - std::string servername; - wcsToMultibyteStdString(wservername, servername); + const std::string servername = utf16ToUtf8(wservername); std::ostringstream contextss; contextss << "{" -- cgit v1.2.3