[Mono-bugs] [Bug 50116][Wis] New - Converting back and from utf8 with glib doesn't work?

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Sat, 25 Oct 2003 01:07:34 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by pbaena@uol.com.ar.

http://bugzilla.ximian.com/show_bug.cgi?id=50116

--- shadow/50116	2003-10-25 01:07:34.000000000 -0400
+++ shadow/50116.tmp.22160	2003-10-25 01:07:34.000000000 -0400
@@ -0,0 +1,129 @@
+Bug#: 50116
+Product: Mono/Runtime
+Version: unspecified
+OS: other
+OS Details: Debian Sid
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Wishlist
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: pbaena@uol.com.ar               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Converting back and from utf8 with glib doesn't work?
+
+This sample code should return some output. It stores a string in a 
+g_list, and retrieves it. But it doesn't with mono from cvs, but does with 
+mono 0.28 AFAIK: 
+ 
+using System; 
+using System.Runtime.InteropServices; 
+ 
+class testbug { 
+ 
+public const string GLIB_LIBRARY        = "glib-2.0"; 
+public const string STRLEN_LIBRARY      = "pango-1.0"; 
+public const string MEMMOVE_LIBRARY     = "gtk-x11-2.0"; 
+ 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int g_utf16_to_utf8(char[] str, int len, int[] 
+items_read, int[] items_written, int[] error); 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int g_utf8_to_utf16(byte[] str, int len, int[] 
+items_read, int[] items_written, int[] error); 
+ 
+[DllImport(STRLEN_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int strlen(int str); 
+ 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(int dest, int[] src, int size); 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(int dest, byte[] src, int size); 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(int[] dest, byte[] src, int size); 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(byte[] dest, int src, int size); 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(char[] dest, int src, int size); 
+[DllImport(MEMMOVE_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void memmove(int[] dest, int src, int size); 
+ 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern void g_free(int mem); 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int g_malloc(int size); 
+ 
+ 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int g_list_append(int list, int data); 
+[DllImport(GLIB_LIBRARY, CharSet = CharSet.Unicode)] 
+public static extern int g_list_nth_data(int list, int n); 
+ 
+public static readonly byte [] NullByteArray = new byte [1]; 
+public static readonly byte [] EmptyByteArray = new byte [0]; 
+public static readonly char [] EmptyCharArray = new char [0]; 
+ 
+public static void Main () 
+{ 
+	string str = "Let's i18n, baby...do it hard!"; 
+	int glist = 0; 
+	bool terminate = true; 
+	char [] strchar = str.ToCharArray(); 
+ 
+	int [] items_read = new int [1], items_written = new int [1]; 
+	int ptr = g_utf16_to_utf8 (strchar, str.Length, items_read, 
+items_written, null); 
+	if (ptr == 0) 
+		Console.WriteLine ("Maldito bastardo retorna null2"); 
+ 
+	int written = items_written [0]; 
+	//TEMPORARY CODE - convertion stops at the first NULL 
+	if (items_read [0] != strchar.Length) written++; 
+	byte [] buffer = new byte [written + (terminate ? 1 : 0)]; 
+	memmove (buffer, ptr, written); 
+	g_free (ptr); 
+ 
+	/* 
+	for (int i = 0; i < buffer.Length; i++) { 
+		Console.Write (buffer[0]); 
+	} 
+	Console.WriteLine (); 
+	*/ 
+ 
+	int data = g_malloc (buffer.Length); 
+	memmove (data, buffer, buffer.Length); 
+	glist = g_list_append (glist, data); 
+ 
+	data = g_list_nth_data (glist, 0); 
+	int length = strlen (data); 
+	byte [] buffer1 = new byte [length]; 
+	memmove (buffer1, data, length); 
+ 
+	/* 
+	for (int i = 0; i < buffer1.Length; i++) { 
+		Console.Write (buffer1[0]); 
+	} 
+	Console.WriteLine (); 
+	*/ 
+ 
+	ptr = g_utf8_to_utf16 (buffer1, buffer1.Length, null, 
+items_written, null); 
+ 
+	if (ptr == 0) { 
+		Console.WriteLine ("Maldito bastardo retorna null"); 
+	} 
+ 
+	length = items_written [0]; 
+	char [] chars = new char [length]; 
+	memmove (chars, ptr, length * 2); 
+ 
+	Console.WriteLine (chars); 
+ 
+	g_free (ptr); 
+} 
+ 
+}