Author: ArcRiley
Date: 2009-01-09 04:33:57 -0500 (Fri, 09 Jan 2009)
New Revision: 1450

Modified:
   trunk/concordance/src/utils.c
   trunk/concordance/src/utils.h
Log:
added conPyUnicodeToUTF8 to convert either UCS2 or UCS4 to UTF8


Modified: trunk/concordance/src/utils.c
===================================================================
--- trunk/concordance/src/utils.c       2009-01-09 04:44:30 UTC (rev 1449)
+++ trunk/concordance/src/utils.c       2009-01-09 09:33:57 UTC (rev 1450)
@@ -19,7 +19,7 @@
 # $Id$
 */
 
-#include "glib.h"
+#include "utils.h"
 
 GIOChannel*
 conAddSocket(gint fd, GIOCondition condition, GSourceFunc callback,
@@ -91,3 +91,38 @@
   /* return either the found attribute value or NULL */
   return attrs[i+1];
 }
+
+gchar*
+conPyUnicodeToUTF8(PyObject* unicode) {                                   /*\
+  cdef :                                                                  \*/
+    PY_UNICODE_TYPE* uni;
+    Py_ssize_t       len;
+
+  if (!unicode)
+    return NULL;
+
+  /* return Python unicode object converted to UTF8 gchar*
+
+     Returned gchar* must be freed with g_free().
+
+     Py_UNICODE*        PyUnicode_AsUnicode      (PyObject *unicode);
+     Py_ssize_t         PyUnicode_GetSize        (PyObject *unicode);
+     gchar*             g_utf16_to_utf8          (const gunichar2 *str,
+                                                  glong len,
+                                                  glong *items_read,
+                                                  glong *items_written,
+                                                  GError **error);
+     gchar*             g_ucs4_to_utf8           (const gunichar *str,
+                                                  glong len,
+                                                  glong *items_read,
+                                                  glong *items_written,
+                                                  GError **error);
+  */
+  uni = PyUnicode_AsUnicode(unicode);
+  len = PyUnicode_GetSize(unicode);
+  #if Py_UNICODE_SIZE == 2
+    return g_utf16_to_utf8((gunichar*) uni, len, NULL, NULL, NULL);
+  #else
+    return g_ucs4_to_utf8((gunichar*) uni, len, NULL, NULL, NULL);
+  #endif
+}

Modified: trunk/concordance/src/utils.h
===================================================================
--- trunk/concordance/src/utils.h       2009-01-09 04:44:30 UTC (rev 1449)
+++ trunk/concordance/src/utils.h       2009-01-09 09:33:57 UTC (rev 1450)
@@ -19,8 +19,20 @@
 # $Id$
 */
 
-GIOChannel* conAddSocket(gint fd, GIOCondition condition, GSourceFunc callback,
-                         gpointer data, GMainContext* context);
-gint conAddWatch(GIOChannel* channel, GIOCondition condition, 
-                 GSourceFunc callback, gpointer data, GMainContext* context);
-const gchar* conSearchAttributes(const gchar**, const gchar*);
+#ifndef CONUTILS_H
+#define CONUTILS_H
+
+#include "Python.h"
+#include "glib.h"
+
+GIOChannel*   conAddSocket             (gint fd, GIOCondition condition,
+                                        GSourceFunc callback, gpointer data,
+                                        GMainContext* context);
+gint          conAddWatch              (GIOChannel* channel,
+                                        GIOCondition condition, 
+                                        GSourceFunc callback, gpointer data,
+                                        GMainContext* context);
+const gchar*  conSearchAttributes      (const gchar**, const gchar*);
+gchar*        conPyUnicodeToUTF8       (PyObject* unicode);
+
+#endif

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to