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