40 #if !defined(lint) && !defined(SCCSID)
45 #define CT_BUFSIZ ((size_t)1024)
49 ct_conv_buff_resize(ct_buffer_t *conv,
size_t mincsize,
size_t minwsize)
52 if (mincsize > conv->csize) {
53 conv->csize = mincsize;
54 p = el_realloc(conv->cbuff, conv->csize *
sizeof(*conv->cbuff));
63 if (minwsize > conv->wsize) {
64 conv->wsize = minwsize;
65 p = el_realloc(conv->wbuff, conv->wsize *
sizeof(*conv->wbuff));
77 ct_encode_string(
const Char *s, ct_buffer_t *conv)
83 memset(&state, 0,
sizeof(mbstate_t));
88 ct_conv_buff_resize(conv, CT_BUFSIZ, (
size_t)0);
94 used = (ssize_t)(conv->csize - (
size_t)(dst - conv->cbuff));
96 used = dst - conv->cbuff;
97 ct_conv_buff_resize(conv, conv->csize + CT_BUFSIZ,
101 dst = conv->cbuff + used;
103 used = ct_encode_char(dst, (
size_t)5, *s, &state);
114 ct_decode_string(
const char *s, ct_buffer_t *conv)
121 ct_conv_buff_resize(conv, (
size_t)0, CT_BUFSIZ);
125 len = ct_mbstowcs(NULL, s, (
size_t)0);
126 if (len == (
size_t)-1)
128 if (len > conv->wsize)
129 ct_conv_buff_resize(conv, (
size_t)0, len + 1);
132 ct_mbstowcs(conv->wbuff, s, conv->wsize);
138 ct_decode_argv(
int argc,
const char *argv[], ct_buffer_t *conv)
149 for (i = 0, bufspace = 0; i < argc; ++
i)
150 bufspace += argv[i] ? strlen(argv[i]) + 1 : 0;
151 ct_conv_buff_resize(conv, (
size_t)0, bufspace);
155 wargv = el_malloc((
size_t)argc *
sizeof(*wargv));
157 for (i = 0, p = conv->wbuff; i < argc; ++i) {
163 memset(&state, 0,
sizeof(mbstate_t));
164 bytes = (ssize_t)mbsrtowcs(p, argv + i, bufspace, &state);
171 bufspace -= (size_t)bytes;
187 else if (c < 0x10000)
189 else if (c < 0x110000)
196 ct_encode_char(
char *dst,
size_t len, Char c, mbstate_t *state)
200 if (len < ct_enc_width(c))
203 l = wcrtomb(dst, c, state);
206 memset (state, 0,
sizeof (mbstate_t));
213 protected const Char *
214 ct_visual_string(
const Char *s)
216 static Char *buff = NULL;
217 static size_t buffsize = 0;
225 buffsize = CT_BUFSIZ;
226 buff = el_malloc(buffsize *
sizeof(*buff));
230 used = ct_visual_char(dst, buffsize - (
size_t)(dst - buff), *s);
233 buffsize += CT_BUFSIZ;
234 p = el_realloc(buff, buffsize *
sizeof(*buff));
245 if (dst >= (buff + buffsize)) {
247 p = el_realloc(buff, buffsize *
sizeof(*buff));
251 dst = buff + buffsize - 1;
263 ct_visual_width(Char c)
265 int t = ct_chr_class(c);
267 case CHTYPE_ASCIICTL:
276 case CHTYPE_NONPRINT:
284 case CHTYPE_NONPRINT:
294 ct_visual_char(Char *dst,
size_t len, Char c)
296 int t = ct_chr_class(c);
300 case CHTYPE_ASCIICTL:
314 case CHTYPE_NONPRINT:
317 if ((ssize_t)len < ct_visual_width(c))
323 #define tohexdigit(v) "0123456789ABCDEF"[v]
325 *dst++ = tohexdigit(((
unsigned int) c >> 16) & 0xf);
326 *dst++ = tohexdigit(((
unsigned int) c >> 12) & 0xf);
327 *dst++ = tohexdigit(((
unsigned int) c >> 8) & 0xf);
328 *dst++ = tohexdigit(((
unsigned int) c >> 4) & 0xf);
329 *dst = tohexdigit(((
unsigned int) c ) & 0xf);
330 return c > 0xffff ? 8 : 7;
333 #define tooctaldigit(v) ((v) + '0')
334 *dst++ = tooctaldigit(((
unsigned int) c >> 6) & 0x7);
335 *dst++ = tooctaldigit(((
unsigned int) c >> 3) & 0x7);
336 *dst++ = tooctaldigit(((
unsigned int) c ) & 0x7);
355 else if (IsASCII(c) && Iscntrl(c))
356 return CHTYPE_ASCIICTL;
360 return CHTYPE_NONPRINT;