-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnicodeConvAtl.h
225 lines (194 loc) · 8.42 KB
/
UnicodeConvAtl.h
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
#pragma once
////////////////////////////////////////////////////////////////////////////////
// Unicode UTF-16/UTF-8 conversion functions for ATL (and MFC) CStringA/W
//
// Copyright (C) by Giovanni Dicanio
// <giovanni.dicanio AT gmail.com>
//
////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// This is a header-only C++ file that implements a couple of functions
// to simply and conveniently convert Unicode text between UTF-16 and UTF-8.
//
// CStringW is used to store UTF-16-encoded text.
// CStringA is used to store UTF-8-encoded text.
//
// The exported functions are:
//
// * Convert from UTF-16 to UTF-8:
// CStringA ToUtf8(CStringW const& utf16)
//
// * Convert from UTF-8 to UTF-16:
// CStringW ToUtf16(CStringA const& utf8)
//
// These functions live under the UnicodeConvAtl namespace.
//
// This code compiles cleanly at warning level 4 (/W4)
// on both 32-bit and 64-bit builds.
//
//
// NOTE ON COMPILING THE CODE ON OLDER VC++ COMPILERS
// ==================================================
//
// This code has been written and compiled with Visual Studio 2019.
// If you want to back-port it to older C++ compilers (like VS 2008)
// that don't implement C++11+ features, you can simply:
//
// - replace all the occurrences of nullptr with NULL
// - replace all the occurrences of constexpr with static const
//
//
//------------------------------------------------------------------------------
//
// The MIT License(MIT)
//
// Copyright(c) 2010-2023 by Giovanni Dicanio
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//------------------------------------------------------------------------------
//==============================================================================
// Includes
//==============================================================================
#include <windows.h> // Win32 Platform SDK
#include <atldef.h> // ATLASSERT, AtlThrow, AtlThrowLastWin32
#include <atlstr.h> // CStringA/W
//==============================================================================
// Function Implementations
//==============================================================================
namespace UnicodeConvAtl {
//------------------------------------------------------------------------------
// Convert from UTF-16 CStringW to UTF-8 CStringA.
// Signal errors using AtlThrow.
//------------------------------------------------------------------------------
inline CStringA ToUtf8(CStringW const& utf16)
{
// Special case of empty input string
if (utf16.IsEmpty())
{
// Empty input --> return empty output string
return CStringA();
}
// Safely fail if an invalid UTF-16 character sequence is encountered
constexpr DWORD kFlags = WC_ERR_INVALID_CHARS;
const int utf16Length = utf16.GetLength();
// Get the length, in chars, of the resulting UTF-8 string
const int utf8Length = ::WideCharToMultiByte(
CP_UTF8, // convert to UTF-8
kFlags, // conversion flags
utf16, // source UTF-16 string
utf16Length, // length of source UTF-16 string, in wchar_ts
nullptr, // unused - no conversion required in this step
0, // request size of destination buffer, in chars
nullptr, nullptr // unused
);
if (utf8Length == 0)
{
// Conversion error: capture error code and throw
AtlThrowLastWin32();
}
// Make room in the destination string for the converted bits
CStringA utf8;
char* utf8Buffer = utf8.GetBuffer(utf8Length);
ATLASSERT(utf8Buffer != nullptr);
// Do the actual conversion from UTF-16 to UTF-8
int result = ::WideCharToMultiByte(
CP_UTF8, // convert to UTF-8
kFlags, // conversion flags
utf16, // source UTF-16 string
utf16Length, // length of source UTF-16 string, in wchar_ts
utf8Buffer, // pointer to destination buffer
utf8Length, // size of destination buffer, in chars
nullptr, nullptr // unused
);
if (result == 0)
{
// Conversion error: capture error code and throw
AtlThrowLastWin32();
}
// Don't forget to call ReleaseBuffer on the CString object!
utf8.ReleaseBuffer(utf8Length);
// It is good coding practice to clear the CString buffer pointer
// that was returned by CString::GetBuffer after a matching call
// to CString::ReleaseBuffer.
// However, in this case we just return the result string
// from the function, so we can skip that line:
//
// utf8Buffer = nullptr;
return utf8;
}
//------------------------------------------------------------------------------
// Convert from UTF-8 CStringA to UTF-16 CStringW.
// Signal errors using AtlThrow.
//------------------------------------------------------------------------------
inline CStringW ToUtf16(CStringA const& utf8)
{
// Special case of empty input string
if (utf8.IsEmpty())
{
// Empty input --> return empty output string
return CStringW();
}
// Safely fail if an invalid UTF-8 character sequence is encountered
constexpr DWORD kFlags = MB_ERR_INVALID_CHARS;
const int utf8Length = utf8.GetLength();
// Get the size of the destination UTF-16 string
const int utf16Length = ::MultiByteToWideChar(
CP_UTF8, // source string is in UTF-8
kFlags, // conversion flags
utf8, // source UTF-8 string pointer
utf8Length, // length of the source UTF-8 string, in chars
nullptr, // unused - no conversion done in this step
0 // request size of destination buffer, in wchar_ts
);
if (utf16Length == 0)
{
// Conversion error: capture error code and throw
AtlThrowLastWin32();
}
// Make room in the destination string for the converted bits
CStringW utf16;
wchar_t* utf16Buffer = utf16.GetBuffer(utf16Length);
ATLASSERT(utf16Buffer != nullptr);
// Do the actual conversion from UTF-8 to UTF-16
int result = ::MultiByteToWideChar(
CP_UTF8, // source string is in UTF-8
kFlags, // conversion flags
utf8, // source UTF-8 string pointer
utf8Length, // length of source UTF-8 string, in chars
utf16Buffer, // pointer to destination buffer
utf16Length // size of destination buffer, in wchar_ts
);
if (result == 0)
{
// Conversion error: capture error code and throw
AtlThrowLastWin32();
}
// Don't forget to call ReleaseBuffer on the CString object!
utf16.ReleaseBuffer(utf16Length);
// It is good coding practice to clear the CString buffer pointer
// that was returned by CString::GetBuffer after a matching call
// to CString::ReleaseBuffer.
// However, in this case we just return the result string
// from the function, so we can skip that line:
//
// utf16Buffer = nullptr;
return utf16;
}
} // namespace UnicodeConvAtl