-
Notifications
You must be signed in to change notification settings - Fork 15
/
PotConv.cpp
71 lines (64 loc) · 1.52 KB
/
PotConv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#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];
}
}