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