1 Star 0 Fork 0

陈祎麒/mlcc

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
PotConv.cpp 1.52 KB
一键复制 编辑 原始数据 按行查看 历史
sb500 提交于 2022-01-10 16:12 . 添加PotIconv
#include "PotConv.h"
PotConv::PotConv()
{
}
PotConv::~PotConv()
{
for (auto& cd : cds_)
{
iconv_close(cd.second);
}
}
std::string PotConv::conv(const std::string& src, const char* from, const char* to)
{
//const char *from_charset, const char *to_charset, const char *inbuf, size_t inlen, char *outbuf;
iconv_t cd = createcd(from, to);
if (cd == nullptr)
{
return "";
}
size_t inlen = src.length();
size_t outlen = src.length() * 2;
auto in = new char[inlen + 1];
auto out = new char[outlen + 1];
memset(in, 0, inlen + 1);
memcpy(in, src.c_str(), inlen);
memset(out, 0, outlen + 1);
char *pin = in, *pout = out;
if (iconv(cd, &pin, &inlen, &pout, &outlen) == -1)
{
out[0] = '\0';
}
std::string result(out, src.length() * 2 - outlen);
delete[] in;
delete[] out;
return result;
}
std::string PotConv::conv(const std::string& src, const std::string& from, const std::string& to)
{
return conv(src, from.c_str(), to.c_str());
}
std::string PotConv::to_read(const std::string& src)
{
#ifdef _WIN32
return conv(src, "utf-8", "cp936");
#else
return src;
#endif
}
PotConv PotConv::potconv_;
iconv_t PotConv::createcd(const char* from, const char* to)
{
std::string cds = std::string(from) + std::string(to);
if (potconv_.cds_.count(cds) == 0)
{
iconv_t cd;
cd = iconv_open(to, from);
potconv_.cds_[cds] = cd;
return cd;
}
else
{
return potconv_.cds_[cds];
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/chen-yiqi-duke/mlcc.git
git@gitee.com:chen-yiqi-duke/mlcc.git
chen-yiqi-duke
mlcc
mlcc
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385