-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32_charset_conversion.cpp
78 lines (64 loc) · 1.42 KB
/
win32_charset_conversion.cpp
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
#include <string>
#include "win32_charset_conversion.h"
#include "win32_error_exception.h"
extern "C"
{
#include <Windows.h>
}
using namespace std;
string wstring_to_string(const wstring& wstr)
{
if (wstr.length() > 0)
{
int utf8_size = WideCharToMultiByte(
CP_UTF8, 0, wstr.c_str(), wstr.length(),
nullptr, 0, nullptr, nullptr);
if (utf8_size == 0)
{
throw win32_error_exception(GetLastError(), L"Converting to unicode string failed: ");
}
LPSTR c = new char[utf8_size];
if (WideCharToMultiByte(
CP_UTF8, 0, wstr.c_str(), wstr.length(),
c, utf8_size, nullptr, nullptr) != utf8_size)
{
delete[] c;
throw win32_error_exception(GetLastError(), L"Converting to unicode string failed: ");
}
string str(c, utf8_size);
delete[] c;
return str;
}
else
{
return string();
}
}
wstring string_to_wstring(const string& str)
{
if (str.length() > 0)
{
int wide_size = MultiByteToWideChar(
CP_UTF8, 0, str.c_str(), str.length(),
nullptr, 0);
if (wide_size == 0)
{
throw win32_error_exception(GetLastError(), L"Converting to wstring failed: ");
}
LPWSTR w = new wchar_t[wide_size];
if (MultiByteToWideChar(
CP_UTF8, 0, str.c_str(), str.length(),
w, wide_size) != wide_size)
{
delete[] w;
throw win32_error_exception(GetLastError(), L"Converting to wstring failed: ");
}
wstring wstr(w, wide_size);
delete[] w;
return wstr;
}
else
{
return wstring();
}
}