Debian Codegraph @

src/win/wunicode.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Windows "UNICODE" helper routines.
12 *
13 * See LICENSE.txt for copyright information.
14 */
15
16 #include "allegro5/allegro.h"
17 #include "allegro5/internal/aintern_wunicode.h"
18
19 #include <windows.h>
20
21 ALLEGRO_DEBUG_CHANNEL("wunicode")
22
23 /* _al_win_ustr_to_utf16:
24 * Convert ALLEGRO_USTR to newly-allocated UTF-16 buffer
25 */
26 wchar_t *_al_win_ustr_to_utf16(const ALLEGRO_USTR *u)
27 {
28 int wslen;
29 wchar_t *ws;
30 const char* us = al_cstr(u);
31 size_t uslen = al_ustr_size(u);
32
33 if (uslen == 0) {
34 ws = al_malloc(sizeof(wchar_t));
35 ws[0] = 0;
36 return ws;
37 }
38 wslen = MultiByteToWideChar(CP_UTF8, 0, us, uslen, NULL, 0);
39 if (wslen == 0) {
40 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
41 return NULL;
42 }
43 /* For the NUL at the end. */
44 wslen += 1;
45 ws = al_malloc(sizeof(wchar_t) * wslen);
46 if (!ws) {
47 ALLEGRO_ERROR("Out of memory\n");
48 return NULL;
49 }
50 if (0 == MultiByteToWideChar(CP_UTF8, 0, us, uslen, ws, wslen)) {
51 al_free(ws);
52 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
53 return NULL;
54 }
55 ws[wslen - 1] = 0;
56 return ws;
57 }
58
59 /* _al_win_utf8_to_ansi:
60 * Convert UTF-8 to newly-allocated ansi buffer
61 */
62 char* _al_win_ustr_to_ansi(const ALLEGRO_USTR *u) {
63 int wslen;
64 wchar_t *ws;
65 int slen;
66 char *s;
67 const char* us = al_cstr(u);
68 size_t uslen = al_ustr_size(u);
69
70 if (uslen == 0) {
71 s = al_malloc(sizeof(char));
72 s[0] = 0;
73 return s;
74 }
75 wslen = MultiByteToWideChar(CP_UTF8, 0, us, uslen, NULL, 0);
76 if (wslen == 0) {
77 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
78 return NULL;
79 }
80 ws = al_malloc(sizeof(wchar_t) * wslen);
81 if (!ws) {
82 ALLEGRO_ERROR("Out of memory\n");
83 return NULL;
84 }
85 if (0 == MultiByteToWideChar(CP_UTF8, 0, us, uslen, ws, wslen)) {
86 al_free(ws);
87 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
88 return NULL;
89 }
90 slen = WideCharToMultiByte(CP_ACP, 0, ws, wslen, NULL, 0, NULL, NULL);
91 if (slen == 0) {
92 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
93 al_free(ws);
94 return NULL;
95 }
96 /* For the NUL at the end. */
97 slen += 1;
98 s = al_malloc(sizeof(char) * slen);
99 if (!s) {
100 ALLEGRO_ERROR("Out of memory\n");
101 al_free(ws);
102 return NULL;
103 }
104 if (0 == WideCharToMultiByte(CP_ACP, 0, ws, wslen, s, slen, NULL, NULL)) {
105 al_free(ws);
106 al_free(s);
107 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
108 return NULL;
109 }
110 s[slen - 1] = 0;
111 al_free(ws);
112 return s;
113 }
114
115 /* _al_win_utf8_to_utf16:
116 * Convert UTF-8 to newly-allocated UTF-16 buffer
117 */
118 wchar_t *_al_win_utf8_to_utf16(const char *us)
119 {
120 int wslen;
121 wchar_t *ws;
122
123 if (us == NULL) {
124 return NULL;
125 }
126 wslen = MultiByteToWideChar(CP_UTF8, 0, us, -1, NULL, 0);
127 if (wslen == 0) {
128 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
129 return NULL;
130 }
131 ws = al_malloc(sizeof(wchar_t) * wslen);
132 if (!ws) {
133 ALLEGRO_ERROR("Out of memory\n");
134 return NULL;
135 }
136 if (0 == MultiByteToWideChar(CP_UTF8, 0, us, -1, ws, wslen)) {
137 al_free(ws);
138 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
139 return NULL;
140 }
141 return ws;
142 }
143
144 /* _al_win_utf16_to_utf8:
145 * Convert UTF-16 to newly-allocated UTF-8 buffer
146 */
147 char *_al_win_utf16_to_utf8(const wchar_t *ws)
148 {
149 int uslen;
150 char *us;
151
152 if (ws == NULL) {
153 return NULL;
154 }
155 uslen = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
156 if (uslen == 0) {
157 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
158 return NULL;
159 }
160 us = al_malloc(sizeof(char) * uslen);
161 if (!us) {
162 ALLEGRO_ERROR("Out of memory\n");
163 return NULL;
164 }
165 if (0 == WideCharToMultiByte(CP_UTF8, 0, ws, -1, us, uslen, NULL, NULL)) {
166 al_free(us);
167 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
168 return NULL;
169 }
170 return us;
171 }
172 /* _al_win_utf8_to_ansi:
173 * Convert UTF-8 to newly-allocated ansi buffer
174 */
175 char* _al_win_utf8_to_ansi(const char *us) {
176 int wslen;
177 wchar_t *ws;
178 int slen;
179 char *s;
180
181 if (us == NULL) {
182 return NULL;
183 }
184 wslen = MultiByteToWideChar(CP_UTF8, 0, us, -1, NULL, 0);
185 if (wslen == 0) {
186 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
187 return NULL;
188 }
189 ws = al_malloc(sizeof(wchar_t) * wslen);
190 if (!ws) {
191 ALLEGRO_ERROR("Out of memory\n");
192 return NULL;
193 }
194 if (0 == MultiByteToWideChar(CP_UTF8, 0, us, -1, ws, wslen)) {
195 al_free(ws);
196 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
197 return NULL;
198 }
199 slen = WideCharToMultiByte(CP_ACP, 0, ws, -1, NULL, 0, NULL, NULL);
200 if (slen == 0) {
201 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
202 al_free(ws);
203 return NULL;
204 }
205 s = al_malloc(sizeof(char) * slen);
206 if (!s) {
207 ALLEGRO_ERROR("Out of memory\n");
208 al_free(ws);
209 return NULL;
210 }
211 if (0 == WideCharToMultiByte(CP_ACP, 0, ws, -1, s, slen, NULL, NULL)) {
212 al_free(ws);
213 al_free(s);
214 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
215 return NULL;
216 }
217 al_free(ws);
218 return s;
219 }
220
221 /* _al_win_ansi_to_utf8:
222 * Convert ansi to newly-allocated UTF-8 buffer
223 */
224 char* _al_win_ansi_to_utf8(const char *s) {
225 int wslen;
226 wchar_t *ws;
227 int uslen;
228 char *us;
229
230 if (s == NULL) {
231 return NULL;
232 }
233 wslen = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
234 if (wslen == 0) {
235 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
236 return NULL;
237 }
238 ws = al_malloc(sizeof(wchar_t) * wslen);
239 if (!ws) {
240 ALLEGRO_ERROR("Out of memory\n");
241 return NULL;
242 }
243 if (0 == MultiByteToWideChar(CP_ACP, 0, s, -1, ws, wslen)) {
244 al_free(ws);
245 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
246 return NULL;
247 }
248 uslen = WideCharToMultiByte(CP_UTF8, 0, ws, -1, NULL, 0, NULL, NULL);
249 if (uslen == 0) {
250 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
251 al_free(ws);
252 return NULL;
253 }
254 us = al_malloc(sizeof(char) * uslen);
255 if (!s) {
256 ALLEGRO_ERROR("Out of memory\n");
257 al_free(ws);
258 return NULL;
259 }
260 if (0 == WideCharToMultiByte(CP_UTF8, 0, ws, -1, us, uslen, NULL, NULL)) {
261 al_free(ws);
262 al_free(us);
263 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
264 return NULL;
265 }
266 al_free(ws);
267 return us;
268 }
269
270 /* _al_win_copy_utf16_to_utf8: Copy string and convert to UTF-8.
271 * This takes a string and copies a UTF-8 version of it to
272 * a buffer of fixed size.
273 * If successful, the string will be zero-terminated and is
274 * the return value of the function.
275 * If unsuccesful, NULL will be returned.
276 * If the representation would overflow the buffer, nothing
277 * is copied and the return value is NULL.
278 */
279 char *_al_win_copy_utf16_to_utf8(char* us, const wchar_t *ws, size_t uslen)
280 {
281 int rc = WideCharToMultiByte(CP_UTF8, 0, ws, -1, us, uslen, NULL, NULL);
282 if (rc == 0) {
283 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
284 return NULL;
285 }
286 return us;
287 }
288
289 char *_al_win_copy_utf8_to_ansi(char* s, const char *us, size_t slen)
290 {
291 int wslen = MultiByteToWideChar(CP_UTF8, 0, us, -1, NULL, 0);
292 if (wslen == 0) {
293 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
294 return NULL;
295 }
296 wchar_t* ws = al_malloc(wslen * sizeof(wchar_t));
297 if (!ws) {
298 ALLEGRO_ERROR("Out of memory\n");
299 return NULL;
300 }
301 MultiByteToWideChar(CP_UTF8, 0, us, -1, ws, wslen);
302 int rc = WideCharToMultiByte(CP_ACP, 0, ws, wslen, s, slen, NULL, NULL);
303 al_free(ws);
304 if (rc == 0) {
305 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
306 return NULL;
307 }
308 return s;
309 }
310
311 char *_al_win_copy_ansi_to_utf8(char* us, const char *s, size_t uslen)
312 {
313 int wslen = MultiByteToWideChar(CP_ACP, 0, s, -1, NULL, 0);
314 if (wslen == 0) {
315 ALLEGRO_ERROR("MultiByteToWideChar failed\n");
316 return NULL;
317 }
318 wchar_t* ws = al_malloc(wslen * sizeof(wchar_t));
319 if (!ws) {
320 ALLEGRO_ERROR("Out of memory\n");
321 return NULL;
322 }
323 MultiByteToWideChar(CP_ACP, 0, s, -1, ws, wslen);
324 int rc = WideCharToMultiByte(CP_UTF8, 0, ws, wslen, us, uslen, NULL, NULL);
325 al_free(ws);
326 if (rc == 0) {
327 ALLEGRO_ERROR("WideCharToMultiByte failed\n");
328 return NULL;
329 }
330 return us;
331 }
332
333
334
335 /* vim: set sts=3 sw=3 et: */