comparators: generalize objects a bit

This commit is contained in:
Andrzej Rybczak
2012-09-08 14:34:46 +02:00
parent 691e0322e7
commit 8fb88b7181
12 changed files with 54 additions and 34 deletions

View File

@@ -2105,7 +2105,8 @@ void ToggleBrowserSortMode::Run()
ShowMessage("Sort songs by: Name");
break;
}
std::sort(myBrowser->Main()->beginV()+(myBrowser->CurrentDir() != "/"), myBrowser->Main()->endV(), CaseInsensitiveSorting());
std::sort(myBrowser->Main()->begin()+(myBrowser->CurrentDir() != "/"), myBrowser->Main()->end(),
LocaleBasedItemSorting(std::locale(), Config.ignore_leading_the, Config.browser_sort_mode));
}
bool ToggleLibraryTagType::canBeRun() const

View File

@@ -423,7 +423,8 @@ void Browser::GetDirectory(std::string dir, std::string subdir)
list = Mpd.GetDirectory(dir);
# endif // !WIN32
if (!isLocal()) // local directory is already sorted
std::sort(list.begin(), list.end(), CaseInsensitiveSorting());
std::sort(list.begin(), list.end(),
LocaleBasedItemSorting(std::locale(), Config.ignore_leading_the, Config.browser_sort_mode));
for (MPD::ItemList::iterator it = list.begin(); it != list.end(); ++it)
{
@@ -519,7 +520,8 @@ void Browser::GetLocalDirectory(MPD::ItemList &v, const std::string &directory,
}
}
closedir(dir);
std::sort(v.begin()+old_size, v.end(), CaseInsensitiveSorting());
std::sort(v.begin()+old_size, v.end(),
LocaleBasedItemSorting(std::locale(), Config.ignore_leading_the, Config.browser_sort_mode));
}
void Browser::ClearDirectory(const std::string &path) const

View File

@@ -80,7 +80,7 @@ public:
&MPD::Song::getDate,
&MPD::Song::getAlbum,
&MPD::Song::getDisc
}}), m_cmp(std::locale(""), Config.ignore_leading_the) { }
}}), m_cmp(std::locale(), Config.ignore_leading_the) { }
bool operator()(const MPD::Song &a, const MPD::Song &b) {
for (auto get = m_gets.begin(); get != m_gets.end(); ++get) {
int ret = m_cmp(a.getTags(*get), b.getTags(*get));
@@ -94,7 +94,7 @@ public:
class SortSearchConstraints {
LocaleStringComparison m_cmp;
public:
SortSearchConstraints() : m_cmp(std::locale(""), Config.ignore_leading_the) { }
SortSearchConstraints() : m_cmp(std::locale(), Config.ignore_leading_the) { }
bool operator()(const SearchConstraints &a, const SearchConstraints &b) const {
int result;
result = m_cmp(a.PrimaryTag, b.PrimaryTag);
@@ -255,7 +255,8 @@ void MediaLibrary::Update()
Albums->clear();
Songs->clear();
auto list = Mpd.GetList(Config.media_lib_primary_tag);
std::sort(list.begin(), list.end(), CaseInsensitiveSorting());
std::sort(list.begin(), list.end(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
for (auto it = list.begin(); it != list.end(); ++it)
{
if (it->empty() && !Config.media_library_display_empty_tag)

View File

@@ -60,10 +60,6 @@ template <typename T> struct Menu : public Window
bool isSeparator() const { return m_is_separator; }
private:
// make those private, they shouldn't be used
Item(const Item &) { assert(false); }
Item &operator=(const Item &) { assert(false); }
static Item *mkSeparator()
{
Item *i = new Item;

View File

@@ -90,6 +90,7 @@ int main(int argc, char **argv)
srand(time(0));
setlocale(LC_ALL, "");
std::locale::global(std::locale(""));
Config.CheckForCommandLineConfigFilePath(argv, argc);

View File

@@ -197,7 +197,7 @@ void Playlist::EnterPressed()
for (; begin != end; ++begin)
playlist.push_back(begin->value());
LocaleStringComparison cmp(std::locale(""), Config.ignore_leading_the);
LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the);
std::function<void(MPD::SongList::iterator, MPD::SongList::iterator)> iter_swap, quick_sort;
auto song_cmp = [&cmp](const MPD::Song &a, const MPD::Song &b) -> bool {
for (size_t i = 0; i < SortOptions; ++i)

View File

@@ -146,7 +146,8 @@ void PlaylistEditor::Update()
Playlists->clearSearchResults();
withUnfilteredMenuReapplyFilter(*Playlists, [this]() {
auto list = Mpd.GetPlaylists();
std::sort(list.begin(), list.end(), CaseInsensitiveSorting());
std::sort(list.begin(), list.end(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
auto playlist = list.begin();
if (Playlists->size() > list.size())
Playlists->resizeList(list.size());

View File

@@ -448,7 +448,7 @@ void SearchEngine::Search()
bool any_found = 1;
bool found = 1;
LocaleStringComparison cmp(std::locale(""), Config.ignore_leading_the);
LocaleStringComparison cmp(std::locale(), Config.ignore_leading_the);
for (auto it = list.begin(); it != list.end(); ++it)
{
if (SearchMode != &SearchModes[2]) // match to pattern

View File

@@ -105,7 +105,8 @@ void SelectedItemsAdder::SwitchTo()
w->addSeparator();
auto playlists = Mpd.GetPlaylists();
std::sort(playlists.begin(), playlists.end(), CaseInsensitiveSorting());
std::sort(playlists.begin(), playlists.end(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
for (auto it = playlists.begin(); it != playlists.end(); ++it)
w->addItem(*it, 0, playlists_not_active);
w->addSeparator();

View File

@@ -257,7 +257,7 @@ void TagEditor::Update()
int highlightme = -1;
auto dirs = Mpd.GetDirectories(itsBrowsedDir);
std::sort(dirs.begin(), dirs.end(), CaseInsensitiveSorting());
std::sort(dirs.begin(), dirs.end(), LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
if (itsBrowsedDir != "/")
{
size_t slash = itsBrowsedDir.rfind("/");
@@ -284,7 +284,8 @@ void TagEditor::Update()
{
Tags->reset();
auto songs = Mpd.GetSongs(Dirs->current().value().second);
std::sort(songs.begin(), songs.end(), CaseInsensitiveSorting());
std::sort(songs.begin(), songs.end(),
LocaleBasedSorting(std::locale(), Config.ignore_leading_the));
for (auto s = songs.begin(); s != songs.end(); ++s)
Tags->addItem(*s);
Tags->refresh();

View File

@@ -20,9 +20,10 @@
#include <locale>
#include "comparators.h"
#include "settings.h"
bool LocaleStringComparison::hasTheWord(const std::string &s) const
namespace {//
bool hasTheWord(const std::string &s)
{
return s.length() >= 4
&& (s[0] == 't' || s[0] == 'T')
@@ -31,6 +32,8 @@ bool LocaleStringComparison::hasTheWord(const std::string &s) const
&& (s[3] == ' ');
}
}
int LocaleStringComparison::operator()(const std::string &a, const std::string &b) const
{
const char *ac = a.c_str();
@@ -48,9 +51,7 @@ int LocaleStringComparison::operator()(const std::string &a, const std::string &
);
}
CaseInsensitiveSorting::CaseInsensitiveSorting(): cmp(std::locale(""), Config.ignore_leading_the) { }
bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b) const
bool LocaleBasedItemSorting::operator()(const MPD::Item &a, const MPD::Item &b) const
{
bool result = false;
if (a.type == b.type)
@@ -58,22 +59,23 @@ bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
switch (a.type)
{
case MPD::itDirectory:
result = cmp(getBasename(a.name), getBasename(b.name)) < 0;
result = m_cmp(getBasename(a.name), getBasename(b.name)) < 0;
break;
case MPD::itPlaylist:
result = cmp(a.name, b.name) < 0;
result = m_cmp(a.name, b.name) < 0;
break;
case MPD::itSong:
switch (Config.browser_sort_mode)
switch (m_sort_mode)
{
case smName:
result = operator()(*a.song, *b.song);
result = m_cmp(*a.song, *b.song);
break;
case smMTime:
result = a.song->getMTime() > b.song->getMTime();
break;
case smCustomFormat:
result = cmp(a.song->toString(Config.browser_sort_format), b.song->toString(Config.browser_sort_format)) < 0;
result = m_cmp(a.song->toString(Config.browser_sort_format),
b.song->toString(Config.browser_sort_format)) < 0;
break;
}
break;

View File

@@ -23,14 +23,14 @@
#include <string>
#include "mpdpp.h"
#include "settings.h"
#include "menu.h"
class LocaleStringComparison
{
std::locale m_locale;
bool m_ignore_the;
bool hasTheWord(const std::string &s) const;
public:
LocaleStringComparison(const std::locale &loc, bool ignore_the)
: m_locale(loc), m_ignore_the(ignore_the) { }
@@ -38,27 +38,41 @@ public:
int operator()(const std::string &a, const std::string &b) const;
};
class CaseInsensitiveSorting
class LocaleBasedSorting
{
LocaleStringComparison cmp;
LocaleStringComparison m_cmp;
public:
CaseInsensitiveSorting();
LocaleBasedSorting(const std::locale loc, bool ignore_the) : m_cmp(loc, ignore_the) { }
bool operator()(const std::string &a, const std::string &b) const {
return cmp(a, b) < 0;
return m_cmp(a, b) < 0;
}
bool operator()(const MPD::Song &a, const MPD::Song &b) const {
return cmp(a.getName(), b.getName()) < 0;
return m_cmp(a.getName(), b.getName()) < 0;
}
template <typename A, typename B>
bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b) const {
return cmp(a.first, b.first) < 0;
return m_cmp(a.first, b.first) < 0;
}
};
class LocaleBasedItemSorting
{
LocaleBasedSorting m_cmp;
SortMode m_sort_mode;
public:
LocaleBasedItemSorting(const std::locale loc, bool ignore_the, SortMode mode)
: m_cmp(loc, ignore_the), m_sort_mode(mode) { }
bool operator()(const MPD::Item &a, const MPD::Item &b) const;
bool operator()(const NC::Menu<MPD::Item>::Item &a, const NC::Menu<MPD::Item>::Item &b) const {
return (*this)(a.value(), b.value());
}
};
#endif // _UTILITY_COMPARATORS