-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoding_std.cpp
More file actions
229 lines (194 loc) · 5.73 KB
/
encoding_std.cpp
File metadata and controls
229 lines (194 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "../include/encoding/encoding_std.h"
#ifdef __cplusplus
extern "C" {
#endif
#include "../include/encoding/encoding.h"
#ifdef __cplusplus
}
#endif
template <typename dest_string_type>
HRESULT
string_convert(HRESULT (*func_convert)(const void *, size_t, void *, size_t *), const void *ptr, size_t size_of_bytes,
dest_string_type &dst, size_t dest_element_size)
{
if (!(func_convert && ptr && size_of_bytes && dest_element_size))
return E_INVALIDARG;
dst.clear();
HRESULT hr;
size_t out_size = 0;
hr = func_convert(ptr, size_of_bytes, NULL, &out_size);
if (FAILED(hr))
return hr;
if (out_size)
{
dst.resize(out_size / dest_element_size);
return func_convert(ptr, size_of_bytes, &dst[0], &out_size);
}
return S_OK;
}
template <typename dest_string_type>
HRESULT
string_convert(HRESULT (*func_convert)(const void *, size_t, void *, size_t *), const void *ptr, size_t size_of_bytes,
dest_string_type &dst)
{
return string_convert(func_convert, ptr, size_of_bytes, dst, sizeof(typename dest_string_type::value_type));
}
HRESULT
string_2_wstring(const char *src, size_t src_size, std::wstring &dst)
{
#ifdef WIN32
return string_convert(mbcs_2_utf16le, src, src_size, dst);
#else
return string_convert(mbcs_2_utf32le, src, src_size, dst);
#endif
}
HRESULT
string_2_wstring(const std::string &src, std::wstring &dst)
{
return string_2_wstring(src.c_str(), src.size(), dst);
}
HRESULT
wstring_2_string(const wchar_t *src, size_t src_size, std::string &dst)
{
#ifdef WIN32
return string_convert(utf16le_2_mbcs, src, src_size * sizeof(wchar_t), dst);
#else
return string_convert(utf32le_2_mbcs, src, src_size * sizeof(wchar_t), dst);
#endif
}
HRESULT
wstring_2_string(const std::wstring &src, std::string &dst)
{
return wstring_2_string(src.c_str(), src.size(), dst);
}
// string <-> utf8
HRESULT
string_2_utf8(const char *src, size_t src_size, std::string &dst)
{
#ifdef WIN32
std::wstring ws;
HRESULT hr;
hr = string_2_wstring(src, src_size, ws);
if (FAILED(hr))
return hr;
return wstring_2_utf8(ws, dst);
#else
return string_convert(mbcs_2_utf8, src, src_size, dst);
#endif
}
HRESULT
string_2_utf8(const std::string &src, std::string &dst)
{
return string_2_utf8(src.c_str(), src.size(), dst);
}
HRESULT
utf8_2_string(const char *src, size_t src_size, std::string &dst)
{
#ifdef WIN32
std::wstring ws;
HRESULT hr;
hr = utf8_2_wstring(src, src_size, ws);
if (FAILED(hr))
return hr;
return wstring_2_string(ws, dst);
#else
return string_convert(utf8_2_mbcs, src, src_size, dst);
#endif
}
HRESULT
utf8_2_string(const std::string &src, std::string &dst)
{
return utf8_2_string(src.c_str(), src.size(), dst);
}
// wstring <-> utf8
HRESULT
wstring_2_utf8(const wchar_t *src, size_t src_size, std::string &dst)
{
#ifdef WIN32
return string_convert(utf16le_2_utf8, src, src_size * sizeof(wchar_t), dst);
#else
return string_convert(utf32le_2_utf8, src, src_size * sizeof(wchar_t), dst);
#endif
}
HRESULT
wstring_2_utf8(const std::wstring &src, std::string &dst)
{
return wstring_2_utf8(src.c_str(), src.size(), dst);
}
HRESULT utf8_2_wstring(const char *src, size_t src_size, std::wstring &dst)
{
#ifdef WIN32
return string_convert(utf8_2_utf16le, src, src_size, dst);
#else
return string_convert(utf8_2_utf32le, src, src_size, dst);
#endif
}
HRESULT utf8_2_wstring(const std::string &src, std::wstring &dst)
{
return utf8_2_wstring(src.c_str(), src.size(), dst);
}
// string <-> u16string
HRESULT string_2_u16string(const char *src, size_t src_size, my_u16string &dst)
{
return string_convert(mbcs_2_utf16le, src, src_size, dst);
}
HRESULT string_2_u16string(const std::string &src, my_u16string &dst)
{
return string_2_u16string(src.c_str(), src.size(), dst);
}
HRESULT u16string_2_string(const unsigned short *src, size_t src_size, std::string &dst)
{
return string_convert(utf16le_2_mbcs, src, src_size * sizeof(unsigned short), dst);
}
HRESULT u16string_2_string(const my_u16string &src, std::string &dst)
{
return u16string_2_string((const unsigned short *)src.c_str(), src.size(), dst);
}
// wstring <-> u16string
HRESULT wstring_2_u16string(const wchar_t *src, size_t src_size, my_u16string &dst)
{
#ifdef WIN32
if (!(src && src_size))
return E_INVALIDARG;
dst.assign((const my_u16string::value_type *)src, src_size);
return S_OK;
#else
return string_convert(utf32le_2_utf16le, src, src_size * sizeof(wchar_t), dst);
#endif
}
HRESULT wstring_2_u16string(const std::wstring &src, my_u16string &dst)
{
return wstring_2_u16string(src.c_str(), src.size(), dst);
}
HRESULT u16string_2_wstring(const unsigned short *src, size_t src_size, std::wstring &dst)
{
#ifdef WIN32
if (!(src && src_size))
return E_INVALIDARG;
dst.assign((const wchar_t *)src, src_size);
return S_OK;
#else
return string_convert(utf16le_2_utf32le, src, src_size * sizeof(wchar_t), dst);
#endif
}
HRESULT u16string_2_wstring(const my_u16string &src, std::wstring &dst)
{
return u16string_2_wstring((const unsigned short *)src.c_str(), src.size(), dst);
}
// utf8 <-> u16string
HRESULT utf8_2_u16string(const char *src, size_t src_size, my_u16string &dst)
{
return string_convert(utf8_2_utf16le, src, src_size, dst);
}
HRESULT utf8_2_u16string(const std::string &src, my_u16string &dst)
{
return utf8_2_u16string(src.c_str(), src.size(), dst);
}
HRESULT u16string_2_utf8(const unsigned short *src, size_t src_size, std::string &dst)
{
return string_convert(utf16le_2_utf8, src, src_size, dst);
}
HRESULT u16string_2_utf8(const my_u16string &src, std::string &dst)
{
return u16string_2_utf8((const unsigned short *)src.c_str(), src.size(), dst);
}