diff --git a/src/actions.cpp b/src/actions.cpp index da89b848..03c778b7 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -901,7 +901,6 @@ void SavePlaylist::Run() Statusbar::lock(); Statusbar::put() << "Save playlist as: "; std::string playlist_name = wFooter->getString(); - std::string real_playlist_name = locale_to_utf_cpy(playlist_name); Statusbar::unlock(); if (playlist_name.find("/") != std::string::npos) { @@ -914,14 +913,14 @@ void SavePlaylist::Run() { Mpd.StartCommandsList(); for (size_t i = 0; i < myPlaylist->Items->size(); ++i) - Mpd.AddToPlaylist(real_playlist_name, (*myPlaylist->Items)[i].value()); + Mpd.AddToPlaylist(playlist_name, (*myPlaylist->Items)[i].value()); Mpd.CommitCommandsList(); if (Mpd.GetErrorMessage().empty()) Statusbar::msg("Filtered items added to playlist \"%s\"", playlist_name.c_str()); } else { - int result = Mpd.SavePlaylist(real_playlist_name); + int result = Mpd.SavePlaylist(playlist_name); if (result == MPD_ERROR_SUCCESS) { Statusbar::msg("Playlist saved as \"%s\"", playlist_name.c_str()); @@ -933,8 +932,8 @@ void SavePlaylist::Run() bool yes = AskYesNoQuestion("Playlist \"" + playlist_name + "\" already exists, overwrite?", TraceMpdStatus); if (yes) { - Mpd.DeletePlaylist(real_playlist_name); - if (Mpd.SavePlaylist(real_playlist_name) == MPD_ERROR_SUCCESS) + Mpd.DeletePlaylist(playlist_name); + if (Mpd.SavePlaylist(playlist_name) == MPD_ERROR_SUCCESS) Statusbar::msg("Playlist overwritten"); } else @@ -1062,7 +1061,6 @@ void Add::Run() Statusbar::lock(); Statusbar::put() << (myScreen == myPlaylistEditor ? "Add to playlist: " : "Add: "); std::string path = wFooter->getString(); - locale_to_utf(path); Statusbar::unlock(); if (!path.empty()) { @@ -1210,7 +1208,7 @@ void TogglePlayingSongCentering::Run() void UpdateDatabase::Run() { if (myScreen == myBrowser) - Mpd.UpdateDirectory(locale_to_utf_cpy(myBrowser->CurrentDir())); + Mpd.UpdateDirectory(myBrowser->CurrentDir()); # ifdef HAVE_TAGLIB_H else if (myScreen == myTagEditor) Mpd.UpdateDirectory(myTagEditor->CurrentDir()); @@ -1370,7 +1368,7 @@ void EditLibraryTag::Run() { Statusbar::msg("Updating tags..."); Mpd.StartSearch(1); - Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(myLibrary->Tags->current().value())); + Mpd.AddSearch(Config.media_lib_primary_tag, myLibrary->Tags->current().value()); MPD::MutableSong::SetFunction set = tagTypeToSetFunction(Config.media_lib_primary_tag); assert(set); bool success = true; @@ -1482,18 +1480,18 @@ void EditDirectoryName::Run() std::string full_old_dir; if (!myBrowser->isLocal()) full_old_dir += Config.mpd_music_dir; - full_old_dir += locale_to_utf_cpy(old_dir); + full_old_dir += old_dir; std::string full_new_dir; if (!myBrowser->isLocal()) full_new_dir += Config.mpd_music_dir; - full_new_dir += locale_to_utf_cpy(new_dir); + full_new_dir += new_dir; int rename_result = rename(full_old_dir.c_str(), full_new_dir.c_str()); if (rename_result == 0) { const char msg[] = "Directory renamed to \"%ls\""; Statusbar::msg(msg, wideShorten(ToWString(new_dir), COLS-const_strlen(msg)).c_str()); if (!myBrowser->isLocal()) - Mpd.UpdateDirectory(locale_to_utf_cpy(getSharedDirectory(old_dir, new_dir))); + Mpd.UpdateDirectory(getSharedDirectory(old_dir, new_dir)); myBrowser->GetDirectory(myBrowser->CurrentDir()); } else @@ -1513,8 +1511,8 @@ void EditDirectoryName::Run() Statusbar::unlock(); if (!new_dir.empty() && new_dir != old_dir) { - std::string full_old_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + locale_to_utf_cpy(old_dir); - std::string full_new_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + locale_to_utf_cpy(new_dir); + std::string full_old_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + old_dir; + std::string full_new_dir = Config.mpd_music_dir + myTagEditor->CurrentDir() + "/" + new_dir; if (rename(full_old_dir.c_str(), full_new_dir.c_str()) == 0) { const char msg[] = "Directory renamed to \"%ls\""; @@ -1555,7 +1553,7 @@ void EditPlaylistName::Run() Statusbar::unlock(); if (!new_name.empty() && new_name != old_name) { - if (Mpd.Rename(locale_to_utf_cpy(old_name), locale_to_utf_cpy(new_name))) + if (Mpd.Rename(old_name, new_name)) { const char msg[] = "Playlist renamed to \"%ls\""; Statusbar::msg(msg, wideShorten(ToWString(new_name), COLS-const_strlen(msg)).c_str()); diff --git a/src/browser.cpp b/src/browser.cpp index e90526c4..f41149af 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -157,7 +157,7 @@ void Browser::EnterPressed() } case itPlaylist: { - if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name))) + if (Mpd.LoadPlaylist(item.name)) { Statusbar::msg("Playlist \"%s\" loaded", item.name.c_str()); myPlaylist->PlayNewlyAddedSongs(); @@ -204,7 +204,7 @@ void Browser::SpacePressed() } else # endif // !WIN32 - result = Mpd.Add(locale_to_utf_cpy(item.name)); + result = Mpd.Add(item.name); if (result) Statusbar::msg("Directory \"%s\" added", item.name.c_str()); break; @@ -216,7 +216,7 @@ void Browser::SpacePressed() } case itPlaylist: { - if (Mpd.LoadPlaylist(locale_to_utf_cpy(item.name))) + if (Mpd.LoadPlaylist(item.name)) Statusbar::msg("Playlist \"%s\" loaded", item.name.c_str()); break; } @@ -403,8 +403,6 @@ void Browser::GetDirectory(std::string dir, std::string subdir) w->reset(); itsBrowsedDir = dir; - locale_to_utf(dir); - w->clear(); if (dir != "/") @@ -434,13 +432,11 @@ void Browser::GetDirectory(std::string dir, std::string subdir) { case itPlaylist: { - utf_to_locale(it->name); w->addItem(*it); break; } case itDirectory: { - utf_to_locale(it->name); if (it->name == subdir) highlightme = w->size(); w->addItem(*it); @@ -589,7 +585,7 @@ bool Browser::deleteItem(const MPD::Item &item) // playlist created by mpd if (!isLocal() && item.type == itPlaylist && CurrentDir() == "/") - return Mpd.DeletePlaylist(locale_to_utf_cpy(item.name)); + return Mpd.DeletePlaylist(item.name); std::string path; if (!isLocal()) diff --git a/src/charset.cpp b/src/charset.cpp index 6748b55b..cfc7837f 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -31,66 +31,73 @@ #include "settings.h" -namespace +namespace {// + +bool is_utf8(const char *s) { - bool is_utf8(const char *s) + for (; *s; ++s) { - for (; *s; ++s) + if (*s & 0x80) // 1xxxxxxx { - 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 + 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; - for (unsigned j = 0; j < i; ++j) - if (!*++s || !(*s & 0x80) || *s & 0x40) // 10xxxxxx - return false; - } } - return true; + } + return true; +} + +bool has_non_ascii_chars(const char *s) +{ + 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; } - bool has_non_ascii_chars(const char *s) - { - for (; *s; ++s) - if (*s & 0x80) - return true; - return false; - } + if (!len) + len = strlen(inbuf); + size_t buflen = len*MB_CUR_MAX+1; + char *outbuf = static_cast(malloc(buflen)); + char *outstart = outbuf; + const char *instart = inbuf; - void charset_convert(const char *from, const char *to, const char *&inbuf, bool delete_old, size_t len = 0) + if (iconv(cd, const_cast(&inbuf), &len, &outbuf, &buflen) == size_t(-1)) + { + std::cerr << "Error while executing iconv: " << strerror(errno) << "\n"; + inbuf = instart; + delete [] outstart; + } + else { - if (!inbuf || !from || !to) - return; - - iconv_t cd = iconv_open(to, from); - - if (cd == iconv_t(-1)) - return; - - if (!len) - len = strlen(inbuf); - size_t buflen = len*6+1; - char *outbuf = new char[buflen]; - char *outstart = outbuf; - const char *instart = inbuf; - - if (iconv(cd, const_cast(&inbuf), &len, &outbuf, &buflen) == size_t(-1)) - { - inbuf = instart; - delete [] outstart; - iconv_close(cd); - return; - } - iconv_close(cd); *outbuf = 0; if (delete_old) free(const_cast(instart)); inbuf = outstart; } + iconv_close(cd); +} + } void iconv_convert_from_to(const char *from, const char *to, std::string &s) @@ -103,7 +110,7 @@ void iconv_convert_from_to(const char *from, const char *to, std::string &s) void utf_to_locale(std::string &s) { - if (s.empty() || Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str())) + 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()); @@ -120,7 +127,7 @@ std::string utf_to_locale_cpy(const std::string &s) void locale_to_utf(std::string &s) { - if (s.empty() || Config.system_encoding.empty() || !has_non_ascii_chars(s.c_str()) || is_utf8(s.c_str())) + 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()); diff --git a/src/charset.h b/src/charset.h index c512eaab..b53398b5 100644 --- a/src/charset.h +++ b/src/charset.h @@ -21,9 +21,7 @@ #ifndef _CHARSET_H #define _CHARSET_H -#ifdef HAVE_CONFIG_H -#include -#endif +#include "config.h" #ifdef HAVE_ICONV_H diff --git a/src/lastfm.cpp b/src/lastfm.cpp index 83299134..420975e6 100644 --- a/src/lastfm.cpp +++ b/src/lastfm.cpp @@ -123,8 +123,6 @@ void Lastfm::Load() w->reset(); std::string artist = itsArgs.find("artist")->second; - locale_to_utf(artist); - std::string file = lowercase(artist + ".txt"); removeInvalidCharsFromFilename(file); diff --git a/src/lyrics.cpp b/src/lyrics.cpp index e69273f6..2062771d 100644 --- a/src/lyrics.cpp +++ b/src/lyrics.cpp @@ -244,8 +244,8 @@ void Lyrics::DownloadInBackgroundImplHelper(const MPD::Song &s) void *Lyrics::Download() { - std::string artist = Curl::escape(locale_to_utf_cpy(itsSong.getArtist())); - std::string title = Curl::escape(locale_to_utf_cpy(itsSong.getTitle())); + std::string artist = Curl::escape(itsSong.getArtist()); + std::string title = Curl::escape(itsSong.getTitle()); LyricsFetcher::Result result; @@ -267,9 +267,8 @@ void *Lyrics::Download() if (result.first == true) { Save(itsFilename, result.second); - - utf_to_locale(result.second); w->clear(); + utf_to_locale(result.second); *w << result.second; } else @@ -306,9 +305,9 @@ std::string Lyrics::GenerateFilename(const MPD::Song &s) } else { - std::string file = locale_to_utf_cpy(s.getArtist()); + std::string file = s.getArtist(); file += " - "; - file += locale_to_utf_cpy(s.getTitle()); + file += s.getTitle(); file += ".txt"; removeInvalidCharsFromFilename(file); filename = Config.lyrics_directory; diff --git a/src/media_library.cpp b/src/media_library.cpp index c880948b..c2e21593 100644 --- a/src/media_library.cpp +++ b/src/media_library.cpp @@ -350,12 +350,12 @@ void MediaLibrary::Update() Songs->reset(); Mpd.StartSearch(1); - Mpd.AddSearch(Config.media_lib_primary_tag, locale_to_utf_cpy(hasTwoColumns ? Albums->current().value().PrimaryTag : Tags->current().value())); + Mpd.AddSearch(Config.media_lib_primary_tag, hasTwoColumns ? Albums->current().value().PrimaryTag : Tags->current().value()); if (Albums->current().value().Date != AllTracksMarker) { - Mpd.AddSearch(MPD_TAG_ALBUM, locale_to_utf_cpy(Albums->current().value().Album)); + Mpd.AddSearch(MPD_TAG_ALBUM, Albums->current().value().Album); if (Config.media_library_display_date) - Mpd.AddSearch(MPD_TAG_DATE, locale_to_utf_cpy(Albums->current().value().Date)); + Mpd.AddSearch(MPD_TAG_DATE, Albums->current().value().Date); } auto songs = Mpd.CommitSearchSongs(); for (auto s = songs.begin(); s != songs.end(); ++s) diff --git a/src/playlist_editor.cpp b/src/playlist_editor.cpp index 653d83e5..66204406 100644 --- a/src/playlist_editor.cpp +++ b/src/playlist_editor.cpp @@ -277,7 +277,7 @@ void PlaylistEditor::AddToPlaylist(bool add_n_play) if (w == Playlists && !Playlists->empty()) { - if (Mpd.LoadPlaylist(utf_to_locale_cpy(Playlists->current().value()))) + if (Mpd.LoadPlaylist(Playlists->current().value())) { Statusbar::msg("Playlist \"%s\" loaded", Playlists->current().value().c_str()); if (add_n_play) diff --git a/src/sel_items_adder.cpp b/src/sel_items_adder.cpp index a7495e91..c90b2498 100644 --- a/src/sel_items_adder.cpp +++ b/src/sel_items_adder.cpp @@ -179,17 +179,16 @@ void SelectedItemsAdder::EnterPressed() Statusbar::unlock(); if (!playlist.empty()) { - std::string utf_playlist = locale_to_utf_cpy(playlist); Mpd.StartCommandsList(); for (auto it = list.begin(); it != list.end(); ++it) - Mpd.AddToPlaylist(utf_playlist, *it); + Mpd.AddToPlaylist(playlist, *it); if (Mpd.CommitCommandsList()) Statusbar::msg("Selected item(s) added to playlist \"%s\"", playlist.c_str()); } } else if (pos > 1 && pos < w->size()-1) // add items to existing playlist { - std::string playlist = locale_to_utf_cpy(w->current().value()); + std::string playlist = w->current().value(); Mpd.StartCommandsList(); for (auto it = list.begin(); it != list.end(); ++it) Mpd.AddToPlaylist(playlist, *it); diff --git a/src/tag_editor.cpp b/src/tag_editor.cpp index 7c394957..6143a205 100644 --- a/src/tag_editor.cpp +++ b/src/tag_editor.cpp @@ -1078,7 +1078,6 @@ bool TagEditor::WriteTags(MPD::MutableSong &s) if (file_is_from_db) new_name += Config.mpd_music_dir; new_name += s.getDirectory() + "/" + s.getNewURI(); - locale_to_utf(new_name); if (rename(path_to_file.c_str(), new_name.c_str()) == 0 && !file_is_from_db) { if (Global::myOldScreen == myPlaylist)