… | |
… | |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
36 | char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); |
37 | |
37 | |
38 | char *dst = r; |
38 | char *dst = r; |
39 | while (len--) |
39 | while (len--) |
40 | { |
40 | { |
41 | int l = wcrtomb (dst, *str++, mbs); |
41 | ssize_t l = wcrtomb (dst, *str++, mbs); |
42 | if (l < 0) |
42 | if (l < 0) |
43 | *dst++ = '?'; |
43 | *dst++ = '?'; |
44 | else |
44 | else |
45 | dst += l; |
45 | dst += l; |
46 | } |
46 | } |
47 | |
47 | |
48 | *dst++ = 0; |
48 | *dst++ = 0; |
49 | |
49 | |
50 | return r; |
50 | return (char *)rxvt_realloc (r, dst - r); |
51 | } |
51 | } |
52 | |
52 | |
53 | wchar_t * |
53 | wchar_t * |
54 | rxvt_mbstowcs (const char *str, int len) |
54 | rxvt_mbstowcs (const char *str, int len) |
55 | { |
55 | { |
56 | if (len < 0) len = strlen (str); |
56 | if (len < 0) len = strlen (str); |
57 | |
57 | |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
58 | wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); |
59 | |
59 | |
60 | if (mbstowcs (r, str, len + 1) < 0) |
60 | if ((ssize_t)mbstowcs (r, str, len + 1) < 0) |
61 | *r = 0; |
61 | *r = 0; |
62 | |
62 | |
63 | return r; |
63 | return r; |
64 | } |
64 | } |
65 | |
65 | |
… | |
… | |
91 | *p++ = 0x80 | ( w & 0x3f); |
91 | *p++ = 0x80 | ( w & 0x3f); |
92 | else |
92 | else |
93 | *p++ = '?'; |
93 | *p++ = '?'; |
94 | } |
94 | } |
95 | |
95 | |
96 | *p = 0; |
96 | *p++ = 0; |
97 | |
97 | |
98 | return r; |
98 | return (char *)rxvt_realloc (r, p - r); |
99 | } |
99 | } |
100 | |
100 | |
101 | wchar_t * |
101 | wchar_t * |
102 | rxvt_utf8towcs (const char *str, int len) |
102 | rxvt_utf8towcs (const char *str, int len) |
103 | { |
103 | { |
… | |
… | |
282 | /* INTPROTO */ |
282 | /* INTPROTO */ |
283 | char * |
283 | char * |
284 | rxvt_Str_trim (char *str) |
284 | rxvt_Str_trim (char *str) |
285 | { |
285 | { |
286 | char *r, *s; |
286 | char *r, *s; |
287 | int n; |
|
|
288 | |
287 | |
289 | if (!str || !*str) /* shortcut */ |
288 | if (!str || !*str) /* shortcut */ |
290 | return str; |
289 | return str; |
291 | |
290 | |
292 | /* skip leading spaces */ |
291 | /* skip leading spaces */ |
293 | for (s = str; *s && isspace (*s); s++) ; |
292 | for (s = str; *s && isspace (*s); s++) ; |
|
|
293 | |
294 | /* goto end of string */ |
294 | /* goto end of string */ |
295 | for (n = 0, r = s; *r++; n++) ; |
295 | r = s + strlen (s) - 1; |
296 | r -= 2; |
296 | |
297 | /* dump return */ |
297 | /* dump return and other trailing whitespace */ |
298 | if (n > 0 && *r == '\n') |
298 | while (r > s && isspace (*r)) |
299 | n--, r--; |
299 | r--; |
300 | /* backtrack along trailing spaces */ |
300 | |
301 | for (; n > 0 && isspace (*r); r--, n--) ; |
301 | #if 0 |
302 | /* skip matching leading/trailing quotes */ |
302 | /* skip matching leading/trailing quotes */ |
303 | if (*s == '"' && *r == '"' && n > 1) |
303 | if (*s == '"' && *r == '"' && n > 1) |
304 | { |
304 | { |
305 | s++; |
305 | s++; |
306 | n -= 2; |
306 | n -= 2; |
307 | } |
307 | } |
|
|
308 | #endif |
308 | |
309 | |
309 | /* copy back over: forwards copy */ |
310 | memmove (str, s, r + 1 - s); |
310 | for (r = str; n; n--) |
311 | str[r + 1 - s] = 0; |
311 | *r++ = *s++; |
|
|
312 | *r = '\0'; |
|
|
313 | |
312 | |
314 | return str; |
313 | return str; |
315 | } |
314 | } |
316 | |
315 | |
317 | /* |
316 | /* |
… | |
… | |
400 | return (d - str); |
399 | return (d - str); |
401 | } |
400 | } |
402 | |
401 | |
403 | /* |
402 | /* |
404 | * Split a comma-separated string into an array, stripping leading and |
403 | * Split a comma-separated string into an array, stripping leading and |
405 | * trailing spaces (and paired quotes) from each entry. Empty strings |
404 | * trailing spaces from each entry. Empty strings are properly returned |
406 | * are properly returned |
|
|
407 | * Caller should free each entry and array when done |
405 | * Caller should free each entry and array when done |
408 | */ |
406 | */ |
409 | /* INTPROTO */ |
407 | /* INTPROTO */ |
410 | char ** |
408 | char ** |
411 | rxvt_splitcommastring (const char *cs) |
409 | rxvt_splitcommastring (const char *cs) |
… | |
… | |
418 | s = ""; |
416 | s = ""; |
419 | |
417 | |
420 | for (n = 1, t = s; *t; t++) |
418 | for (n = 1, t = s; *t; t++) |
421 | if (*t == ',') |
419 | if (*t == ',') |
422 | n++; |
420 | n++; |
|
|
421 | |
423 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
422 | ret = (char **)malloc ((n + 1) * sizeof (char *)); |
424 | ret[n] = NULL; |
423 | ret[n] = NULL; |
425 | |
424 | |
426 | for (l = 0, t = s; l < n; l++) |
425 | for (l = 0, t = s; l < n; l++) |
427 | { |
426 | { |
… | |
… | |
431 | strncpy (ret[l], s, p); |
430 | strncpy (ret[l], s, p); |
432 | ret[l][p] = '\0'; |
431 | ret[l][p] = '\0'; |
433 | rxvt_Str_trim (ret[l]); |
432 | rxvt_Str_trim (ret[l]); |
434 | s = ++t; |
433 | s = ++t; |
435 | } |
434 | } |
|
|
435 | |
436 | return ret; |
436 | return ret; |
437 | } |
437 | } |
438 | |
438 | |
439 | void |
439 | void |
440 | rxvt_freecommastring (char **cs) |
440 | rxvt_freecommastring (char **cs) |