use boost.locale for charset conversions instead of iconv
This commit is contained in:
120
src/charset.cpp
120
src/charset.cpp
@@ -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
|
||||
|
||||
@@ -21,34 +21,16 @@
|
||||
#ifndef NCMPCPP_CHARSET_H
|
||||
#define NCMPCPP_CHARSET_H
|
||||
|
||||
#include "config.h"
|
||||
#include <string>
|
||||
|
||||
namespace IConv {//
|
||||
namespace Charset {//
|
||||
|
||||
#ifdef HAVE_ICONV_H
|
||||
|
||||
void convertFromTo(const char *from, const char *to, std::string &s);
|
||||
std::string toUtf8From(std::string s, const char *charset);
|
||||
std::string fromUtf8To(std::string s, const char *charset);
|
||||
|
||||
std::string utf8ToLocale(std::string s);
|
||||
std::string localeToUtf8(std::string s);
|
||||
|
||||
void utf8ToLocale_(std::string &s);
|
||||
void localeToUtf8_(std::string &s);
|
||||
|
||||
#else
|
||||
|
||||
inline void convertFromTo(const char *, const char *, std::string &) { }
|
||||
|
||||
inline std::string utf8ToLocale(std::string s) { return s; }
|
||||
inline std::string localeToUtf8(std::string s) { return s; }
|
||||
|
||||
inline void utf8ToLocale_(std::string &) { }
|
||||
inline void localeToUtf8_(std::string &) { }
|
||||
|
||||
#endif // HAVE_ICONV_H
|
||||
|
||||
}
|
||||
|
||||
#endif // NCMPCPP_CHARSET_H
|
||||
|
||||
|
||||
@@ -89,9 +89,6 @@ void ParseArgv(int argc, char **argv)
|
||||
# ifdef HAVE_CURL_CURL_H
|
||||
<< " curl"
|
||||
# endif
|
||||
# ifdef HAVE_ICONV_H
|
||||
<< " iconv"
|
||||
# endif
|
||||
# ifdef HAVE_FFTW3_H
|
||||
<< " fftw"
|
||||
# endif
|
||||
@@ -188,7 +185,7 @@ void ParseArgv(int argc, char **argv)
|
||||
replace(now_playing_format, "\\t", "\t");
|
||||
}
|
||||
}
|
||||
std::cout << IConv::utf8ToLocale(
|
||||
std::cout << Charset::utf8ToLocale(
|
||||
Mpd.GetCurrentlyPlayingSong().toString(now_playing_format, Config.tags_separator)) << "\n";
|
||||
}
|
||||
exit(0);
|
||||
|
||||
@@ -127,12 +127,11 @@ void Lastfm::Load()
|
||||
{
|
||||
bool first = 1;
|
||||
std::string line;
|
||||
while (getline(input, line))
|
||||
while (std::getline(input, line))
|
||||
{
|
||||
if (!first)
|
||||
w << '\n';
|
||||
IConv::utf8ToLocale_(line);
|
||||
w << line;
|
||||
w << Charset::utf8ToLocale(line);
|
||||
first = 0;
|
||||
}
|
||||
input.close();
|
||||
@@ -171,8 +170,7 @@ void Lastfm::Download()
|
||||
{
|
||||
Save(result.second);
|
||||
w.clear();
|
||||
IConv::utf8ToLocale_(result.second);
|
||||
w << result.second;
|
||||
w << Charset::utf8ToLocale(result.second);
|
||||
itsService->colorizeOutput(w);
|
||||
}
|
||||
else
|
||||
|
||||
@@ -249,8 +249,7 @@ void *Lyrics::Download()
|
||||
{
|
||||
Save(itsFilename, result.second);
|
||||
w.clear();
|
||||
IConv::utf8ToLocale_(result.second);
|
||||
w << result.second;
|
||||
w << Charset::utf8ToLocale(result.second);
|
||||
}
|
||||
else
|
||||
w << '\n' << L"Lyrics weren't found.";
|
||||
@@ -320,12 +319,11 @@ void Lyrics::Load()
|
||||
{
|
||||
bool first = 1;
|
||||
std::string line;
|
||||
while (getline(input, line))
|
||||
while (std::getline(input, line))
|
||||
{
|
||||
if (!first)
|
||||
w << '\n';
|
||||
IConv::utf8ToLocale_(line);
|
||||
w << line;
|
||||
w << Charset::utf8ToLocale(line);
|
||||
first = 0;
|
||||
}
|
||||
w.flush();
|
||||
|
||||
@@ -212,7 +212,7 @@ void LyricstimeFetcher::postProcess(std::string &data)
|
||||
{
|
||||
// lyricstime.com uses iso-8859-1 as the encoding
|
||||
// so we need to convert obtained lyrics to utf-8
|
||||
IConv::convertFromTo("iso-8859-1", "utf-8", data);
|
||||
data = Charset::toUtf8From(data, "iso-8859-1");
|
||||
LyricsFetcher::postProcess(data);
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ void LyricsmaniaFetcher::postProcess(std::string &data)
|
||||
{
|
||||
// lyricsmania.com uses iso-8859-1 as the encoding
|
||||
// so we need to convert obtained lyrics to utf-8
|
||||
IConv::convertFromTo("iso-8859-1", "utf-8", data);
|
||||
data = Charset::toUtf8From(data, "iso-8859-1");
|
||||
LyricsFetcher::postProcess(data);
|
||||
}
|
||||
|
||||
|
||||
@@ -314,6 +314,7 @@ void MediaLibrary::update()
|
||||
}
|
||||
if (idx < Tags.size())
|
||||
Tags.resizeList(idx);
|
||||
std::sort(Tags.beginV(), Tags.endV(), SortPrimaryTags());
|
||||
});
|
||||
Tags.refresh();
|
||||
}
|
||||
|
||||
@@ -89,8 +89,8 @@ int main(int argc, char **argv)
|
||||
using Global::VolumeState;
|
||||
using Global::Timer;
|
||||
|
||||
srand(time(0));
|
||||
setlocale(LC_ALL, "");
|
||||
std::srand(std::time(0));
|
||||
std::setlocale(LC_ALL, "");
|
||||
std::locale::global(std::locale(""));
|
||||
|
||||
Config.CheckForCommandLineConfigFilePath(argv, argc);
|
||||
|
||||
@@ -330,8 +330,8 @@ void Status::Changes::elapsedTime()
|
||||
}
|
||||
|
||||
NC::WBuffer first, second;
|
||||
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, Config.tags_separator, "$"))), first);
|
||||
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, Config.tags_separator, "$"))), second);
|
||||
stringToBuffer(ToWString(Charset::utf8ToLocale(np.toString(Config.new_header_first_line, Config.tags_separator, "$"))), first);
|
||||
stringToBuffer(ToWString(Charset::utf8ToLocale(np.toString(Config.new_header_second_line, Config.tags_separator, "$"))), second);
|
||||
|
||||
size_t first_len = wideLength(first.str());
|
||||
size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2;
|
||||
@@ -382,7 +382,7 @@ void Status::Changes::elapsedTime()
|
||||
tracklength += "]";
|
||||
}
|
||||
NC::WBuffer np_song;
|
||||
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, Config.tags_separator, "$"))), np_song);
|
||||
stringToBuffer(ToWString(Charset::utf8ToLocale(np.toString(Config.song_status_format, Config.tags_separator, "$"))), np_song);
|
||||
*wFooter << NC::XY(0, 1) << wclrtoeol << NC::fmtBold << player_state << NC::fmtBoldEnd;
|
||||
np_song.write(*wFooter, playing_song_scroll_begin, wFooter->getWidth()-player_state.length()-tracklength.length(), L" ** ");
|
||||
*wFooter << NC::fmtBold << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength << NC::fmtBoldEnd;
|
||||
|
||||
@@ -18,31 +18,17 @@
|
||||
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||||
***************************************************************************/
|
||||
|
||||
#include <boost/locale/encoding.hpp>
|
||||
#include "utility/wide_string.h"
|
||||
|
||||
std::string ToString(const std::wstring &ws)
|
||||
std::string ToString(std::wstring ws)
|
||||
{
|
||||
std::string result;
|
||||
char s[MB_CUR_MAX];
|
||||
for (size_t i = 0; i < ws.length(); ++i)
|
||||
{
|
||||
int n = wcrtomb(s, ws[i], 0);
|
||||
if (n > 0)
|
||||
result.append(s, n);
|
||||
}
|
||||
return result;
|
||||
return boost::locale::conv::utf_to_utf<char>(ws);
|
||||
}
|
||||
|
||||
std::wstring ToWString(const std::string &s)
|
||||
std::wstring ToWString(std::string s)
|
||||
{
|
||||
std::wstring result;
|
||||
wchar_t *ws = new wchar_t[s.length()];
|
||||
const char *c_s = s.c_str();
|
||||
int n = mbsrtowcs(ws, &c_s, s.length(), 0);
|
||||
if (n > 0)
|
||||
result.append(ws, n);
|
||||
delete [] ws;
|
||||
return result;
|
||||
return boost::locale::conv::utf_to_utf<wchar_t>(s);
|
||||
}
|
||||
|
||||
size_t wideLength(const std::wstring &ws)
|
||||
|
||||
@@ -23,8 +23,8 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
std::string ToString(const std::wstring &ws);
|
||||
std::wstring ToWString(const std::string &s);
|
||||
std::string ToString(std::wstring ws);
|
||||
std::wstring ToWString(std::string s);
|
||||
|
||||
size_t wideLength(const std::wstring &ws);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user