diff options
Diffstat (limited to 'widgets/misc')
-rw-r--r-- | widgets/misc/e-unicode.c | 123 | ||||
-rw-r--r-- | widgets/misc/e-unicode.h | 9 |
2 files changed, 130 insertions, 2 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; diff --git a/widgets/misc/e-unicode.h b/widgets/misc/e-unicode.h index 1b51595f6c..8f0c8b646d 100644 --- a/widgets/misc/e-unicode.h +++ b/widgets/misc/e-unicode.h @@ -46,9 +46,14 @@ gchar *e_utf8_from_gtk_event_key (GtkWidget *widget, guint keyval, const gchar * gchar *e_utf8_from_gtk_string (GtkWidget *widget, const gchar *string); gchar *e_utf8_from_gtk_string_sized (GtkWidget *widget, const gchar *string, gint bytes); -gchar * e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string); -gchar * e_utf8_to_gtk_string_sized (GtkWidget *widget, const gchar *string, gint bytes); +gchar *e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string); +gchar *e_utf8_to_gtk_string_sized (GtkWidget *widget, const gchar *string, gint bytes); +gchar *e_utf8_from_locale_string (const gchar *string); +gchar *e_utf8_from_locale_string_sized (const gchar *string, gint bytes); + +gchar *e_utf8_to_locale_string (const gchar *string); +gchar *e_utf8_to_locale_string_sized (const gchar *string, gint bytes); /* * These are simple wrappers that save us some typing */ |