diff options
author | Andrzej Rybczak <electricityispower@gmail.com> | 2012-09-11 22:56:57 +0200 |
---|---|---|
committer | Andrzej Rybczak <electricityispower@gmail.com> | 2012-09-11 22:57:56 +0200 |
commit | a9bee35cca51c3a192477aa87ad03f2b75f726ed (patch) | |
tree | b1991bae6449615e2dbe1549e4d8e47ce60b9098 | |
parent | c8097a898ef8ac1d0afb367bb6b368edd9cb558f (diff) |
charset: put functions into namespace and change naming convention
-rw-r--r-- | src/charset.cpp | 41 | ||||
-rw-r--r-- | src/charset.h | 30 | ||||
-rw-r--r-- | src/cmdargs.cpp | 3 | ||||
-rw-r--r-- | src/lastfm.cpp | 4 | ||||
-rw-r--r-- | src/lyrics.cpp | 4 | ||||
-rw-r--r-- | src/lyrics_fetcher.cpp | 4 | ||||
-rw-r--r-- | src/status.cpp | 6 |
7 files changed, 40 insertions, 52 deletions
diff --git a/src/charset.cpp b/src/charset.cpp index cfc7837f..0941419d 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -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 - diff --git a/src/charset.h b/src/charset.h index b53398b5..737c1608 100644 --- a/src/charset.h +++ b/src/charset.h @@ -22,33 +22,33 @@ #define _CHARSET_H #include "config.h" - -#ifdef HAVE_ICONV_H - #include <string> -void iconv_convert_from_to(const char *from, const char *to, std::string &s); +namespace IConv {// -void utf_to_locale(std::string &); -void locale_to_utf(std::string &); +#ifdef HAVE_ICONV_H + +void convertFromTo(const char *from, const char *to, std::string &s); -std::string utf_to_locale_cpy(const std::string &s); -std::string locale_to_utf_cpy(const std::string &s); +std::string utf8ToLocale(std::string s); +std::string localeToUtf8(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 diff --git a/src/cmdargs.cpp b/src/cmdargs.cpp index 6e035212..0e458557 100644 --- a/src/cmdargs.cpp +++ b/src/cmdargs.cpp @@ -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); } diff --git a/src/lastfm.cpp b/src/lastfm.cpp index 420975e6..08790827 100644 --- a/src/lastfm.cpp +++ b/src/lastfm.cpp @@ -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); } diff --git a/src/lyrics.cpp b/src/lyrics.cpp index 2062771d..5eee0bcd 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -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; } diff --git a/src/lyrics_fetcher.cpp b/src/lyrics_fetcher.cpp index 13e060e8..998197a1 100644 --- a/src/lyrics_fetcher.cpp +++ b/src/lyrics_fetcher.cpp @@ -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); } diff --git a/src/status.cpp b/src/status.cpp index fd6668c2..f9624399 100644 --- a/src/status.cpp +++ b/src/status.cpp @@ -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; |