-/*
- * Allocate a string into an array of chtype's. If UTF-8 mode is
- * active, translate the string accordingly.
- */
-static NCURSES_CH_T *
-ch_dup(char *src)
-{
- unsigned len = (unsigned) strlen(src);
- NCURSES_CH_T *dst = typeMalloc(NCURSES_CH_T, len + 1);
- size_t j, k;
-#if USE_WIDEC_SUPPORT
- wchar_t wstr[CCHARW_MAX + 1];
- wchar_t wch;
- int l = 0;
- size_t rc;
- int width;
-#ifndef state_unused
- mbstate_t state;
-#endif
-#endif /* USE_WIDEC_SUPPORT */
-
-#if USE_WIDEC_SUPPORT
- reset_mbytes(state);
-#endif
- for (j = k = 0; j < len; j++) {
-#if USE_WIDEC_SUPPORT
- rc = (size_t) check_mbytes(wch, src + j, len - j, state);
- if (rc == (size_t) -1 || rc == (size_t) -2) {
- break;
- }
- j += rc - 1;
- width = wcwidth(wch);
- if (width == 0) {
- if (l == 0) {
- wstr[l++] = L' ';
- }
- } else if ((l > 0) || (l == CCHARW_MAX)) {
- wstr[l] = L'\0';
- l = 0;
- if (setcchar(dst + k, wstr, 0, 0, NULL) != OK) {
- break;
- }
- ++k;
- }
- wstr[l++] = wch;
-#else
- dst[k++] = (chtype) UChar(src[j]);
-#endif
- }
-#if USE_WIDEC_SUPPORT
- if (l > 0) {
- wstr[l] = L'\0';
- if (setcchar(dst + k, wstr, 0, 0, NULL) == OK)
- ++k;
- }
- wstr[0] = L'\0';
- setcchar(dst + k, wstr, 0, 0, NULL);
-#else
- dst[k] = 0;
-#endif
- return dst;
-}
-