ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/misc.C
(Generate patch)

Comparing rxvt-unicode/src/misc.C (file contents):
Revision 1.19 by root, Sun Dec 12 06:30:25 2004 UTC vs.
Revision 1.26 by root, Wed Feb 16 20:32:05 2005 UTC

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
30char * 29char *
31rxvt_wcstombs (const wchar_t *str, int len) 30rxvt_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
53wchar_t * 52wchar_t *
54rxvt_mbstowcs (const char *str, int len) 53rxvt_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
101wchar_t * 100wchar_t *
102rxvt_utf8towcs (const char *str, int len) 101rxvt_utf8towcs (const char *str, int len)
103{ 102{
104 if (len < 0) len = strlen (str); 103 if (len < 0) len = strlen (str);
105 104
106 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)); 105 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t)),
107 wchar_t *p = r; 106 *p = r;
108 107
109 unsigned char *s = (unsigned char *)str; 108 unsigned char *s = (unsigned char *)str,
109 *e = s + len;
110 110
111 while (len--) 111 for (;;)
112 { 112 {
113 len = e - s;
114
115 if (len == 0)
116 break;
113 if (s[0] < 0x80) 117 else if (s[0] < 0x80)
114 *p++ = *s++; 118 *p++ = *s++;
115 else if (len > 0 119 else if (len >= 2
116 && s[0] >= 0xc2 && s[0] <= 0xdf 120 && s[0] >= 0xc2 && s[0] <= 0xdf
117 && (s[1] & 0xc0) == 0x80) 121 && (s[1] & 0xc0) == 0x80)
118 { 122 {
119 *p++ = ((s[0] & 0x1f) << 6) 123 *p++ = ((s[0] & 0x1f) << 6)
120 | (s[1] & 0x3f); 124 | (s[1] & 0x3f);
121 s += 2; 125 s += 2;
122 } 126 }
123 else if (len > 1 127 else if (len >= 3
124 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) 128 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
125 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) 129 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
126 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) 130 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
127 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) 131 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
128 ) 132 )
131 *p++ = ((s[0] & 0x0f) << 12) 135 *p++ = ((s[0] & 0x0f) << 12)
132 | ((s[1] & 0x3f) << 6) 136 | ((s[1] & 0x3f) << 6)
133 | (s[2] & 0x3f); 137 | (s[2] & 0x3f);
134 s += 3; 138 s += 3;
135 } 139 }
136 else if (len > 2 140 else if (len >= 4
137 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) 141 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
138 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) 142 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
139 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) 143 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
140 ) 144 )
141 && (s[2] & 0xc0) == 0x80 145 && (s[2] & 0xc0) == 0x80
277/* INTPROTO */ 281/* INTPROTO */
278char * 282char *
279rxvt_Str_trim (char *str) 283rxvt_Str_trim (char *str)
280{ 284{
281 char *r, *s; 285 char *r, *s;
282 int n;
283 286
284 if (!str || !*str) /* shortcut */ 287 if (!str || !*str) /* shortcut */
285 return str; 288 return str;
286 289
287 /* skip leading spaces */ 290 /* skip leading spaces */
288 for (s = str; *s && isspace (*s); s++) ; 291 for (s = str; *s && isspace (*s); s++) ;
292
289 /* goto end of string */ 293 /* goto end of string */
290 for (n = 0, r = s; *r++; n++) ; 294 r = s + strlen (s) - 1;
291 r -= 2; 295
292 /* dump return */ 296 /* dump return and other trailing whitespace */
293 if (n > 0 && *r == '\n') 297 while (r > s && isspace (*r))
294 n--, r--; 298 r--;
295 /* backtrack along trailing spaces */ 299
296 for (; n > 0 && isspace (*r); r--, n--) ; 300#if 0
297 /* skip matching leading/trailing quotes */ 301 /* skip matching leading/trailing quotes */
298 if (*s == '"' && *r == '"' && n > 1) 302 if (*s == '"' && *r == '"' && n > 1)
299 { 303 {
300 s++; 304 s++;
301 n -= 2; 305 n -= 2;
302 } 306 }
307#endif
303 308
304 /* copy back over: forwards copy */ 309 memmove (str, s, r + 1 - s);
305 for (r = str; n; n--) 310 str[r + 1 - s] = 0;
306 *r++ = *s++;
307 *r = '\0';
308 311
309 return str; 312 return str;
310} 313}
311 314
312/* 315/*
395 return (d - str); 398 return (d - str);
396} 399}
397 400
398/* 401/*
399 * Split a comma-separated string into an array, stripping leading and 402 * Split a comma-separated string into an array, stripping leading and
400 * trailing spaces (and paired quotes) from each entry. Empty strings 403 * trailing spaces from each entry. Empty strings are properly returned
401 * are properly returned
402 * Caller should free each entry and array when done 404 * Caller should free each entry and array when done
403 */ 405 */
404/* INTPROTO */ 406/* INTPROTO */
405char ** 407char **
406rxvt_splitcommastring (const char *cs) 408rxvt_splitcommastring (const char *cs)
413 s = ""; 415 s = "";
414 416
415 for (n = 1, t = s; *t; t++) 417 for (n = 1, t = s; *t; t++)
416 if (*t == ',') 418 if (*t == ',')
417 n++; 419 n++;
420
418 ret = (char **)malloc ((n + 1) * sizeof (char *)); 421 ret = (char **)malloc ((n + 1) * sizeof (char *));
419 ret[n] = NULL; 422 ret[n] = NULL;
420 423
421 for (l = 0, t = s; l < n; l++) 424 for (l = 0, t = s; l < n; l++)
422 { 425 {
426 strncpy (ret[l], s, p); 429 strncpy (ret[l], s, p);
427 ret[l][p] = '\0'; 430 ret[l][p] = '\0';
428 rxvt_Str_trim (ret[l]); 431 rxvt_Str_trim (ret[l]);
429 s = ++t; 432 s = ++t;
430 } 433 }
434
431 return ret; 435 return ret;
432} 436}
433 437
434void 438void
435rxvt_freecommastring (char **cs) 439rxvt_freecommastring (char **cs)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines