Kurlyk
Loading...
Searching...
No Matches
encoding_utils.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef KURLYK_HEADER_KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED
3#define KURLYK_HEADER_KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED
4
7
8namespace kurlyk {
9namespace utils {
10
11# if defined(_WIN32)
15 inline std::string utf8_to_ansi(const std::string& utf8) noexcept {
16 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
17 if (n_len == 0) return {};
18
19 std::wstring wide_string(n_len + 1, L'\0');
20 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
21
22 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
23 if (n_len == 0) return {};
24
25 std::string ansi_string(n_len - 1, '\0');
26 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, &ansi_string[0], n_len, NULL, NULL);
27 return ansi_string;
28 }
29# endif
30
31} // namespace utils
32} // namespace kurlyk
33
34#endif // KURLYK_HEADER_KURLYK_UTILS_ENCODING_UTILS_HPP_INCLUDED
Primary namespace for the Kurlyk library, encompassing initialization, request management,...