#include #include #include #include typedef char utfchar; int get_local_charset(char **charset) { char *cst = "ISO-8859-1"; *charset = cst; return 0; } static unicode_iconv_t conv_u2l = (unicode_iconv_t)0; static int local_is_utf8 = 0; static void check_conv_u2l(void){ char *charset = NULL; if (local_is_utf8 || (conv_u2l!=(unicode_iconv_t)0)) return; local_is_utf8 = get_local_charset(&charset); if (local_is_utf8) return; conv_u2l = unicode_iconv_open(charset,"UTF-8"); } extern gchar * charconv_utf8_to_local8(const utfchar *utf) { const utfchar *u = utf; int uleft = strlen(utf); gchar *local,*l,*lres; int lleft; int lost = 0; g_assert(utf); if (!utf) return NULL; /* GIGO */ check_conv_u2l(); if (local_is_utf8) return g_strdup(utf); lleft = uleft +2; l = local = g_malloc(lleft+2); *l = 0; unicode_iconv(conv_u2l,NULL,NULL,NULL,NULL); /* reset the state machines */ while ((uleft) && (lleft)) { ssize_t res = unicode_iconv(conv_u2l, &u,&uleft, &l,&lleft); *l = 0; if (res==(ssize_t)-1) { g_warning("unicode_iconv(u2l,...) failed, because '%s'", strerror(errno)); break; } else { lost += (int)res; /* lost chars in the process. */ } } lres = g_strdup(local); /* get the actual size. */ g_free(local); return lres; } utfchar *test0_data = "pépère"; // latin1 utfchar *test_data = "これは日本語のテストです"; utfchar *test2_data = "コレハハンカクカタカナノテストデス"; utfchar *test3_data = "Mélange hétérogène d'€ et de £."; int main(int argc, char **argv) { gchar *charset = NULL; get_local_charset(&charset); printf("Current charset: %s\n",charset); printf("test0=%s\ntest=%s\ntest2=%s\ntest3=%s\n", charconv_utf8_to_local8(test0_data), charconv_utf8_to_local8(test_data), charconv_utf8_to_local8(test2_data), charconv_utf8_to_local8(test3_data)); return 0; }