summaryrefslogtreecommitdiff
path: root/src/utility/comparators.h
blob: 40fd61099d193835e02164c97275797cfa052e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/***************************************************************************
 *   Copyright (C) 2008-2014 by Andrzej Rybczak                            *
 *   electricityispower@gmail.com                                          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#ifndef NCMPCPP_UTILITY_COMPARATORS_H
#define NCMPCPP_UTILITY_COMPARATORS_H

#include <string>
#include "runnable_item.h"
#include "mpdpp.h"
#include "settings.h"
#include "menu.h"

class LocaleStringComparison
{
	std::locale m_locale;
	bool m_ignore_the;
	
public:
	LocaleStringComparison(const std::locale &loc, bool ignore_the)
	: m_locale(loc), m_ignore_the(ignore_the) { }

	int operator()(const char *a, const char *b) const {
		return compare(a, strlen(a), b, strlen(b));
	}
	int operator()(const std::string &a, const std::string &b) const {
		return compare(a.c_str(), a.length(), b.c_str(), b.length());
	}

	int compare(const char *a, size_t a_len, const char *b, size_t b_len) const;
};

class LocaleBasedSorting
{
	LocaleStringComparison m_cmp;
	
public:
	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 m_cmp(a, b) < 0;
	}
	
	bool operator()(const MPD::Playlist &a, const MPD::Playlist &b) const {
		return m_cmp(a.path(), b.path()) < 0;
	}

	bool operator()(const MPD::Song &a, const MPD::Song &b) const {
		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 m_cmp(a.first, b.first) < 0;
	}
	
	template <typename ItemT, typename FunT>
	bool operator()(const RunnableItem<ItemT, FunT> &a, const RunnableItem<ItemT, FunT> &b) const {
		return m_cmp(a.item(), b.item()) < 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 // NCMPCPP_UTILITY_COMPARATORS_H