标签:inter end https codec 支持 efs war protect targe
如果只想在Windows 平台下使用 可以参考 我的这篇文章
https://www.cnblogs.com/guolongzheng/p/13939527.html
代码片段
#pragma once
#include <string>
#include <codecvt>
#include <locale>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)
#define GBK_LOCALE ".936"
//Windows平台下 在visual studio 的 预处理中添加 宏定义 _SILENCE_ALL_CXX17_DEPRECATION_WARNINGS 否则会报错误
#else
#define GBK_LOCALE "zh_CN.GBK"
#endif
//为了兼容 Linux 系统下 析构函数不能设为 protected 问题
namespace std
{
template<typename _InternT, typename _ExternT, typename _StateT>
struct my_codecvt_byname : public codecvt_byname<_InternT, _ExternT, _StateT>
{
explicit my_codecvt_byname(const char* __s, long unsigned int __refs = 0): codecvt_byname<_InternT, _ExternT, _StateT>(__s, __refs){}
explicit my_codecvt_byname(long unsigned int __refs = 0) : codecvt_byname<_InternT, _ExternT, _StateT>("C",__refs) {}
#if __cplusplus >= 201103L
explicit my_codecvt_byname(const string& __s, long unsigned int __refs = 0):my_codecvt_byname(__s.c_str(), __refs) {}
#endif
virtual ~my_codecvt_byname() { }
};
}
namespace cool
{
class Encoder {
public:
Encoder() = default;
virtual ~Encoder() = default;
inline std::string unicode_to_gbk(const std::wstring& str_value) {
auto utf8_value = unicode_to_utf8(str_value);
return utf8_to_gbk(utf8_value);
}
inline std::wstring gbk_to_unicode(const std::string& str_value) {
auto gbk_value = gbk_to_utf8(str_value);
return utf8_to_unicode(gbk_value);
}
inline std::string unicode_to_utf8(const std::wstring& str_value) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.to_bytes(str_value);
}
inline std::wstring utf8_to_unicode(const std::string& str_value) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
return conv.from_bytes(str_value);
}
inline std::string utf8_to_gbk(const std::string& str_value) {
auto wstr_value = utf8_to_unicode(str_value);
using utf8_and_gbk = std::my_codecvt_byname<wchar_t, char, std::mbstate_t>;
std::wstring_convert<utf8_and_gbk> conv(new utf8_and_gbk(GBK_LOCALE));
return conv.to_bytes(wstr_value);
}
inline std::string gbk_to_utf8(const std::string& str_value) {
using utf8_and_gbk = std::my_codecvt_byname<wchar_t, char, std::mbstate_t>;
std::wstring_convert<utf8_and_gbk> conv(new utf8_and_gbk(GBK_LOCALE));
auto wstr_value = conv.from_bytes(str_value);
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv2;
return conv2.to_bytes(wstr_value);
}
};
}
标签:inter end https codec 支持 efs war protect targe
原文地址:https://www.cnblogs.com/guolongzheng/p/14746994.html