keys: implement parsing key configuration file

This commit is contained in:
Andrzej Rybczak
2012-09-05 18:02:37 +02:00
parent 95e2cfe6e1
commit 7c6467a9e7
10 changed files with 655 additions and 298 deletions

View File

@@ -21,24 +21,32 @@
#include "comparators.h"
#include "settings.h"
int CaseInsensitiveStringComparison::operator()(const std::string &a, const std::string &b)
bool CaseInsensitiveStringComparison::hasTheWord(const char *s) const
{
const char *i = a.c_str();
const char *j = b.c_str();
if (Config.ignore_leading_the)
return (s[0] == 't' || s[0] == 'T')
&& (s[1] == 'h' || s[1] == 'H')
&& (s[2] == 'e' || s[2] == 'E')
&& (s[3] == ' ');
}
int CaseInsensitiveStringComparison::operator()(const char *a, const char *b) const
{
if (m_ignore_the)
{
if (hasTheWord(a))
i += 4;
a += 4;
if (hasTheWord(b))
j += 4;
b += 4;
}
int dist;
while (!(dist = tolower(*i)-tolower(*j)) && *j)
++i, ++j;
while (!(dist = tolower(*a)-tolower(*b)) && *b)
++a, ++b;
return dist;
}
bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
CaseInsensitiveSorting::CaseInsensitiveSorting(): cmp(Config.ignore_leading_the) { }
bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b) const
{
bool result = false;
if (a.type == b.type)
@@ -65,8 +73,6 @@ bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
break;
}
break;
default: // there is no other option, silence compiler
assert(false);
}
}
else

View File

@@ -26,17 +26,24 @@
class CaseInsensitiveStringComparison
{
bool hasTheWord(const std::string &s)
{
return (s.length() > 3)
&& (s[0] == 't' || s[0] == 'T')
&& (s[1] == 'h' || s[1] == 'H')
&& (s[2] == 'e' || s[2] == 'E')
&& (s[3] == ' ');
}
bool m_ignore_the;
bool hasTheWord(const char *s) const;
public:
int operator()(const std::string &a, const std::string &b);
CaseInsensitiveStringComparison(bool ignore_the) : m_ignore_the(ignore_the) { }
int operator()(const char *a, const char *b) const;
int operator()(const char *a, const std::string &b) const {
return (*this)(a, b.c_str());
}
int operator()(const std::string &a, const char *b) const {
return (*this)(a.c_str(), b);
}
int operator()(const std::string &a, const std::string &b) const {
return (*this)(a.c_str(), b.c_str());
}
};
class CaseInsensitiveSorting
@@ -44,22 +51,22 @@ class CaseInsensitiveSorting
CaseInsensitiveStringComparison cmp;
public:
bool operator()(const std::string &a, const std::string &b)
{
CaseInsensitiveSorting();
bool operator()(const std::string &a, const std::string &b) const {
return cmp(a, b) < 0;
}
bool operator()(const MPD::Song &a, const MPD::Song &b)
{
bool operator()(const MPD::Song &a, const MPD::Song &b) const {
return 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)
{
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;
}
bool operator()(const MPD::Item &a, const MPD::Item &b);
bool operator()(const MPD::Item &a, const MPD::Item &b) const;
};
#endif // _UTILITY_COMPARATORS
#endif // _UTILITY_COMPARATORS