charset: put functions into namespace and change naming convention

This commit is contained in:
Andrzej Rybczak
2012-09-11 22:56:57 +02:00
parent c8097a898e
commit a9bee35cca
7 changed files with 40 additions and 52 deletions

View File

@@ -100,7 +100,9 @@ void charset_convert(const char *from, const char *to, const char *&inbuf,
}
void iconv_convert_from_to(const char *from, const char *to, std::string &s)
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());
@@ -108,7 +110,13 @@ void iconv_convert_from_to(const char *from, const char *to, std::string &s)
free(const_cast<char *>(tmp));
}
void utf_to_locale(std::string &s)
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;
@@ -118,14 +126,13 @@ void utf_to_locale(std::string &s)
free(const_cast<char *>(tmp));
}
std::string utf_to_locale_cpy(const std::string &s)
std::string localeToUtf8(std::string s)
{
std::string result = s;
utf_to_locale(result);
return result;
localeToUtf8_(s);
return s;
}
void locale_to_utf(std::string &s)
void localeToUtf8_(std::string &s)
{
if (Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()) || is_utf8(s.c_str()))
return;
@@ -135,26 +142,6 @@ void locale_to_utf(std::string &s)
free(const_cast<char *>(tmp));
}
std::string locale_to_utf_cpy(const std::string &s)
{
std::string result = s;
locale_to_utf(result);
return result;
}
void utf_to_locale(const char *&s, bool delete_old)
{
if (!s || Config.system_encoding.empty() || !has_non_ascii_chars(s))
return;
charset_convert("utf-8", Config.system_encoding.c_str(), s, delete_old);
}
void locale_to_utf(const char *&s, bool delete_old)
{
if (!s || Config.system_encoding.empty() || !has_non_ascii_chars(s) || is_utf8(s))
return;
charset_convert(Config.system_encoding.c_str(), "utf-8", s, delete_old);
}
#endif // HAVE_ICONV_H

View File

@@ -22,33 +22,33 @@
#define _CHARSET_H
#include "config.h"
#include <string>
namespace IConv {//
#ifdef HAVE_ICONV_H
#include <string>
void convertFromTo(const char *from, const char *to, std::string &s);
void iconv_convert_from_to(const char *from, const char *to, std::string &s);
std::string utf8ToLocale(std::string s);
std::string localeToUtf8(std::string s);
void utf_to_locale(std::string &);
void locale_to_utf(std::string &);
std::string utf_to_locale_cpy(const std::string &s);
std::string locale_to_utf_cpy(const std::string &s);
void utf_to_locale(const char *&, bool);
void locale_to_utf(const char *&, bool);
void utf8ToLocale_(std::string &s);
void localeToUtf8_(std::string &s);
#else
#define iconv_convert_from_to(x, y, z);
inline void convertFromTo(const char *, const char *, std::string &) { }
#define utf_to_locale(x);
#define locale_to_utf(x);
inline std::string utf8ToLocale(std::string s) { return s; }
inline std::string localeToUtf8(std::string s) { return s; }
#define utf_to_locale_cpy(x) (x)
#define locale_to_utf_cpy(x) (x)
inline void utf8ToLocale_(std::string &) { }
inline void localeToUtf8_(std::string &) { }
#endif // HAVE_ICONV_H
}
#endif

View File

@@ -188,7 +188,8 @@ void ParseArgv(int argc, char **argv)
replace(now_playing_format, "\\t", "\t");
}
}
std::cout << utf_to_locale_cpy(Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n";
std::cout << IConv::utf8ToLocale(
Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n";
}
exit(0);
}

View File

@@ -143,7 +143,7 @@ void Lastfm::Load()
{
if (!first)
*w << '\n';
utf_to_locale(line);
IConv::utf8ToLocale_(line);
*w << line;
first = 0;
}
@@ -183,7 +183,7 @@ void Lastfm::Download()
{
Save(result.second);
w->clear();
utf_to_locale(result.second);
IConv::utf8ToLocale_(result.second);
*w << result.second;
itsService->colorizeOutput(*w);
}

View File

@@ -268,7 +268,7 @@ void *Lyrics::Download()
{
Save(itsFilename, result.second);
w->clear();
utf_to_locale(result.second);
IConv::utf8ToLocale_(result.second);
*w << result.second;
}
else
@@ -343,7 +343,7 @@ void Lyrics::Load()
{
if (!first)
*w << '\n';
utf_to_locale(line);
IConv::utf8ToLocale_(line);
*w << line;
first = 0;
}

View File

@@ -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_convert_from_to("iso-8859-1", "utf-8", data);
IConv::convertFromTo("iso-8859-1", "utf-8", data);
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_convert_from_to("iso-8859-1", "utf-8", data);
IConv::convertFromTo("iso-8859-1", "utf-8", data);
LyricsFetcher::postProcess(data);
}

View File

@@ -341,8 +341,8 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
}
NC::WBuffer first, second;
String2Buffer(ToWString(utf_to_locale_cpy(np.toString(Config.new_header_first_line, "$"))), first);
String2Buffer(ToWString(utf_to_locale_cpy(np.toString(Config.new_header_second_line, "$"))), second);
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_first_line, "$"))), first);
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.new_header_second_line, "$"))), second);
size_t first_len = wideLength(first.str());
size_t first_margin = (std::max(tracklength.length()+1, VolumeState.length()))*2;
@@ -393,7 +393,7 @@ void NcmpcppStatusChanged(MPD::Connection *, MPD::StatusChanges changed, void *)
tracklength += "]";
}
NC::WBuffer np_song;
String2Buffer(ToWString(utf_to_locale_cpy(np.toString(Config.song_status_format, "$"))), np_song);
String2Buffer(ToWString(IConv::utf8ToLocale(np.toString(Config.song_status_format, "$"))), 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;