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.15 by root, Sun Aug 15 04:54:21 2004 UTC vs.
Revision 1.20 by root, Mon Dec 13 06:44:18 2004 UTC

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 */ 28#include "misc.intpro" /* PROTOS for internal routines */
29 29
30/* INTPROTO */
31char * 30char *
32rxvt_wcstombs (const wchar_t *str, int len) 31rxvt_wcstombs (const wchar_t *str, int len)
33{ 32{
33 if (len < 0) len = wcslen (str);
34
34 mbstate mbs; 35 mbstate mbs;
35 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1); 36 char *r = (char *)rxvt_malloc (len * MB_CUR_MAX + 1);
36 37
37 char *dst = r; 38 char *dst = r;
38 while (len--) 39 while (len--)
47 *dst++ = 0; 48 *dst++ = 0;
48 49
49 return r; 50 return r;
50} 51}
51 52
52/* INTPROTO */ 53wchar_t *
54rxvt_mbstowcs (const char *str, int len)
55{
56 if (len < 0) len = strlen (str);
57
58 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
59
60 if (mbstowcs (r, str, len + 1) < 0)
61 *r = 0;
62
63 return r;
64}
65
66char *
67rxvt_wcstoutf8 (const wchar_t *str, int len)
68{
69 if (len < 0) len = wcslen (str);
70
71 char *r = (char *)rxvt_malloc (len * 4 + 1);
72 char *p = r;
73
74 while (len--)
75 {
76 unicode_t w = *str++;
77
78 if (w < 0x000080)
79 *p++ = w;
80 else if (w < 0x000800)
81 *p++ = 0xc0 | ( w >> 6),
82 *p++ = 0x80 | ( w & 0x3f);
83 else if (w < 0x010000)
84 *p++ = 0xe0 | ( w >> 12 ),
85 *p++ = 0x80 | ((w >> 6) & 0x3f),
86 *p++ = 0x80 | ( w & 0x3f);
87 else if (w < 0x110000)
88 *p++ = 0xf0 | ( w >> 18),
89 *p++ = 0x80 | ((w >> 12) & 0x3f),
90 *p++ = 0x80 | ((w >> 6) & 0x3f),
91 *p++ = 0x80 | ( w & 0x3f);
92 else
93 *p++ = '?';
94 }
95
96 *p = 0;
97
98 return r;
99}
100
101wchar_t *
102rxvt_utf8towcs (const char *str, int len)
103{
104 if (len < 0) len = strlen (str);
105
106 wchar_t *r = (wchar_t *)rxvt_malloc ((len + 1) * sizeof (wchar_t));
107 wchar_t *p = r;
108
109 unsigned char *s = (unsigned char *)str;
110
111 while (len)
112 {
113 if (s[0] < 0x80)
114 {
115 *p++ = *s++; len--;
116 }
117 else if (len > 0
118 && s[0] >= 0xc2 && s[0] <= 0xdf
119 && (s[1] & 0xc0) == 0x80)
120 {
121 *p++ = ((s[0] & 0x1f) << 6)
122 | (s[1] & 0x3f);
123 s += 2; len -= 2;
124 }
125 else if (len > 1
126 && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf)
127 || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf)
128 || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f)
129 || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf)
130 )
131 && (s[2] & 0xc0) == 0x80)
132 {
133 *p++ = ((s[0] & 0x0f) << 12)
134 | ((s[1] & 0x3f) << 6)
135 | (s[2] & 0x3f);
136 s += 3; len -= 3;
137 }
138 else if (len > 2
139 && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf)
140 || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf)
141 || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f)
142 )
143 && (s[2] & 0xc0) == 0x80
144 && (s[3] & 0xc0) == 0x80)
145 {
146 *p++ = ((s[0] & 0x07) << 18)
147 | ((s[1] & 0x3f) << 12)
148 | ((s[2] & 0x3f) << 6)
149 | (s[3] & 0x3f);
150 s += 4; len -= 4;
151 }
152 else
153 {
154 *p++ = 0xfffd;
155 s++; len--;
156 }
157 }
158
159 *p = 0;
160
161 return r;
162}
163
53char * 164char *
54rxvt_strdup (const char *str) 165rxvt_strdup (const char *str)
55{ 166{
56 return str ? strdup (str) : 0; 167 return str ? strdup (str) : 0;
57} 168}
320 s = ++t; 431 s = ++t;
321 } 432 }
322 return ret; 433 return ret;
323} 434}
324 435
436void
437rxvt_freecommastring (char **cs)
438{
439 for (int i = 0; cs[i]; ++i)
440 free (cs[i]);
441
442 free (cs);
443}
444
325/*----------------------------------------------------------------------* 445/*----------------------------------------------------------------------*
326 * file searching 446 * file searching
327 */ 447 */
328 448
329/* #define DEBUG_SEARCH_PATH */ 449/* #define DEBUG_SEARCH_PATH */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines