Author: miguel
Date: 2006-10-05 19:23:31 -0400 (Thu, 05 Oct 2006)
New Revision: 66323

Modified:
   trunk/mono/eglib/ChangeLog
   trunk/mono/eglib/TODO
   trunk/mono/eglib/src/glib.h
   trunk/mono/eglib/src/gunicode.c
   trunk/mono/eglib/test/utf8.c
Log:
2006-10-05  Miguel de Icaza  <[EMAIL PROTECTED]>

        * src/gunicode.c: Some work in progress to implement g_convert
        (which is needed by g_locale_to_utf8).    



Modified: trunk/mono/eglib/ChangeLog
===================================================================
--- trunk/mono/eglib/ChangeLog  2006-10-05 21:59:46 UTC (rev 66322)
+++ trunk/mono/eglib/ChangeLog  2006-10-05 23:23:31 UTC (rev 66323)
@@ -1,3 +1,8 @@
+2006-10-05  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       * src/gunicode.c: Some work in progress to implement g_convert
+       (which is needed by g_locale_to_utf8).    
+
 2006-09-18  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * test/string-util.c (test_filename_from_uri): tests for new

Modified: trunk/mono/eglib/TODO
===================================================================
--- trunk/mono/eglib/TODO       2006-10-05 21:59:46 UTC (rev 66322)
+++ trunk/mono/eglib/TODO       2006-10-05 23:23:31 UTC (rev 66323)
@@ -20,7 +20,6 @@
              3 g_thread_init
 
         * Information retrieval
-             1 g_get_charset
              1 g_win32_getlocale
        
        * String manipulation

Modified: trunk/mono/eglib/src/glib.h
===================================================================
--- trunk/mono/eglib/src/glib.h 2006-10-05 21:59:46 UTC (rev 66322)
+++ trunk/mono/eglib/src/glib.h 2006-10-05 23:23:31 UTC (rev 66323)
@@ -644,5 +644,19 @@
 gboolean         g_markup_parse_context_end_parse (GMarkupParseContext 
*context,
                                                   GError **error);
 
+/*
+ * Character set conversion
+ */
+gboolean  g_get_charset        (char **charset);
+gchar    *g_locale_to_utf8     (const gchar *opsysstring, gssize len,
+                               gsize *bytes_read, gsize *bytes_written,
+                               GError **error);
+gchar    *g_filename_from_utf8 (const gchar *utf8string, gssize len, gsize 
*bytes_read,
+                               gsize *bytes_written, GError **error);
+gchar    *g_convert            (const gchar *str, gssize len,
+                               const gchar *to_codeset, const gchar 
*from_codeset,
+                               gsize *bytes_read, gsize *bytes_written, GError 
**error);
+
+
 #endif
 

Modified: trunk/mono/eglib/src/gunicode.c
===================================================================
--- trunk/mono/eglib/src/gunicode.c     2006-10-05 21:59:46 UTC (rev 66322)
+++ trunk/mono/eglib/src/gunicode.c     2006-10-05 23:23:31 UTC (rev 66323)
@@ -27,7 +27,13 @@
  */
 #include <stdio.h>
 #include <glib.h>
+#include <langinfo.h>
+#include <iconv.h>
+#include <errno.h>
 
+static char *my_charset;
+static gboolean is_utf8;
+
 GUnicodeType 
 g_unichar_type (gunichar c)
 {
@@ -47,8 +53,64 @@
           const gchar *to_codeset, const gchar *from_codeset,
           gsize *bytes_read, gsize *bytes_written, GError **error)
 {
-       g_error ("g_convert is not supposed in eglib");
-       return NULL;
+       iconv_t convertor;
+       char *buffer, *result, *output;
+       int str_len = len == -1 ? strlen (str) : len;
+       int buffer_size;
+       size_t left, out_left;
+       
+       convertor = iconv_open (to_codeset, from_codeset);
+       if (convertor == (iconv_t) -1){
+               *bytes_written = 0;
+               *bytes_read = 0;
+               return NULL;
+       }
+
+       buffer_size = out_left = str_len + 1;
+       buffer = g_malloc (out_left);
+       output = buffer;
+       left = str_len;
+       while (left > 0){
+               int res = iconv (convertor, (char **) &str, &left, &output, 
&out_left);
+               if (res == (size_t) -1){
+                       int out_size = buffer_size - out_left;
+                       
+                       if (errno == E2BIG){
+                               char *n;
+
+                               buffer_size += left + 2;
+                               n = g_realloc (buffer, buffer_size);
+                               
+                               if (n == NULL){
+                                       if (error != NULL)
+                                               *error = g_error_new (NULL, 
G_CONVERT_ERROR_FAILED, "No memory left");
+                                       g_free (buffer);
+                                       result = NULL;
+                                       goto leave;
+                               }
+                               buffer = n;
+                               output = buffer + out_size;
+                               out_left = buffer_size - out_size;
+                       } else if (errno == EILSEQ){
+                               if (error != NULL)
+                                       *error = g_error_new (NULL, 
G_CONVERT_ERROR_ILLEGAL_SEQUENCE, "Invalid multi-byte sequence on input");
+                               result = NULL;
+                               g_free (buffer);
+                               goto leave;
+                       } else if (errno == EINVAL){
+                               if (error != NULL)
+                                       *error = g_error_new (NULL, 
G_CONVERT_ERROR_PARTIAL_INPUT, "Partial character sequence");
+                               result = NULL;
+                               g_free (buffer);
+                               goto leave;
+                       }
+               } 
+       }
+       *output = 0;
+       result = buffer;
+ leave:
+       iconv_close (convertor);
+       return result;
 }
 
 /*
@@ -67,3 +129,25 @@
        return res;
 }
 
+gboolean
+g_get_charset (char **charset)
+{
+       if (my_charset == NULL){
+               my_charset = g_strdup (nl_langinfo (CODESET));
+               is_utf8 = strcmp (my_charset, "UTF-8") == 0;
+       }
+       
+       if (charset != NULL)
+               *charset = my_charset;
+
+       return is_utf8;
+}
+
+gchar *
+g_locale_to_utf8 (const gchar *opsysstring, gssize len, gsize *bytes_read, 
gsize *bytes_written, GError **error)
+{
+       g_get_charset (NULL);
+
+       return g_convert (opsysstring, len, "UTF-8", my_charset, bytes_read, 
bytes_written, error);
+}
+

Modified: trunk/mono/eglib/test/utf8.c
===================================================================
--- trunk/mono/eglib/test/utf8.c        2006-10-05 21:59:46 UTC (rev 66322)
+++ trunk/mono/eglib/test/utf8.c        2006-10-05 23:23:31 UTC (rev 66323)
@@ -193,7 +193,16 @@
        return OK;
 }
 
+RESULT
+test_convert ()
+{
+       gsize n;
+       char *s = g_convert ("\242", -1, "UTF-8", "ISO-8859-1", NULL, &n, NULL);
 
+       printf ("Result: %s %d\n", s, strlen (s));
+       return OK;
+}
+
 /*
  * test initialization
  */
@@ -201,6 +210,7 @@
 static Test utf8_tests [] = {
        {"g_utf16_to_utf8", test_utf16_to_utf8},
        {"g_utf8_to_utf16", test_utf8_to_utf16},
+       {"g_convert", test_convert },
        {NULL, NULL}
 };
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to