-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrConvert.cpp
More file actions
115 lines (98 loc) · 3.68 KB
/
StrConvert.cpp
File metadata and controls
115 lines (98 loc) · 3.68 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
#include "StrConvert.h"
#include <windows.h>
#include <vector>
#include <string>
#include "WinUtils.h"
#include "Logger.h"
using namespace std;
using namespace WinUtils;
static Logger logger(TS("StrConvert"));
string WinUtils::WideStringToUtf8(const wstring_view wide_str) noexcept
{
if (wide_str.empty()) return {};
int buffer_size = WideCharToMultiByte(CP_UTF8, 0, wide_str.data(), (int)wide_str.length(),
nullptr, 0, nullptr, nullptr);
if (buffer_size <= 0)
{
logger.DLog(LogLevel::Error, format(TS("WideStringToUtf8: Failed to calculate length - {}"), GetWindowsErrorMsg(GetLastError())));
return {};
}
string utf8_str;
utf8_str.resize(buffer_size);
WideCharToMultiByte(CP_UTF8, 0, wide_str.data(), (int)wide_str.length(),
utf8_str.data(), buffer_size, nullptr, nullptr);
return utf8_str;
}
wstring WinUtils::MultiByteToWide(const char* mb_str, UINT code_page) noexcept
{
if (mb_str == nullptr || *mb_str == '\0')
{
return {};
}
// Calculate required buffer length
const int len = MultiByteToWideChar(code_page, 0, mb_str, -1, nullptr, 0);
if (len == 0)
{
const DWORD err = GetLastError();
logger.DLog(LogLevel::Error, format(TS("MultiByteToWide: Failed to calculate length [Code Page:{}] - {}"), code_page, GetWindowsErrorMsg(err)));
return {};
}
vector<wchar_t> wide_buf(static_cast<size_t>(len));
const int res = MultiByteToWideChar(code_page, 0, mb_str, -1, wide_buf.data(), len);
if (res == 0)
{
const DWORD err = GetLastError();
logger.DLog(LogLevel::Error, format(TS("MultiByteToWide: Conversion failed [Code Page:{}] - {}"), code_page, GetWindowsErrorMsg(err)));
return {};
}
return wide_buf.data();
}
// Convert ANSI (char*) to wide string (wstring/Unicode)
wstring WinUtils::AnsiToWideString(const char* ansi_str) noexcept
{
return MultiByteToWide(ansi_str, CP_ACP);
}
// Convert UTF-8 (char*) to wide string (wstring/Unicode)
wstring WinUtils::Utf8ToWideString(const char* utf8_str) noexcept
{
return MultiByteToWide(utf8_str, CP_UTF8);
}
// Convert wide string (wstring/Unicode) to ANSI (char*)
[[nodiscard]] char* WinUtils::WideStringToAnsi(const wstring_view wide_str) noexcept
{
if (wide_str.empty())return nullptr;
const int len = WideCharToMultiByte(CP_ACP, 0, wide_str.data(), -1, nullptr, 0, nullptr, nullptr);
if (len == 0)
{
logger.DLog(LogLevel::Error, format(TS("WideStringToAnsi: Failed to calculate length - {}"), GetWindowsErrorMsg(GetLastError())));
return nullptr;
}
char* ansi_buf = new char[static_cast<size_t>(len)];
const int res = WideCharToMultiByte(CP_ACP, 0, wide_str.data(), -1, ansi_buf, len, nullptr, nullptr);
if (res == 0)
{
logger.DLog(LogLevel::Error, format(TS("WideStringToAnsi: Conversion failed - {}"), GetWindowsErrorMsg(GetLastError())));
delete[] ansi_buf;
return nullptr;
}
return ansi_buf;
}
// Convert UTF-8 encoded wide string (wstring) to standard Unicode wide string (wstring)
wstring WinUtils::Utf8WideStringToWideString(const wstring_view utf8_wide_str) noexcept
{
if (utf8_wide_str.empty())return {};
const int ansi_len = WideCharToMultiByte(CP_ACP, 0, utf8_wide_str.data(), -1, nullptr, 0, nullptr, nullptr);
if (ansi_len == 0)
{
logger.DLog(LogLevel::Error, format(TS("Utf8WideStringToWideString: Failed to calculate ANSI length - {}"), GetWindowsErrorMsg(GetLastError())));
return {};
}
vector<char> ansi_buf(static_cast<size_t>(ansi_len));
const int convert_ansi = WideCharToMultiByte(CP_ACP, 0, utf8_wide_str.data(), -1, ansi_buf.data(), ansi_len, nullptr, nullptr);
if (convert_ansi == 0)
{
logger.DLog(LogLevel::Error, format(TS("Utf8WideStringToWideString: ANSI conversion failed - {}"), GetWindowsErrorMsg(GetLastError())));
return {};
}
return MultiByteToWide(ansi_buf.data(), CP_UTF8);
}