1 /***************************************************************************
2  *   Copyright (C) 2008-2021 by Andrzej Rybczak                            *
3  *   andrzej@rybczak.net                                                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
19  ***************************************************************************/
20 
21 #ifndef NCMPCPP_UTILITY_COMPARATORS_H
22 #define NCMPCPP_UTILITY_COMPARATORS_H
23 
24 #include <string>
25 #include "runnable_item.h"
26 #include "mpdpp.h"
27 #include "settings.h"
28 #include "menu.h"
29 
30 class LocaleStringComparison
31 {
32 	std::locale m_locale;
33 	bool m_ignore_the;
34 
35 public:
LocaleStringComparison(const std::locale & loc,bool ignore_the)36 	LocaleStringComparison(const std::locale &loc, bool ignore_the)
37 	: m_locale(loc), m_ignore_the(ignore_the) { }
38 
operator()39 	int operator()(const char *a, const char *b) const {
40 		return compare(a, strlen(a), b, strlen(b));
41 	}
operator()42 	int operator()(const std::string &a, const std::string &b) const {
43 		return compare(a.c_str(), a.length(), b.c_str(), b.length());
44 	}
45 
46 	int compare(const char *a, size_t a_len, const char *b, size_t b_len) const;
47 };
48 
49 class LocaleBasedSorting
50 {
51 	LocaleStringComparison m_cmp;
52 
53 public:
LocaleBasedSorting(const std::locale & loc,bool ignore_the)54 	LocaleBasedSorting(const std::locale &loc, bool ignore_the) : m_cmp(loc, ignore_the) { }
55 
operator()56 	bool operator()(const std::string &a, const std::string &b) const {
57 		return m_cmp(a, b) < 0;
58 	}
59 
operator()60 	bool operator()(const MPD::Playlist &a, const MPD::Playlist &b) const {
61 		return m_cmp(a.path(), b.path()) < 0;
62 	}
63 
operator()64 	bool operator()(const MPD::Song &a, const MPD::Song &b) const {
65 		return m_cmp(a.getName(), b.getName()) < 0;
66 	}
67 
68 	template <typename A, typename B>
operator()69 	bool operator()(const std::pair<A, B> &a, const std::pair<A, B> &b) const {
70 		return m_cmp(a.first, b.first) < 0;
71 	}
72 
73 	template <typename ItemT, typename FunT>
operator()74 	bool operator()(const RunnableItem<ItemT, FunT> &a, const RunnableItem<ItemT, FunT> &b) const {
75 		return m_cmp(a.item(), b.item()) < 0;
76 	}
77 };
78 
79 class LocaleBasedItemSorting
80 {
81 	LocaleBasedSorting m_cmp;
82 	SortMode m_sort_mode;
83 
84 public:
LocaleBasedItemSorting(const std::locale & loc,bool ignore_the,SortMode mode)85 	LocaleBasedItemSorting(const std::locale &loc, bool ignore_the, SortMode mode)
86 	: m_cmp(loc, ignore_the), m_sort_mode(mode) { }
87 
88 	bool operator()(const MPD::Item &a, const MPD::Item &b) const;
89 
operator()90 	bool operator()(const NC::Menu<MPD::Item>::Item &a,
91 	                const NC::Menu<MPD::Item>::Item &b) const
92 	{
93 		return (*this)(a.value(), b.value());
94 	}
95 };
96 
97 #endif // NCMPCPP_UTILITY_COMPARATORS_H
98