zmike pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=0ff7469d91df09f859dffd5152dfc508759206e6

commit 0ff7469d91df09f859dffd5152dfc508759206e6
Author: Vincent Torri <[email protected]>
Date:   Tue Feb 25 09:23:48 2020 -0500

    Evil: add evil_utf8_to_utf16() API ; use evil_utf16_to_utf8() in Ecore_Win32
    
    Summary: use existing utf-16 to utf-8 conversion function in Ecore_Win32. 
Add API to convert utf-8 to utf-16 in Evil for copy'n paste on Windows
    
    Test Plan: compilation
    
    Reviewers: raster, zmike
    
    Reviewed By: zmike
    
    Subscribers: cedric, #reviewers, #committers
    
    Tags: #efl
    
    Differential Revision: https://phab.enlightenment.org/D11406
---
 src/lib/ecore_win32/ecore_win32_event.c | 35 +++++----------------------------
 src/lib/evil/evil_util.c                | 28 ++++++++++++++++++++++++++
 src/lib/evil/evil_util.h                | 18 +++++++++++++++++
 3 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/src/lib/ecore_win32/ecore_win32_event.c 
b/src/lib/ecore_win32/ecore_win32_event.c
index 293b16816b..c2e9200580 100644
--- a/src/lib/ecore_win32/ecore_win32_event.c
+++ b/src/lib/ecore_win32/ecore_win32_event.c
@@ -9,6 +9,7 @@
 #undef WIN32_LEAN_AND_MEAN
 #include <windowsx.h>
 
+#include <evil_private.h> /* evil_utf16_to_utf8() */
 #include <Eina.h>
 #include <Ecore.h>
 #include <Ecore_Input.h>
@@ -42,32 +43,6 @@ static Ecore_Win32_Key_Mask _ecore_win32_key_mask = 0;
 static Eina_Bool            _ecore_win32_ctrl_fake = EINA_FALSE;
 static Eina_Bool            _ecore_win32_clipboard_has_data = EINA_FALSE;
 
-static char *
-_ecore_win32_utf16_to_utf8(const wchar_t *text)
-{
-   char *res;
-   int size;
-
-   /* text is used as an array, hence never NULL */
-
-   size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
-   if (size == 0)
-     return NULL;
-
-   res = (char *)malloc(size * sizeof(char));
-   if (!res)
-     return NULL;
-
-   size = WideCharToMultiByte(CP_UTF8, 0, text, -1, res, size, NULL, NULL);
-   if (size == 0)
-     {
-        free(res);
-        return NULL;
-     }
-
-   return res;
-}
-
 static unsigned int
 _ecore_win32_modifiers_get(void)
 {
@@ -1202,13 +1177,13 @@ 
_ecore_win32_event_keystroke_get(Ecore_Win32_Callback_Data *msg,
            if (res == -1)
              {
                 /* dead key, but managed like normal key */
-                compose = _ecore_win32_utf16_to_utf8(buf);
+                compose = evil_utf16_to_utf8(buf);
              }
            else if (res == 0)
              {
                 INF("No translatable character found, skipping");
                 if (msg->window_param >= 0x30 && msg->window_param <= 0x39)
-                  compose = _ecore_win32_utf16_to_utf8(buf);
+                  compose = evil_utf16_to_utf8(buf);
                 /* otherwise, compose is NULL */
              }
            else if (res >= 2)
@@ -1222,11 +1197,11 @@ 
_ecore_win32_event_keystroke_get(Ecore_Win32_Callback_Data *msg,
                                 MapVirtualKey(msg->window_param, 
MAPVK_VK_TO_CHAR),
                                 kbd_state, buf, 4, 0);
                 if (!((res != 1) && (res != -1)))
-                  compose = _ecore_win32_utf16_to_utf8(buf);
+                  compose = evil_utf16_to_utf8(buf);
                 /* otherwise, compose is NULL */
              }
            else /* res == 1 : 1 char written to buf */
-             compose = _ecore_win32_utf16_to_utf8(buf);
+             compose = evil_utf16_to_utf8(buf);
 
            /*** key field ***/
 
diff --git a/src/lib/evil/evil_util.c b/src/lib/evil/evil_util.c
index c045bf3cc0..fc3c0607b2 100644
--- a/src/lib/evil/evil_util.c
+++ b/src/lib/evil/evil_util.c
@@ -107,6 +107,34 @@ evil_utf16_to_utf8(const wchar_t *text16)
    return text8;
 }
 
+wchar_t *
+evil_utf8_to_utf16(const char *text)
+{
+   wchar_t *text16;
+   DWORD flag = MB_ERR_INVALID_CHARS;
+   int size16;
+
+   if (!text)
+     return NULL;
+
+   size16 = MultiByteToWideChar(CP_UTF8, flag, text, -1, NULL, 0);
+   if (size16 == 0)
+     {
+        _evil_last_error_display(__FUNCTION__);
+        return NULL;
+     }
+
+   text16 = malloc(size16 * sizeof(wchar_t));
+   if (text16)
+     if (!MultiByteToWideChar(CP_UTF8, flag, text, -1, text16, size16))
+     {
+        _evil_last_error_display(__FUNCTION__);
+        return NULL;
+     }
+
+   return text16;
+}
+
 const char *
 evil_format_message(long err)
 {
diff --git a/src/lib/evil/evil_util.h b/src/lib/evil/evil_util.h
index 5709371f18..55b42f6823 100644
--- a/src/lib/evil/evil_util.h
+++ b/src/lib/evil/evil_util.h
@@ -59,6 +59,24 @@ EAPI char *evil_wchar_to_char(const wchar_t *text);
  */
 EAPI char *evil_utf16_to_utf8(const wchar_t *text);
 
+/**
+ * @brief Convert a string from UTF-8 to UTF-16.
+ *
+ * @param text The string to convert in UTF-8.
+ * @return The converted string in UTF-16.
+ *
+ * Convert a string from UTF-8 to UTF-16 and return it. If the
+ * allocation or conversion fails, NULL is returned. On success, the
+ * returned value must be freed when it is not used anymore.
+ *
+ * Conformity: Non applicable.
+ *
+ * @since 1.24
+ *
+ * @ingroup Evil
+ */
+EAPI wchar_t *evil_utf8_to_utf16(const char *text);
+
 EAPI const char *evil_format_message(long err);
 
 EAPI const char *evil_last_error_get(void);

-- 


Reply via email to