From 3b17ff7c866cf6b27ce82dbe6a8f87df1310069c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 10 Jun 2019 15:44:45 +0200 Subject: [PATCH] Add functions to convert wide char to UTF-8 There was already utf8_to_wide_char(), used to correctly execute commands on Windows. Add the reverse converter: utf8_from_wide_char(). We will need it to build the scrcpy-server path based on the executable directory. --- app/src/str_util.c | 16 ++++++++++++++++ app/src/str_util.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/app/src/str_util.c b/app/src/str_util.c index 2878bf96..7d46a1a0 100644 --- a/app/src/str_util.c +++ b/app/src/str_util.c @@ -92,4 +92,20 @@ utf8_to_wide_char(const char *utf8) { return wide; } +char * +utf8_from_wide_char(const wchar_t *ws) { + int len = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL); + if (!len) { + return NULL; + } + + char *utf8 = SDL_malloc(len); + if (!utf8) { + return NULL; + } + + WideCharToMultiByte(CP_UTF8, 0, ws, -1, utf8, len, NULL, NULL); + return utf8; +} + #endif diff --git a/app/src/str_util.h b/app/src/str_util.h index 0d1b9c01..0b7a571a 100644 --- a/app/src/str_util.h +++ b/app/src/str_util.h @@ -32,6 +32,9 @@ utf8_truncation_index(const char *utf8, size_t max_len); // returns the new allocated string, to be freed by the caller wchar_t * utf8_to_wide_char(const char *utf8); + +char * +utf8_from_wide_char(const wchar_t *s); #endif #endif