split majority of helpers and rewrite a few heinous functions

This commit is contained in:
Andrzej Rybczak
2012-08-29 11:46:17 +02:00
parent 4cb0e2232a
commit b1c301dc1c
31 changed files with 427 additions and 373 deletions

View File

@@ -0,0 +1,75 @@
/***************************************************************************
* Copyright (C) 2008-2012 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. *
***************************************************************************/
#include "comparators.h"
#include "settings.h"
int CaseInsensitiveStringComparison::operator()(const std::string &a, const std::string &b)
{
const char *i = a.c_str();
const char *j = b.c_str();
if (Config.ignore_leading_the)
{
if (hasTheWord(a))
i += 4;
if (hasTheWord(b))
j += 4;
}
int dist;
while (!(dist = tolower(*i)-tolower(*j)) && *j)
++i, ++j;
return dist;
}
bool CaseInsensitiveSorting::operator()(const MPD::Item &a, const MPD::Item &b)
{
bool result = false;
if (a.type == b.type)
{
switch (a.type)
{
case MPD::itDirectory:
result = cmp(getBasename(a.name), getBasename(b.name)) < 0;
break;
case MPD::itPlaylist:
result = cmp(a.name, b.name) < 0;
break;
case MPD::itSong:
switch (Config.browser_sort_mode)
{
case smName:
result = operator()(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;
break;
}
break;
default: // there is no other option, silence compiler
assert(false);
}
}
else
result = a.type < b.type;
return result;
}

65
src/utility/comparators.h Normal file
View File

@@ -0,0 +1,65 @@
/***************************************************************************
* Copyright (C) 2008-2012 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 _UTILITY_COMPARATORS
#define _UTILITY_COMPARATORS
#include <string>
#include "mpdpp.h"
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] == ' ');
}
public:
int operator()(const std::string &a, const std::string &b);
};
class CaseInsensitiveSorting
{
CaseInsensitiveStringComparison cmp;
public:
bool operator()(const std::string &a, const std::string &b)
{
return cmp(a, b) < 0;
}
bool operator()(const MPD::Song &a, const MPD::Song &b)
{
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)
{
return cmp(a.first, b.first) < 0;
}
bool operator()(const MPD::Item &a, const MPD::Item &b);
};
#endif // _UTILITY_COMPARATORS

80
src/utility/html.cpp Normal file
View File

@@ -0,0 +1,80 @@
/***************************************************************************
* Copyright (C) 2008-2012 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. *
***************************************************************************/
#include "utility/html.h"
#include "utility/string.h"
std::string unescapeHtmlUtf8(const std::string &data)
{
std::string result;
for (size_t i = 0, j; i < data.length(); ++i)
{
if (data[i] == '&' && data[i+1] == '#' && (j = data.find(';', i)) != std::string::npos)
{
int n = atoi(&data.c_str()[i+2]);
if (n >= 0x800)
{
result += (0xe0 | ((n >> 12) & 0x0f));
result += (0x80 | ((n >> 6) & 0x3f));
result += (0x80 | (n & 0x3f));
}
else if (n >= 0x80)
{
result += (0xc0 | ((n >> 6) & 0x1f));
result += (0x80 | (n & 0x3f));
}
else
result += n;
i = j;
}
else
result += data[i];
}
return result;
}
void stripHtmlTags(std::string &s)
{
bool erase = 0;
for (size_t i = s.find("<"); i != std::string::npos; i = s.find("<"))
{
size_t j = s.find(">", i)+1;
s.replace(i, j-i, "");
}
replace(s, "&#039;", "'");
replace(s, "&amp;", "&");
replace(s, "&quot;", "\"");
replace(s, "&nbsp;", " ");
for (size_t i = 0; i < s.length(); ++i)
{
if (erase)
{
s.erase(s.begin()+i);
erase = 0;
}
if (s[i] == 13) // ascii code for windows line ending, get rid of this shit
{
s[i] = '\n';
erase = 1;
}
else if (s[i] == '\t')
s[i] = ' ';
}
}

30
src/utility/html.h Normal file
View File

@@ -0,0 +1,30 @@
/***************************************************************************
* Copyright (C) 2008-2012 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 _UTILITY_HTML
#define _UTILITY_HTML
#include <string>
std::string unescapeHtmlUtf8(const std::string &s);
void stripHtmlTags(std::string &s);
#endif // _UTILITY_HTML

146
src/utility/string.cpp Normal file
View File

@@ -0,0 +1,146 @@
/***************************************************************************
* Copyright (C) 2008-2012 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. *
***************************************************************************/
#include <cassert>
#include <cwctype>
#include <algorithm>
#include "utility/string.h"
std::vector<std::string> split(const std::string &s, const std::string &delimiter)
{
if (delimiter.empty())
return { s };
std::vector<std::string> result;
size_t i = 0, j = 0;
while (true)
{
i = j;
j = s.find(delimiter, i);
if (j == std::string::npos)
break;
else
result.push_back(s.substr(i, j-i));
j += delimiter.length();
}
result.push_back(s.substr(i));
return result;
}
void replace(std::string &s, const std::string &from, const std::string &to)
{
for (size_t i = 0; (i = s.find(from, i)) != std::string::npos; i += to.length())
s.replace(i, from.length(), to);
}
void lowercase(std::string &s)
{
std::transform(s.begin(), s.end(), s.begin(), tolower);
}
void lowercase(std::wstring &ws)
{
std::transform(ws.begin(), ws.end(), ws.begin(), towlower);
}
void trim(std::string &s)
{
if (s.empty())
return;
size_t b = 0;
size_t e = s.length()-1;
while (s[e] == ' ' || s[e] == '\n' || s[e] == '\t')
--e;
++e;
if (e != s.length())
s.resize(e);
while (s[b] == ' ' || s[b] == '\n' || s[e] == '\t')
++b;
if (b != 0)
s = s.substr(b);
}
std::string getBasename(const std::string &path)
{
size_t slash = path.rfind("/");
if (slash == std::string::npos)
return path;
else
return path.substr(slash+1);
}
std::string getParentDirectory(const std::string &path)
{
size_t slash = path.rfind('/');
if (slash == std::string::npos)
return "/";
else
return path.substr(0, slash);
}
std::string getSharedDirectory(const std::string &dir1, const std::string &dir2)
{
size_t i = 0;
size_t min_len = std::min(dir1.length(), dir2.length());
while (i < min_len && !dir1.compare(i, 1, dir2, i, 1))
++i;
i = dir1.rfind("/", i);
if (i == std::string::npos)
return "/";
else
return dir1.substr(0, i);
}
std::string getEnclosedString(const std::string &s, char a, char b, size_t *pos)
{
std::string result;
size_t i = s.find(a, pos ? *pos : 0);
if (i != std::string::npos)
{
++i;
while (i < s.length() && s[i] != b)
{
if (s[i] == '\\' && i+1 < s.length() && s[i+1] == b)
result += s[++i];
else
result += s[i];
++i;
}
// we want to set pos to char after b if possible
if (i < s.length())
++i;
}
if (pos != 0)
*pos = i;
return result;
}
bool isInteger(const char *s)
{
assert(s);
if (*s == '\0')
return false;
for (const char *it = s; *it != '\0'; ++it)
if (!isdigit(*it) && (it != s || *it != '-'))
return false;
return true;
}

43
src/utility/string.h Normal file
View File

@@ -0,0 +1,43 @@
/***************************************************************************
* Copyright (C) 2008-2012 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 _UTILITY_STRING
#define _UTILITY_STRING
#include <string>
#include <vector>
std::vector<std::string> split(const std::string &s, const std::string &delimiter);
void replace(std::string &s, const std::string &from, const std::string &to);
void lowercase(std::string &s);
void lowercase(std::wstring &s);
void trim(std::string &s);
std::string getBasename(const std::string &path);
std::string getParentDirectory(const std::string &path);
std::string getSharedDirectory(const std::string &dir1, const std::string &dir2);
std::string getEnclosedString(const std::string &s, char a, char b, size_t *pos);
bool isInteger(const char *s);
#endif // _UTILITY_STRING