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

@@ -15,7 +15,6 @@ AC_ARG_ENABLE(clock, AS_HELP_STRING([--enable-clock], [Enable clock screen @<:@d
AC_ARG_ENABLE(unicode, AS_HELP_STRING([--enable-unicode], [Enable utf8 support @<:@default=yes@:>@]), [unicode=$enableval], [unicode=yes]) AC_ARG_ENABLE(unicode, AS_HELP_STRING([--enable-unicode], [Enable utf8 support @<:@default=yes@:>@]), [unicode=$enableval], [unicode=yes])
AC_ARG_WITH(curl, AS_HELP_STRING([--with-curl], [Enable fetching lyrics from the Internet @<:@default=auto@:>@]), [curl=$withval], [curl=auto]) AC_ARG_WITH(curl, AS_HELP_STRING([--with-curl], [Enable fetching lyrics from the Internet @<:@default=auto@:>@]), [curl=$withval], [curl=auto])
AC_ARG_WITH(fftw, AS_HELP_STRING([--with-fftw], [Enable fftw support (required for frequency spectrum vizualization) @<:@default=auto@:>@]), [fftw=$withval], [fftw=auto]) AC_ARG_WITH(fftw, AS_HELP_STRING([--with-fftw], [Enable fftw support (required for frequency spectrum vizualization) @<:@default=auto@:>@]), [fftw=$withval], [fftw=auto])
AC_ARG_WITH(iconv, AS_HELP_STRING([--with-iconv], [Enable iconv support (Note: if you use utf-8 system wide, you can disable this) @<:@default=auto@:>@]), [iconv=$withval], [iconv=auto])
AC_ARG_WITH(pdcurses, AS_HELP_STRING([--with-pdcurses[=LIBNAME]], [Link against pdcurses instead of ncurses @<:@default=XCurses@:>@]), [pdcurses=$withval], [pdcurses=no]) AC_ARG_WITH(pdcurses, AS_HELP_STRING([--with-pdcurses[=LIBNAME]], [Link against pdcurses instead of ncurses @<:@default=XCurses@:>@]), [pdcurses=$withval], [pdcurses=no])
AC_ARG_WITH(taglib, AS_HELP_STRING([--with-taglib], [Enable tag editor @<:@default=auto@:>@]), [taglib=$withval], [taglib=auto]) AC_ARG_WITH(taglib, AS_HELP_STRING([--with-taglib], [Enable tag editor @<:@default=auto@:>@]), [taglib=$withval], [taglib=auto])
@@ -27,19 +26,6 @@ if test "$clock" = "yes"; then
AC_DEFINE([ENABLE_CLOCK], [1], [enables clock screen]) AC_DEFINE([ENABLE_CLOCK], [1], [enables clock screen])
fi fi
dnl =====================================
dnl = checking for -fno-exceptions flag =
dnl =====================================
AC_MSG_CHECKING([whether compiler supports -fno-exceptions])
old_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="-fno-exceptions"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[ ]])],
AC_MSG_RESULT([yes])
no_exceptions="-fno-exceptions",
AC_MSG_RESULT([no])
)
CXXFLAGS="$old_CXXFLAGS $no_exceptions"
dnl ================================ dnl ================================
dnl = checking for -std=c++0x flag = dnl = checking for -std=c++0x flag =
dnl ================================ dnl ================================
@@ -96,6 +82,22 @@ AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[struct A { virtual void foo() { } }; struct
AC_DEFINE([OVERRIDE], []), AC_DEFINE([OVERRIDE], []),
) )
dnl =============================
dnl = setting boost environment =
dnl =============================
AS_IF([test -z "${BOOST_LIB_SUFFIX+x}"], [BOOST_LIB_SUFFIX=-mt])
AC_ARG_VAR([BOOST_LIB_SUFFIX], [Boost library name suffix [default=-mt]])
dnl =============================
dnl = checking for boost.locale =
dnl =============================
AC_CHECK_HEADERS([boost/locale/encoding.hpp], ,
AC_MSG_ERROR(boost/locale/encoding.hpp is missing)
)
AC_CHECK_LIB(boost_locale$BOOST_LIB_SUFFIX, main, LDFLAGS="$LDFLAGS -lboost_locale$BOOST_LIB_SUFFIX",
AC_MSG_ERROR([no boost.locale library found])
)
dnl ============================== dnl ==============================
dnl = checking for regex (win32) = dnl = checking for regex (win32) =
dnl ============================== dnl ==============================
@@ -132,23 +134,6 @@ AC_CHECK_HEADERS([pthread.h],
), ),
) )
dnl ======================
dnl = checking for iconv =
dnl ======================
if test "$iconv" != "no" ; then
AC_CHECK_HEADERS([iconv.h],
AC_MSG_CHECKING([whether iconv takes const char **])
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[#include <iconv.h>]], [[iconv(0, (const char **)0, 0, 0, 0);]])],
AC_MSG_RESULT([yes]) AC_DEFINE([ICONV_CONST], [const], [pass const pointer to iconv]),
AC_MSG_RESULT([no]) AC_DEFINE([ICONV_CONST], [], [pass non-const pointer to iconv]))
AC_CHECK_LIB(iconv, libiconv, LDFLAGS="$LDFLAGS -liconv", )
,
if test "$iconv" = "yes"; then
AC_MSG_ERROR([iconv.h header is required])
fi
)
fi
dnl ======================== dnl ========================
dnl = checking for ncurses = dnl = checking for ncurses =
dnl ======================== dnl ========================

View File

@@ -18,130 +18,34 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/locale/encoding.hpp>
#include "charset.h" #include "charset.h"
#ifdef HAVE_ICONV_H
#include <iconv.h>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include "settings.h" #include "settings.h"
namespace {// namespace Charset {//
bool is_utf8(const char *s) std::string toUtf8From(std::string s, const char *charset)
{ {
for (; *s; ++s) return boost::locale::conv::to_utf<char>(s, charset);
{
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;
} }
bool has_non_ascii_chars(const char *s) std::string fromUtf8To(std::string s, const char *charset)
{ {
for (; *s; ++s) return boost::locale::conv::to_utf<char>(s, charset);
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));
} }
std::string utf8ToLocale(std::string s) std::string utf8ToLocale(std::string s)
{ {
utf8ToLocale_(s); return Config.system_encoding.empty()
return s; ? s
} : boost::locale::conv::from_utf<char>(s, Config.system_encoding);
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));
} }
std::string localeToUtf8(std::string s) std::string localeToUtf8(std::string s)
{ {
localeToUtf8_(s); return Config.system_encoding.empty()
return s; ? s
} : boost::locale::conv::to_utf<char>(s, Config.system_encoding);
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));
} }
} }
#endif // HAVE_ICONV_H

View File

@@ -21,34 +21,16 @@
#ifndef NCMPCPP_CHARSET_H #ifndef NCMPCPP_CHARSET_H
#define NCMPCPP_CHARSET_H #define NCMPCPP_CHARSET_H
#include "config.h"
#include <string> #include <string>
namespace IConv {// namespace Charset {//
#ifdef HAVE_ICONV_H std::string toUtf8From(std::string s, const char *charset);
std::string fromUtf8To(std::string s, const char *charset);
void convertFromTo(const char *from, const char *to, std::string &s);
std::string utf8ToLocale(std::string s); std::string utf8ToLocale(std::string s);
std::string localeToUtf8(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 #endif // NCMPCPP_CHARSET_H

View File

@@ -89,9 +89,6 @@ void ParseArgv(int argc, char **argv)
# ifdef HAVE_CURL_CURL_H # ifdef HAVE_CURL_CURL_H
<< " curl" << " curl"
# endif # endif
# ifdef HAVE_ICONV_H
<< " iconv"
# endif
# ifdef HAVE_FFTW3_H # ifdef HAVE_FFTW3_H
<< " fftw" << " fftw"
# endif # endif
@@ -188,7 +185,7 @@ void ParseArgv(int argc, char **argv)
replace(now_playing_format, "\\t", "\t"); replace(now_playing_format, "\\t", "\t");
} }
} }
std::cout << IConv::utf8ToLocale( std::cout << Charset::utf8ToLocale(
Mpd.GetCurrentlyPlayingSong().toString(now_playing_format, Config.tags_separator)) << "\n"; Mpd.GetCurrentlyPlayingSong().toString(now_playing_format, Config.tags_separator)) << "\n";
} }
exit(0); exit(0);

View File

@@ -127,12 +127,11 @@ void Lastfm::Load()
{ {
bool first = 1; bool first = 1;
std::string line; std::string line;
while (getline(input, line)) while (std::getline(input, line))
{ {
if (!first) if (!first)
w << '\n'; w << '\n';
IConv::utf8ToLocale_(line); w << Charset::utf8ToLocale(line);
w << line;
first = 0; first = 0;
} }
input.close(); input.close();
@@ -171,8 +170,7 @@ void Lastfm::Download()
{ {
Save(result.second); Save(result.second);
w.clear(); w.clear();
IConv::utf8ToLocale_(result.second); w << Charset::utf8ToLocale(result.second);
w << result.second;
itsService->colorizeOutput(w); itsService->colorizeOutput(w);
} }
else else

View File

@@ -249,8 +249,7 @@ void *Lyrics::Download()
{ {
Save(itsFilename, result.second); Save(itsFilename, result.second);
w.clear(); w.clear();
IConv::utf8ToLocale_(result.second); w << Charset::utf8ToLocale(result.second);
w << result.second;
} }
else else
w << '\n' << L"Lyrics weren't found."; w << '\n' << L"Lyrics weren't found.";
@@ -320,12 +319,11 @@ void Lyrics::Load()
{ {
bool first = 1; bool first = 1;
std::string line; std::string line;
while (getline(input, line)) while (std::getline(input, line))
{ {
if (!first) if (!first)
w << '\n'; w << '\n';
IConv::utf8ToLocale_(line); w << Charset::utf8ToLocale(line);
w << line;
first = 0; first = 0;
} }
w.flush(); w.flush();

View File

@@ -212,7 +212,7 @@ void LyricstimeFetcher::postProcess(std::string &data)
{ {
// lyricstime.com uses iso-8859-1 as the encoding // lyricstime.com uses iso-8859-1 as the encoding
// so we need to convert obtained lyrics to utf-8 // 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); LyricsFetcher::postProcess(data);
} }
@@ -245,7 +245,7 @@ void LyricsmaniaFetcher::postProcess(std::string &data)
{ {
// lyricsmania.com uses iso-8859-1 as the encoding // lyricsmania.com uses iso-8859-1 as the encoding
// so we need to convert obtained lyrics to utf-8 // 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); LyricsFetcher::postProcess(data);
} }

View File

@@ -314,6 +314,7 @@ void MediaLibrary::update()
} }
if (idx < Tags.size()) if (idx < Tags.size())
Tags.resizeList(idx); Tags.resizeList(idx);
std::sort(Tags.beginV(), Tags.endV(), SortPrimaryTags());
}); });
Tags.refresh(); Tags.refresh();
} }

View File

@@ -89,8 +89,8 @@ int main(int argc, char **argv)
using Global::VolumeState; using Global::VolumeState;
using Global::Timer; using Global::Timer;
srand(time(0)); std::srand(std::time(0));
setlocale(LC_ALL, ""); std::setlocale(LC_ALL, "");
std::locale::global(std::locale("")); std::locale::global(std::locale(""));
Config.CheckForCommandLineConfigFilePath(argv, argc); Config.CheckForCommandLineConfigFilePath(argv, argc);

View File

@@ -330,8 +330,8 @@ void Status::Changes::elapsedTime()
} }
NC::WBuffer first, second; NC::WBuffer first, second;
stringToBuffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, Config.tags_separator, "$"))), first); stringToBuffer(ToWString(Charset::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_second_line, Config.tags_separator, "$"))), second);
size_t first_len = wideLength(first.str()); size_t first_len = wideLength(first.str());
size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2; size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2;
@@ -382,7 +382,7 @@ void Status::Changes::elapsedTime()
tracklength += "]"; tracklength += "]";
} }
NC::WBuffer np_song; 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; *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" ** "); 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; *wFooter << NC::fmtBold << NC::XY(wFooter->getWidth()-tracklength.length(), 1) << tracklength << NC::fmtBoldEnd;

View File

@@ -18,31 +18,17 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <boost/locale/encoding.hpp>
#include "utility/wide_string.h" #include "utility/wide_string.h"
std::string ToString(const std::wstring &ws) std::string ToString(std::wstring ws)
{ {
std::string result; return boost::locale::conv::utf_to_utf<char>(ws);
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;
} }
std::wstring ToWString(const std::string &s) std::wstring ToWString(std::string s)
{ {
std::wstring result; return boost::locale::conv::utf_to_utf<wchar_t>(s);
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;
} }
size_t wideLength(const std::wstring &ws) size_t wideLength(const std::wstring &ws)

View File

@@ -23,8 +23,8 @@
#include <string> #include <string>
std::string ToString(const std::wstring &ws); std::string ToString(std::wstring ws);
std::wstring ToWString(const std::string &s); std::wstring ToWString(std::string s);
size_t wideLength(const std::wstring &ws); size_t wideLength(const std::wstring &ws);