int _Xlcmbtowc( XLCd lcd, wchar_t *wstr, char *str, int len) { static XLCd last_lcd = NULL; static XlcConv conv = NULL; XPointer from, to; int from_left, to_left; wchar_t tmp_wc; if (lcd == NULL) { lcd = _XlcCurrentLC(); if (lcd == NULL) return -1; } if (str == NULL) return XLC_PUBLIC(lcd, is_state_depend); if (conv && lcd != last_lcd) { _XlcCloseConverter(conv); conv = NULL; } last_lcd = lcd; if (conv == NULL) { conv = _XlcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNWideChar); if (conv == NULL) return -1; } from = (XPointer) str; from_left = len; to = (XPointer) (wstr ? wstr : &tmp_wc); to_left = 1; if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) return -1; return (len - from_left); }
int _Xlcwctomb( XLCd lcd, char *str, wchar_t wc) { static XLCd last_lcd = NULL; static XlcConv conv = NULL; XPointer from, to; int from_left, to_left, length; if (lcd == NULL) { lcd = _XlcCurrentLC(); if (lcd == NULL) return -1; } if (str == NULL) return XLC_PUBLIC(lcd, is_state_depend); if (conv && lcd != last_lcd) { _XlcCloseConverter(conv); conv = NULL; } last_lcd = lcd; if (conv == NULL) { conv = _XlcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte); if (conv == NULL) return -1; } from = (XPointer) &wc; from_left = 1; to = (XPointer) str; length = to_left = XLC_PUBLIC(lcd, mb_cur_max); if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) return -1; return (length - to_left); }
/* Transforms one multibyte character, and return a 'char' in the same parsing class. Returns the number of consumed bytes in *lenp. */ static char mb_mbchar( XPointer state, const char *str, int *lenp) { XlcConv conv = ((MbState) state)->conv; const char *from; wchar_t *to, wc; int cur_max, i, from_left, to_left, ret; cur_max = XLC_PUBLIC(((MbState) state)->lcd, mb_cur_max); from = str; /* Determine from_left. Avoid overrun error which could occur if from_left > strlen(str). */ from_left = cur_max; for (i = 0; i < cur_max; i++) if (str[i] == '\0') { from_left = i; break; } *lenp = from_left; to = &wc; to_left = 1; ret = _XlcConvert(conv, (XPointer *) &from, &from_left, (XPointer *) &to, &to_left, NULL, 0); *lenp -= from_left; if (ret < 0 || to_left > 0) { /* Invalid or incomplete multibyte character seen. */ *lenp = 1; return 0x7f; } /* Return a 'char' equivalent to wc. */ return (wc >= 0 && wc <= 0x7f ? wc : 0x7f); }
int _Xlcmbstoutf8( XLCd lcd, char *ustr, const char *str, int len) { XlcConv conv; XPointer from, to; int from_left, to_left, ret; if (lcd == NULL) { lcd = _XlcCurrentLC(); if (lcd == NULL) return -1; } conv = _XlcOpenConverter(lcd, XlcNMultiByte, lcd, XlcNUtf8String); if (conv == NULL) return -1; from = (XPointer) str; from_left = strlen(str); to = (XPointer) ustr; to_left = len; if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) ret = -1; else { ret = len - to_left; if (ustr && to_left > 0) ustr[ret] = '\0'; } _XlcCloseConverter(conv); return ret; }
int _Xlcwcstombs( XLCd lcd, char *str, wchar_t *wstr, int len) { XlcConv conv; XPointer from, to; int from_left, to_left, ret; if (lcd == NULL) { lcd = _XlcCurrentLC(); if (lcd == NULL) return -1; } conv = _XlcOpenConverter(lcd, XlcNWideChar, lcd, XlcNMultiByte); if (conv == NULL) return -1; from = (XPointer) wstr; from_left = _Xwcslen(wstr); to = (XPointer) str; to_left = len; if (_XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0) < 0) ret = -1; else { ret = len - to_left; if (str && to_left > 0) str[ret] = '\0'; } _XlcCloseConverter(conv); return ret; }
static int _XTextPropertyToTextList( XLCd lcd, Display *dpy, const XTextProperty *text_prop, const char *to_type, XPointer **list_ret, int *count_ret) { XlcConv conv = NULL; const char *from_type; XPointer from, to, buf; char *str_ptr, *last_ptr; Atom encoding; int from_left, to_left, buf_len, ret, len; int unconv_num, nitems = text_prop->nitems; Bool is_wide_char = False, do_strcpy = False; if (strcmp(XlcNWideChar, to_type) == 0) is_wide_char = True; if (nitems <= 0) { *list_ret = NULL; *count_ret = 0; return Success; } if (text_prop->format != 8) return XConverterNotFound; encoding = text_prop->encoding; if (encoding == XA_STRING) from_type = XlcNString; else if (encoding == XInternAtom(dpy, "UTF8_STRING", False)) from_type = XlcNUtf8String; else if (encoding == XInternAtom(dpy, "COMPOUND_TEXT", False)) from_type = XlcNCompoundText; else if (encoding == XInternAtom(dpy, XLC_PUBLIC(lcd, encoding_name), False)) from_type = XlcNMultiByte; else return XConverterNotFound; if (is_wide_char) { buf_len = (text_prop->nitems + 1) * sizeof(wchar_t); } else { if (strcmp(to_type, XlcNUtf8String) == 0) buf_len = text_prop->nitems * 6 + 1; else buf_len = text_prop->nitems * XLC_PUBLIC(lcd, mb_cur_max) + 1; } buf = Xmalloc(buf_len); if (buf == NULL) return XNoMemory; to = buf; to_left = buf_len; /* can be XlcNMultiByte to XlcNMultiByte, or XlcNUtf8String to XlcNUtf8String */ if (!strcmp(from_type, to_type)) { do_strcpy = True; } else { conv = _XlcOpenConverter(lcd, from_type, lcd, to_type); if (conv == NULL) { Xfree(buf); return XConverterNotFound; } } last_ptr = str_ptr = (char *) text_prop->value; unconv_num = *count_ret = 0; while (1) { if (nitems == 0 || *str_ptr == 0) { from = (XPointer) last_ptr; from_left = str_ptr - last_ptr; last_ptr = str_ptr; if (do_strcpy) { len = min(from_left, to_left); strncpy(to, from, len); from += len; to += len; from_left -= len; to_left -= len; ret = 0; } else { ret = _XlcConvert(conv, &from, &from_left, &to, &to_left, NULL, 0); } if (ret < 0) continue; unconv_num += ret; (*count_ret)++; if (nitems == 0) break; last_ptr = ++str_ptr; if (is_wide_char) { *((wchar_t *)to) = (wchar_t) 0; to += sizeof(wchar_t); to_left -= sizeof(wchar_t); } else { *((char *)to) = '\0'; to++; to_left--; } if (! do_strcpy) _XlcResetConverter(conv); } else str_ptr++; nitems--; } if (! do_strcpy) _XlcCloseConverter(conv); if (is_wide_char) { *((wchar_t *) to) = (wchar_t) 0; to_left -= sizeof(wchar_t); } else { *((char *) to) = '\0'; to_left--; } *list_ret = alloc_list(is_wide_char, *count_ret, buf_len - to_left); if (*list_ret) copy_list(is_wide_char, buf, *list_ret, *count_ret); Xfree(buf); return unconv_num; }