LogIt++
Loading...
Searching...
No Matches
encoding_utils.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifndef _LOGIT_ENCODING_UTILS_HPP_INCLUDED
4#define _LOGIT_ENCODING_UTILS_HPP_INCLUDED
5
8
9#include <string>
10
11#if defined(_WIN32)
12#ifndef NOMINMAX
13#define NOMINMAX
14#endif
15#include <stringapiset.h>
16
17namespace logit {
18
22 inline std::string utf8_to_ansi(const std::string& utf8) {
23 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
24 if (n_len == 0) return {};
25
26 std::wstring wide_string(n_len + 1, L'\0');
27 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
28
29 n_len = WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, NULL, 0, NULL, NULL);
30 if (n_len == 0) return {};
31
32 std::string ansi_string(n_len - 1, '\0');
33 WideCharToMultiByte(CP_ACP, 0, wide_string.c_str(), -1, &ansi_string[0], n_len, NULL, NULL);
34 return ansi_string;
35 }
36
40 inline std::wstring utf8_to_wstring(const std::string& utf8) {
41 int n_len = MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, NULL, 0);
42 if (n_len == 0) return {};
43 std::wstring wide_string(n_len - 1, L'\0');
44 MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), -1, &wide_string[0], n_len);
45 return wide_string;
46 }
47
51 inline std::string wstring_to_utf8(const std::wstring& wide) {
52 int n_len = WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, NULL, 0, NULL, NULL);
53 if (n_len == 0) return {};
54 std::string utf8(n_len - 1, '\0');
55 WideCharToMultiByte(CP_UTF8, 0, wide.c_str(), -1, &utf8[0], n_len, NULL, NULL);
56 return utf8;
57 }
58
59} // namespace logit
60#endif // defined(_WIN32)
61
62#endif // _LOGIT_ENCODING_UTILS_HPP_INCLUDED
The primary namespace for the LogIt++ library.