Consolix
Loading...
Searching...
No Matches
encoding_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_ENCODING_UTILS_HPP_INCLUDED
3#define _CONSOLIX_ENCODING_UTILS_HPP_INCLUDED
4
8
9#if defined(_WIN32) || defined(_WIN64)
10
11#include <string>
12
13namespace consolix {
14
18 std::string utf8_to_ansi(const std::string& utf8) noexcept {
19 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
20 if (n_len == 0) return {};
21
22 std::wstring wide_string(n_len + 1, L'\0');
23 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, wide_string.data(), n_len);
24
25 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
26 if (n_len == 0) return {};
27
28 std::string ansi_string(n_len - 1, '\0');
29 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, ansi_string.data(), n_len, NULL, NULL);
30 return ansi_string;
31 }
32
36 std::string ansi_to_utf8(const std::string& ansi) noexcept {
37 int n_len = MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, NULL, 0);
38 if (n_len == 0) return {};
39
40 std::wstring wide_string(n_len, L'\0');
41 MultiByteToWideChar(CP_ACP, 0, ansi.c_str(), -1, wide_string.data(), n_len);
42
43 n_len = WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
44 if (n_len == 0) return {};
45
46 std::string utf8_string(n_len - 1, '\0');
47 WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, utf8_string.data(), n_len, NULL, NULL);
48 return utf8_string;
49 }
50
54 std::string utf8_to_cp866(const std::string& utf8) noexcept {
55 std::string temp = utf8_to_ansi(utf8);
56 CharToOem((LPSTR)temp.c_str(), temp.data());
57 return temp;
58 }
59
63 bool is_valid_utf8(const char* message) {
64 if (!message) return true;
65 const unsigned char* bytes = reinterpret_cast<const unsigned char*>(message);
66 while (*bytes != 0x00) {
67 int num = 0;
68 unsigned int cp = 0;
69 if ((*bytes & 0x80) == 0x00) { cp = (*bytes & 0x7F); num = 1; }
70 else if ((*bytes & 0xE0) == 0xC0) { cp = (*bytes & 0x1F); num = 2; }
71 else if ((*bytes & 0xF0) == 0xE0) { cp = (*bytes & 0x0F); num = 3; }
72 else if ((*bytes & 0xF8) == 0xF0) { cp = (*bytes & 0x07); num = 4; }
73 else return false;
74
75 bytes++;
76 for (int i = 1; i < num; ++i) {
77 if ((*bytes & 0xC0) != 0x80) return false;
78 cp = (cp << 6) | (*bytes & 0x3F);
79 bytes++;
80 }
81
82 if (cp > 0x10FFFF || (cp >= 0xD800 && cp <= 0xDFFF)) return false;
83 }
84 return true;
85 }
86
90 std::string cp1251_to_utf8(const std::string& cp1251) {
91 int len = MultiByteToWideChar(1251, 0, cp1251.c_str(), -1, NULL, 0);
92 if (len == 0) return {};
93
94 std::wstring wideString(len, L'\0');
95 MultiByteToWideChar(1251, 0, cp1251.c_str(), -1, wideString.data(), len);
96
97 len = WideCharToMultiByte(CP_UTF8, 0, wideString.c_str(), -1, NULL, 0, NULL, NULL);
98 if (len == 0) return {};
99
100 std::string utf8String(len, '\0');
101 WideCharToMultiByte(CP_UTF8, 0, wideString.c_str(), -1, utf8String.data(), len, NULL, NULL);
102 return utf8String;
103 }
104
108 std::string utf16_to_utf8(LPWSTR utf16String) {
109 int bufferSize = WideCharToMultiByte(CP_UTF8, 0, utf16String, -1, nullptr, 0, nullptr, nullptr);
110 std::string utf8String(bufferSize, '\0');
111 WideCharToMultiByte(CP_UTF8, 0, utf16String, -1, utf8String.data(), bufferSize, nullptr, nullptr);
112 return utf8String;
113 }
114
118 std::wstring utf8_to_utf16(const std::string& utf8) noexcept {
119 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
120 if (n_len == 0) return {};
121
122 std::wstring utf16_string(n_len, L'\0');
123 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, utf16_string.data(), n_len);
124 return utf16_string;
125 }
126
127} // namespace consolix
128
129#endif
130
131#endif // _CONSOLIX_ENCODING_UTILS_HPP_INCLUDED
< Utility modules and helpers.