1 /* wvWare
2 * Copyright (C) Caolan McNamara, Dom Lachowicz, and others
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include "wv.h"
27 #include "utf.h"
28
29
30 char *
wvWideStrToMB(U16 * str)31 wvWideStrToMB (U16 * str)
32 {
33 int len, len2 = 0, j;
34 char *utf8 = NULL;
35 char target[5]; /*
36 no wide char becomes longer than about 3 or 4 chars,
37 but you never know :-)
38 */
39 if (str == NULL)
40 return (NULL);
41
42 while (*str != 0)
43 {
44 len = our_wctomb (target, *str);
45 utf8 = (char *) realloc (utf8, len2 + len + 1);
46 for (j = 0; j < len; j++)
47 utf8[len2 + j] = target[j];
48 len2 += len;
49 str++;
50 }
51 if (utf8 != NULL)
52 utf8[len2] = '\0';
53 return (utf8);
54 }
55
56
57 char *
wvWideCharToMB(U16 char16)58 wvWideCharToMB (U16 char16)
59 {
60 int len, len2 = 0, j;
61 char *utf8 = NULL;
62 char target[5]; /*
63 no wide char becomes longer than about 3 or 4 chars,
64 but you never know :-)
65 */
66 len = our_wctomb (target, char16);
67 utf8 = (char *) realloc (utf8, len2 + len + 1);
68 for (j = 0; j < len; j++)
69 utf8[len2 + j] = target[j];
70 len2 += len;
71
72 if (utf8 != NULL)
73 utf8[len2] = '\0';
74 return (utf8);
75 }
76