1 ////////////////////////////////////////////////////////////////////////////////
3 static iconv_t icFromLoc
;
4 static iconv_t icToLoc
;
5 static int needConversion
= 0;
6 static const char *cliLocale
= NULL
;
9 static void initLCConversion (void) {
10 const char *lct
= setlocale(LC_CTYPE
, NULL
);
14 if (cliLocale
== NULL
) {
15 if (strrchr(lct
, '.') != NULL
) lct
= strrchr(lct
, '.')+1;
19 if (strcasecmp(lct
, "utf8") == 0 || strcasecmp(lct
, "utf-8") == 0) return;
20 //fprintf(stderr, "locale: [%s]\n", lct);
21 icFromLoc
= iconv_open("UTF-8", lct
);
22 if (icFromLoc
== (iconv_t
)-1) k8t_die("can't initialize locale conversion");
23 cstr
= SPrintf("%s//TRANSLIT", lct
);
24 icToLoc
= iconv_open(cstr
, "UTF-8");
26 if (icToLoc
== (iconv_t
)-1) k8t_die("can't initialize locale conversion");
31 static int loc2utf (char *dest
, const char *src
, int len
) {
40 il
= iconv(icFromLoc
, &ibuf
, &il
, &obuf
, &ol
);
41 if (il
== (size_t)-1) return 0;
44 if (len
> 0) memmove(dest
, src
, len
);
50 static int utf2loc (char *dest
, const char *src
, int len
) {
59 il
= iconv(icToLoc
, &ibuf
, &il
, &obuf
, &ol
);
60 if (il
== (size_t)-1) return 0;
63 if (len
> 0) memmove(dest
, src
, len
);
69 static int loc2utfCB (K8Term
*term
, char *dest
, const char *src
, int len
) {
70 return loc2utf(dest
, src
, len
);
74 static int utf2locCB (K8Term
*term
, char *dest
, const char *src
, int len
) {
75 return utf2loc(dest
, src
, len
);
79 static int isConversionNecessaryCB (K8Term
*term
) {
80 return needConversion
;