Example #1
0
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;
}
Example #2
0
/* Sets the state to the initial state.
   Initiates a sequence of calls to mbchar. */
static void
mb_mbinit(
    XPointer state)
{
    _XlcResetConverter(((MbState) state)->conv);
}