use boost.locale for charset conversions instead of iconv

This commit is contained in:
Andrzej Rybczak
2012-10-04 21:25:48 +02:00
parent 802886c2e5
commit e40edade0e
12 changed files with 53 additions and 202 deletions

View File

@@ -18,130 +18,34 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include <boost/locale/encoding.hpp>
#include "charset.h"
#ifdef HAVE_ICONV_H
#include <iconv.h>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include "settings.h"
namespace {//
namespace Charset {//
bool is_utf8(const char *s)
std::string toUtf8From(std::string s, const char *charset)
{
for (; *s; ++s)
{
if (*s & 0x80) // 1xxxxxxx
{
char c = 0x40;
unsigned i = 0;
while (c & *s)
++i, c >>= 1;
if (i < 1 || i > 3) // not 110xxxxx, 1110xxxx, 11110xxx
return false;
for (unsigned j = 0; j < i; ++j)
if (!*++s || !(*s & 0x80) || *s & 0x40) // 10xxxxxx
return false;
}
}
return true;
return boost::locale::conv::to_utf<char>(s, charset);
}
bool has_non_ascii_chars(const char *s)
std::string fromUtf8To(std::string s, const char *charset)
{
for (; *s; ++s)
if (*s & 0x80)
return true;
return false;
}
void charset_convert(const char *from, const char *to, const char *&inbuf,
bool delete_old, size_t len = 0)
{
assert(inbuf);
assert(from);
assert(to);
iconv_t cd = iconv_open(to, from);
if (cd == iconv_t(-1))
{
std::cerr << "Error while executing iconv_open: " << strerror(errno) << "\n";
return;
}
if (!len)
len = strlen(inbuf);
size_t buflen = len*MB_CUR_MAX+1;
char *outbuf = static_cast<char *>(malloc(buflen));
char *outstart = outbuf;
const char *instart = inbuf;
if (iconv(cd, const_cast<ICONV_CONST char **>(&inbuf), &len, &outbuf, &buflen) == size_t(-1))
{
std::cerr << "Error while executing iconv: " << strerror(errno) << "\n";
inbuf = instart;
delete [] outstart;
}
else
{
*outbuf = 0;
if (delete_old)
free(const_cast<char *>(instart));
inbuf = outstart;
}
iconv_close(cd);
}
}
namespace IConv {//
void convertFromTo(const char *from, const char *to, std::string &s)
{
const char *tmp = strdup(s.c_str());
charset_convert(from, to, tmp, true, s.length());
s = tmp;
free(const_cast<char *>(tmp));
return boost::locale::conv::to_utf<char>(s, charset);
}
std::string utf8ToLocale(std::string s)
{
utf8ToLocale_(s);
return s;
}
void utf8ToLocale_(std::string &s)
{
if (Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()))
return;
const char *tmp = strdup(s.c_str());
charset_convert("utf-8", Config.system_encoding.c_str(), tmp, 1, s.length());
s = tmp;
free(const_cast<char *>(tmp));
return Config.system_encoding.empty()
? s
: boost::locale::conv::from_utf<char>(s, Config.system_encoding);
}
std::string localeToUtf8(std::string s)
{
localeToUtf8_(s);
return s;
}
void localeToUtf8_(std::string &s)
{
if (Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()) || is_utf8(s.c_str()))
return;
const char *tmp = strdup(s.c_str());
charset_convert(Config.system_encoding.c_str(), "utf-8", tmp, 1, s.length());
s = tmp;
free(const_cast<char *>(tmp));
return Config.system_encoding.empty()
? s
: boost::locale::conv::to_utf<char>(s, Config.system_encoding);
}
}
#endif // HAVE_ICONV_H