aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/misc/e-unicode.c
diff options
context:
space:
mode:
Diffstat (limited to 'widgets/misc/e-unicode.c')
-rw-r--r--widgets/misc/e-unicode.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/widgets/misc/e-unicode.c b/widgets/misc/e-unicode.c
index 8de74fa842..0cb889e323 100644
--- a/widgets/misc/e-unicode.c
+++ b/widgets/misc/e-unicode.c
@@ -355,6 +355,129 @@ e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string)
}
gchar *
+e_utf8_from_locale_string_sized (const gchar *string, gint bytes)
+{
+ iconv_t ic;
+ char *new, *ob;
+ gchar * ib;
+ size_t ibl, obl;
+
+ if (!string) return NULL;
+
+ ic = e_iconv_from_locale ();
+ if (ic == (iconv_t) -1) {
+ gint i;
+ /* iso-8859-1 */
+ ib = (char *) string;
+ new = ob = g_new (unsigned char, bytes * 2 + 1);
+ for (i = 0; i < (bytes); i ++) {
+ ob += g_unichar_to_utf8 (ib[i], ob);
+ }
+ *ob = '\0';
+ return new;
+ }
+
+ ib = (char *) string;
+ ibl = bytes;
+ new = ob = g_new (gchar, ibl * 6 + 1);
+ obl = ibl * 6 + 1;
+
+ while (ibl > 0) {
+ iconv (ic, &ib, &ibl, &ob, &obl);
+ if (ibl > 0) {
+ gint len;
+ if ((*ib & 0x80) == 0x00) len = 1;
+ else if ((*ib &0xe0) == 0xc0) len = 2;
+ else if ((*ib &0xf0) == 0xe0) len = 3;
+ else if ((*ib &0xf8) == 0xf0) len = 4;
+ else {
+ g_warning ("Invalid UTF-8 sequence");
+ break;
+ }
+ ib += len;
+ ibl = bytes - (ib - string);
+ if (ibl > bytes) ibl = 0;
+ *ob++ = '_';
+ obl--;
+ }
+ }
+
+ *ob = '\0';
+
+ return new;
+}
+
+gchar *
+e_utf8_from_locale_string (const gchar *string)
+{
+ return e_utf8_from_locale_string_sized (string, strlen (string));
+}
+
+gchar *
+e_utf8_to_locale_string_sized (const gchar *string, gint bytes)
+{
+ iconv_t ic;
+ char *new, *ob;
+ gchar * ib;
+ size_t ibl, obl;
+
+ if (!string) return NULL;
+
+ ic = e_iconv_to_locale ();
+ if (ic == (iconv_t) -1) {
+ gint len;
+ const gchar *u;
+ unicode_char_t uc;
+
+ new = g_new (unsigned char, bytes * 4 + 1);
+ u = string;
+ len = 0;
+
+ while ((u) && (u - string < bytes)) {
+ u = unicode_get_utf8 (u, &uc);
+ new[len++] = uc & 0xff;
+ }
+ new[len] = '\0';
+ return new;
+ }
+
+ ib = (char *) string;
+ ibl = bytes;
+ new = ob = g_new (gchar, ibl * 4 + 1);
+ obl = ibl * 4 + 1;
+
+ while (ibl > 0) {
+ iconv (ic, &ib, &ibl, &ob, &obl);
+ if (ibl > 0) {
+ gint len;
+ if ((*ib & 0x80) == 0x00) len = 1;
+ else if ((*ib &0xe0) == 0xc0) len = 2;
+ else if ((*ib &0xf0) == 0xe0) len = 3;
+ else if ((*ib &0xf8) == 0xf0) len = 4;
+ else {
+ g_warning ("Invalid UTF-8 sequence");
+ break;
+ }
+ ib += len;
+ ibl = bytes - (ib - string);
+ if (ibl > bytes) ibl = 0;
+ *ob++ = '_';
+ obl--;
+ }
+ }
+
+ *ob = '\0';
+
+ return new;
+}
+
+gchar *
+e_utf8_to_locale_string (const gchar *string)
+{
+ return e_utf8_to_locale_string_sized (string, strlen (string));
+}
+
+gchar *
e_utf8_gtk_entry_get_text (GtkEntry *entry)
{
gchar *s, *u;