… | |
… | |
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 */ |
|
|
31 | char * |
30 | char * |
32 | rxvt_wcstombs (const wchar_t *str, int len) |
31 | rxvt_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 */ |
53 | wchar_t * |
|
|
54 | rxvt_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 | |
|
|
66 | char * |
|
|
67 | rxvt_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 | |
|
|
101 | wchar_t * |
|
|
102 | rxvt_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 | *p++ = *s++; |
|
|
115 | else if (len > 0 |
|
|
116 | && s[0] >= 0xc2 && s[0] <= 0xdf |
|
|
117 | && (s[1] & 0xc0) == 0x80) |
|
|
118 | { |
|
|
119 | *p++ = ((s[0] & 0x1f) << 6) |
|
|
120 | | (s[1] & 0x3f); |
|
|
121 | s += 2; |
|
|
122 | } |
|
|
123 | else if (len > 1 |
|
|
124 | && ( (s[0] == 0xe0 && s[1] >= 0xa0 && s[1] <= 0xbf) |
|
|
125 | || (s[0] >= 0xe1 && s[0] <= 0xec && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
126 | || (s[0] == 0xed && s[1] >= 0x80 && s[1] <= 0x9f) |
|
|
127 | || (s[0] >= 0xee && s[0] <= 0xef && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
128 | ) |
|
|
129 | && (s[2] & 0xc0) == 0x80) |
|
|
130 | { |
|
|
131 | *p++ = ((s[0] & 0x0f) << 12) |
|
|
132 | | ((s[1] & 0x3f) << 6) |
|
|
133 | | (s[2] & 0x3f); |
|
|
134 | s += 3; |
|
|
135 | } |
|
|
136 | else if (len > 2 |
|
|
137 | && ( (s[0] == 0xf0 && s[1] >= 0x90 && s[1] <= 0xbf) |
|
|
138 | || (s[0] >= 0xf1 && s[0] <= 0xf3 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
139 | || (s[0] == 0xf4 && s[1] >= 0x80 && s[1] <= 0x8f) |
|
|
140 | ) |
|
|
141 | && (s[2] & 0xc0) == 0x80 |
|
|
142 | && (s[3] & 0xc0) == 0x80) |
|
|
143 | { |
|
|
144 | *p++ = ((s[0] & 0x07) << 18) |
|
|
145 | | ((s[1] & 0x3f) << 12) |
|
|
146 | | ((s[2] & 0x3f) << 6) |
|
|
147 | | (s[3] & 0x3f); |
|
|
148 | s += 4; |
|
|
149 | } |
|
|
150 | else |
|
|
151 | { |
|
|
152 | *p++ = 0xfffd; |
|
|
153 | s++; |
|
|
154 | } |
|
|
155 | } |
|
|
156 | |
|
|
157 | *p = 0; |
|
|
158 | |
|
|
159 | return r; |
|
|
160 | } |
|
|
161 | |
53 | char * |
162 | char * |
54 | rxvt_strdup (const char *str) |
163 | rxvt_strdup (const char *str) |
55 | { |
164 | { |
56 | return str ? strdup (str) : 0; |
165 | return str ? strdup (str) : 0; |
57 | } |
166 | } |