1 |
/* wptUTF8.cpp - UTF8 conversation |
/* wptUTF8.cpp - UTF8 conversation |
2 |
* Copyright (C) 2002, 2004, 2005, 2006 Timo Schulz |
* Copyright (C) 2002, 2004, 2005, 2006, 2009, 2012 Timo Schulz |
3 |
* |
* |
4 |
* This file is part of WinPT. |
* This file is part of WinPT. |
5 |
* |
* |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
|
* |
|
|
* You should have received a copy of the GNU General Public License |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
|
|
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
15 |
*/ |
*/ |
|
|
|
16 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
17 |
#include <config.h> |
#include <config.h> |
18 |
#endif |
#endif |
27 |
#include "wptErrors.h" |
#include "wptErrors.h" |
28 |
|
|
29 |
|
|
30 |
|
/** |
31 |
|
* Convert the given intput string, which is encoded with the locale |
32 |
|
* setting, into UTF-8 representation. |
33 |
|
*/ |
34 |
char* |
char* |
35 |
native_to_utf8 (const char *string) |
native_to_utf8 (const char *string) |
36 |
{ |
{ |
37 |
wchar_t *result; |
int n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); |
|
char *native; |
|
|
int n; |
|
|
|
|
|
n = MultiByteToWideChar (GetACP (), 0, string, -1, NULL, 0); |
|
38 |
if (n < 0) |
if (n < 0) |
39 |
return NULL; |
return NULL; |
40 |
|
|
41 |
result = new wchar_t[n+1]; |
wchar_t *result = new wchar_t[n+1]; |
42 |
if (!result) |
if (!result) |
43 |
BUG (0); |
BUG (0); |
44 |
|
|
52 |
if (n < 0) |
if (n < 0) |
53 |
return NULL; |
return NULL; |
54 |
|
|
55 |
native = new char[n+1]; |
char *native = new char[n + 1]; |
56 |
if (!native) |
if (!native) |
57 |
BUG (0); |
BUG (0); |
58 |
|
memset(native, 0, n + 1); |
59 |
|
|
60 |
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); |
n = WideCharToMultiByte (CP_UTF8, 0, result, -1, native, n, NULL, NULL); |
61 |
if (n < 0) { |
if (n < 0) { |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
/* Convert utf8 string @str to native CP. */ |
/** |
72 |
|
* Convert an UTF-8 string into an UTF-16 string. |
73 |
|
*/ |
74 |
|
wchar_t* |
75 |
|
utf8_to_utf16(const char *string, size_t *retlen) |
76 |
|
{ |
77 |
|
int n = MultiByteToWideChar(CP_UTF8, 0, string, -1, NULL, 0); |
78 |
|
if (n < 0) |
79 |
|
return NULL; |
80 |
|
|
81 |
|
wchar_t *result = new wchar_t[n + 1]; |
82 |
|
if (!result) |
83 |
|
BUG(0); |
84 |
|
|
85 |
|
n = MultiByteToWideChar(CP_UTF8, 0, string, -1, result, n); |
86 |
|
if (n < 0) { |
87 |
|
free_if_alloc(result); |
88 |
|
return NULL; |
89 |
|
} |
90 |
|
|
91 |
|
*retlen = n; |
92 |
|
return result; |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
/** |
97 |
|
* Convert the given string, which is encoded in UTF-8, |
98 |
|
* into the locale setting. |
99 |
|
*/ |
100 |
char* |
char* |
101 |
utf8_to_native (const char *string) |
utf8_to_native (const char *string) |
102 |
{ |
{ |
103 |
wchar_t *result; |
int n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0); |
|
char *native; |
|
|
int n; |
|
|
|
|
|
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, NULL, 0); |
|
104 |
if (n < 0) |
if (n < 0) |
105 |
return NULL; |
return NULL; |
106 |
|
|
107 |
result = (wchar_t*)malloc ((n+1) * sizeof *result); |
wchar_t *result = new wchar_t[n+1]; |
108 |
if (!result) |
if (!result) |
109 |
BUG (0); |
BUG (0); |
110 |
|
|
111 |
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n); |
n = MultiByteToWideChar (CP_UTF8, 0, string, -1, result, n); |
112 |
if (n < 0) { |
if (n < 0) { |
113 |
free (result); |
free_if_alloc (result); |
114 |
return NULL; |
return NULL; |
115 |
} |
} |
116 |
|
|
118 |
if (n < 0) |
if (n < 0) |
119 |
return NULL; |
return NULL; |
120 |
|
|
121 |
native = (char*)malloc (n+1); |
char *native = new char[n + 1]; |
122 |
if (!native) |
if (!native) |
123 |
BUG (0); |
BUG (0); |
124 |
|
memset(native, 0, n + 1); |
125 |
|
|
126 |
n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL); |
n = WideCharToMultiByte (GetACP (), 0, result, -1, native, n, NULL, NULL); |
127 |
if (n < 0) { |
if (n < 0) { |
128 |
free (result); |
free_if_alloc (result); |
129 |
return NULL; |
return NULL; |
130 |
} |
} |
131 |
|
|
132 |
free (result); |
free_if_alloc (result); |
133 |
return native; |
return native; |
134 |
} |
} |
135 |
|
|
136 |
|
|
137 |
/* Return -1 if the string contains any 8-bit characters. */ |
/** |
138 |
|
* Return -1 if the given string contains any 8-bit characters. |
139 |
|
* This is a helper to decide when to use UTF8 encoding. |
140 |
|
*/ |
141 |
int |
int |
142 |
is_8bit_string (const char *str) |
is_8bit_string (const char *str) |
143 |
{ |
{ |
144 |
size_t i; |
for (size_t i = 0; i < strlen (str); i++) { |
|
|
|
|
for (i = 0; i < strlen (str); i++) { |
|
145 |
if (str[i] & 0x80) |
if (str[i] & 0x80) |
146 |
return -1; |
return -1; |
147 |
} |
} |