Alexandre Julliard : winex11: Add a helper function to export a Unicode string.

Alexandre Julliard julliard at winehq.org
Tue Sep 27 11:08:51 CDT 2016


Module: wine
Branch: master
Commit: 8493fc782b8e183bb0a1b777a791d68d5433b23b
URL:    http://source.winehq.org/git/wine.git/?a=commit;h=8493fc782b8e183bb0a1b777a791d68d5433b23b

Author: Alexandre Julliard <julliard at winehq.org>
Date:   Tue Sep 27 21:24:25 2016 +0900

winex11: Add a helper function to export a Unicode string.

Signed-off-by: Alexandre Julliard <julliard at winehq.org>

---

 dlls/winex11.drv/clipboard.c | 97 +++++++++++++++++++-------------------------
 1 file changed, 41 insertions(+), 56 deletions(-)

diff --git a/dlls/winex11.drv/clipboard.c b/dlls/winex11.drv/clipboard.c
index 983ee58..9f78e3d 100644
--- a/dlls/winex11.drv/clipboard.c
+++ b/dlls/winex11.drv/clipboard.c
@@ -1095,6 +1095,39 @@ static BOOL export_data( Display *display, Window win, Atom prop, Atom target, H
 
 
 /**************************************************************************
+ *		string_from_unicode_text
+ *
+ * Convert CF_UNICODETEXT data to a string in the specified codepage.
+ */
+static char *string_from_unicode_text( UINT codepage, HANDLE handle, UINT *size )
+{
+    UINT i, j;
+    char *str;
+    WCHAR *strW = GlobalLock( handle );
+    UINT lenW = GlobalSize( handle ) / sizeof(WCHAR);
+    DWORD len = WideCharToMultiByte( codepage, 0, strW, lenW, NULL, 0, NULL, NULL );
+
+    if ((str = HeapAlloc( GetProcessHeap(), 0, len )))
+    {
+        WideCharToMultiByte( codepage, 0, strW, lenW, str, len, NULL, NULL);
+        GlobalUnlock( handle );
+
+        /* remove carriage returns */
+        for (i = j = 0; i < len; i++)
+        {
+            if (str[i] == '\r' && (i == len - 1 || str[i + 1] == '\n')) continue;
+            str[j++] = str[i];
+        }
+        if (j && !str[j - 1]) j--;  /* remove trailing null */
+        *size = j;
+        TRACE( "returning %s\n", debugstr_an( str, j ));
+    }
+    GlobalUnlock( handle );
+    return str;
+}
+
+
+/**************************************************************************
  *		export_string
  *
  *  Export CF_TEXT converting the string to XA_STRING.
@@ -1135,38 +1168,12 @@ static BOOL export_string( Display *display, Window win, Atom prop, Atom target,
  */
 static BOOL export_utf8_string( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
 {
-    UINT i, j;
     UINT size;
-    LPSTR text, lpstr;
-    LPWSTR uni_text = GlobalLock( handle );
-
-    size = WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, NULL, 0, NULL, NULL);
-    text = HeapAlloc(GetProcessHeap(), 0, size);
-    if (!text)
-    {
-        GlobalUnlock( handle );
-        return FALSE;
-    }
+    char *text = string_from_unicode_text( CP_UTF8, handle, &size );
 
-    WideCharToMultiByte(CP_UTF8, 0, uni_text, -1, text, size, NULL, NULL);
-    GlobalUnlock( handle );
-
-    /* remove carriage returns */
-    lpstr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size--);
-    if (lpstr == NULL)
-    {
-        HeapFree(GetProcessHeap(), 0, text);
-        return FALSE;
-    }
-    for (i = 0,j = 0; i < size && text[i]; i++)
-    {
-        if (text[i] == '\r' && (text[i+1] == '\n' || text[i+1] == '\0'))
-            continue;
-        lpstr[j++] = text[i];
-    }
-    put_property( display, win, prop, target, 8, lpstr, j );
-    HeapFree(GetProcessHeap(), 0, lpstr);
-    HeapFree(GetProcessHeap(), 0, text);
+    if (!text) return FALSE;
+    put_property( display, win, prop, target, 8, text, size );
+    HeapFree( GetProcessHeap(), 0, text );
     GlobalUnlock( handle );
     return TRUE;
 }
@@ -1179,47 +1186,25 @@ static BOOL export_utf8_string( Display *display, Window win, Atom prop, Atom ta
  */
 static BOOL export_compound_text( Display *display, Window win, Atom prop, Atom target, HANDLE handle )
 {
-    char* lpstr;
     XTextProperty textprop;
     XICCEncodingStyle style;
-    UINT i, j;
     UINT size;
-    LPWSTR uni_text = GlobalLock( handle );
-
-    size = WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, NULL, 0, NULL, NULL);
-    lpstr = HeapAlloc(GetProcessHeap(), 0, size);
-    if (!lpstr)
-    {
-        GlobalUnlock( handle );
-        return FALSE;
-    }
-
-    WideCharToMultiByte(CP_UNIXCP, 0, uni_text, -1, lpstr, size, NULL, NULL);
-
-    /* remove carriage returns */
-    for (i = 0, j = 0; i < size && lpstr[i]; i++)
-    {
-        if (lpstr[i] == '\r' && (lpstr[i+1] == '\n' || lpstr[i+1] == '\0'))
-            continue;
-        lpstr[j++] = lpstr[i];
-    }
-    lpstr[j]='\0';
-
-    GlobalUnlock( handle );
+    char *text = string_from_unicode_text( CP_UNIXCP, handle, &size );
 
+    if (!text) return FALSE;
     if (target == x11drv_atom(COMPOUND_TEXT))
         style = XCompoundTextStyle;
     else
         style = XStdICCTextStyle;
 
     /* Update the X property */
-    if (XmbTextListToTextProperty( display, &lpstr, 1, style, &textprop ) == Success)
+    if (XmbTextListToTextProperty( display, &text, 1, style, &textprop ) == Success)
     {
         XSetTextProperty( display, win, &textprop, prop );
         XFree( textprop.value );
     }
 
-    HeapFree(GetProcessHeap(), 0, lpstr);
+    HeapFree( GetProcessHeap(), 0, text );
     return TRUE;
 }
 




More information about the wine-cvs mailing list