From 4a01c7558799e57732fe620670d6a73f1859c1b5 Mon Sep 17 00:00:00 2001 From: Christopher James Lahey Date: Tue, 6 Feb 2001 22:57:01 +0000 Subject: Check for nl_langinfo (CODESET). Code taken from glib 1.3 branch. 2001-02-06 Christopher James Lahey * configure.in: Check for nl_langinfo (CODESET). Code taken from glib 1.3 branch. * gal/widgets/e-font.c, gal/widgets/e-font.h (e_locale_encoding): Added e_locale_encoding, e_iconv_from_locale, and e_iconv_to_locale. * gal/widgets/e-unicode.c, gal/widgets/e-unicode.h: Added e_utf8_from_locale_string, e_utf8_from_locale_string_sized, e_utf8_to_locale_string, e_utf8_to_locale_string_sized. svn path=/trunk/; revision=8032 --- widgets/misc/e-unicode.c | 123 +++++++++++++++++++++++++++++++++++++++++++++++ widgets/misc/e-unicode.h | 9 +++- 2 files changed, 130 insertions(+), 2 deletions(-) (limited to 'widgets/misc') 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 @@ -354,6 +354,129 @@ e_utf8_to_gtk_string (GtkWidget *widget, const gchar *string) return e_utf8_to_gtk_string_sized (widget, string, strlen (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) { 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 */ -- cgit v1.2.3