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

@@ -1,5 +1,8 @@
bin_PROGRAMS = ncmpcpp bin_PROGRAMS = ncmpcpp
ncmpcpp_SOURCES = \ ncmpcpp_SOURCES = \
utility/comparators.cpp \
utility/html.cpp \
utility/string.cpp \
actions.cpp \ actions.cpp \
browser.cpp \ browser.cpp \
charset.cpp \ charset.cpp \
@@ -32,10 +35,8 @@ ncmpcpp_SOURCES = \
song.cpp \ song.cpp \
song_info.cpp \ song_info.cpp \
status.cpp \ status.cpp \
string_utilities.cpp \
tag_editor.cpp \ tag_editor.cpp \
tiny_tag_editor.cpp \ tiny_tag_editor.cpp \
tolower.cpp \
visualizer.cpp \ visualizer.cpp \
window.cpp window.cpp
@@ -45,6 +46,9 @@ INCLUDES= $(all_includes)
# the library search path. # the library search path.
ncmpcpp_LDFLAGS = $(all_libraries) ncmpcpp_LDFLAGS = $(all_libraries)
noinst_HEADERS = \ noinst_HEADERS = \
utility/comparators.h \
utility/html.h \
utility/string.h \
browser.h \ browser.h \
charset.h \ charset.h \
clock.h \ clock.h \
@@ -75,9 +79,7 @@ noinst_HEADERS = \
settings.h \ settings.h \
song.h \ song.h \
song_info.h \ song_info.h \
string_utilities.h \
tag_editor.h \ tag_editor.h \
tiny_tag_editor.h \ tiny_tag_editor.h \
tolower.h \
visualizer.h \ visualizer.h \
window.h window.h

View File

@@ -28,6 +28,7 @@
#include "display.h" #include "display.h"
#include "global.h" #include "global.h"
#include "mpdpp.h" #include "mpdpp.h"
#include "utility/comparators.h"
#include "browser.h" #include "browser.h"
#include "clock.h" #include "clock.h"
@@ -42,6 +43,7 @@
#include "server_info.h" #include "server_info.h"
#include "song_info.h" #include "song_info.h"
#include "outputs.h" #include "outputs.h"
#include "utility/string.h"
#include "tag_editor.h" #include "tag_editor.h"
#include "tiny_tag_editor.h" #include "tiny_tag_editor.h"
#include "visualizer.h" #include "visualizer.h"
@@ -395,6 +397,18 @@ void Action::ListsChangeFinisher()
} }
} }
bool Action::ConnectToMPD()
{
if (!Mpd.Connect())
{
std::cout << "Couldn't connect to MPD ";
std::cout << "(host = " << Mpd.GetHostname() << ", port = " << Mpd.GetPort() << ")";
std::cout << ": " << Mpd.GetErrorMessage() << std::endl;
return false;
}
return true;
}
bool Action::AskYesNoQuestion(const std::string &question, void (*callback)()) bool Action::AskYesNoQuestion(const std::string &question, void (*callback)())
{ {
using Global::wFooter; using Global::wFooter;
@@ -1433,7 +1447,7 @@ void EditLibraryTag::Run()
if (dir_to_update.empty()) if (dir_to_update.empty())
dir_to_update = es.getDirectory(); dir_to_update = es.getDirectory();
else else
FindSharedDir(es.getDirectory(), dir_to_update); getSharedDirectory(es.getDirectory(), dir_to_update);
}); });
if (success) if (success)
{ {
@@ -1492,7 +1506,7 @@ void EditLibraryAlbum::Run()
} }
if (success) if (success)
{ {
Mpd.UpdateDirectory(FindSharedDir(myLibrary->Songs)); Mpd.UpdateDirectory(getSharedDirectory(myLibrary->Songs));
ShowMessage("Tags updated successfully"); ShowMessage("Tags updated successfully");
} }
} }
@@ -1541,7 +1555,7 @@ void EditDirectoryName::Run()
const char msg[] = "Directory renamed to \"%s\""; const char msg[] = "Directory renamed to \"%s\"";
ShowMessage(msg, Shorten(TO_WSTRING(new_dir), COLS-static_strlen(msg)).c_str()); ShowMessage(msg, Shorten(TO_WSTRING(new_dir), COLS-static_strlen(msg)).c_str());
if (!myBrowser->isLocal()) if (!myBrowser->isLocal())
Mpd.UpdateDirectory(locale_to_utf_cpy(FindSharedDir(old_dir, new_dir))); Mpd.UpdateDirectory(locale_to_utf_cpy(getSharedDirectory(old_dir, new_dir)));
myBrowser->GetDirectory(myBrowser->CurrentDir()); myBrowser->GetDirectory(myBrowser->CurrentDir());
} }
else else
@@ -2151,7 +2165,7 @@ void AddRandomItems::Run()
mpd_tag_type tag_type = IntoTagItem(answer); mpd_tag_type tag_type = IntoTagItem(answer);
std::string tag_type_str = answer == 's' ? "song" : IntoStr(tag_type); std::string tag_type_str = answer == 's' ? "song" : IntoStr(tag_type);
ToLower(tag_type_str); lowercase(tag_type_str);
LockStatusbar(); LockStatusbar();
Statusbar() << "Number of random " << tag_type_str << "s: "; Statusbar() << "Number of random " << tag_type_str << "s: ";
@@ -2218,7 +2232,7 @@ void ToggleLibraryTagType::Run()
std::string item_type = IntoStr(Config.media_lib_primary_tag); std::string item_type = IntoStr(Config.media_lib_primary_tag);
myLibrary->Artists->SetTitle(Config.titles_visibility ? item_type + "s" : ""); myLibrary->Artists->SetTitle(Config.titles_visibility ? item_type + "s" : "");
myLibrary->Artists->Reset(); myLibrary->Artists->Reset();
ToLower(item_type); lowercase(item_type);
if (myLibrary->Columns() == 2) if (myLibrary->Columns() == 2)
{ {
myLibrary->Songs->Clear(); myLibrary->Songs->Clear();

View File

@@ -109,6 +109,7 @@ struct Action
static void ResizeScreen(); static void ResizeScreen();
static void SetWindowsDimensions(); static void SetWindowsDimensions();
static bool ConnectToMPD();
static bool AskYesNoQuestion(const std::string &question, void (*callback)()); static bool AskYesNoQuestion(const std::string &question, void (*callback)());
static bool isMPDMusicDirSet(); static bool isMPDMusicDirSet();

View File

@@ -32,6 +32,7 @@
#include "playlist.h" #include "playlist.h"
#include "settings.h" #include "settings.h"
#include "status.h" #include "status.h"
#include "utility/comparators.h"
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
# include "tag_editor.h" # include "tag_editor.h"
#endif // HAVE_TAGLIB_H #endif // HAVE_TAGLIB_H
@@ -325,7 +326,7 @@ bool Browser::hasSupportedExtension(const std::string &file)
return false; return false;
std::string ext = file.substr(last_dot+1); std::string ext = file.substr(last_dot+1);
ToLower(ext); lowercase(ext);
return SupportedExtensions.find(ext) != SupportedExtensions.end(); return SupportedExtensions.find(ext) != SupportedExtensions.end();
} }
@@ -594,7 +595,7 @@ std::string Browser::ItemToString(const MPD::Item &item, void *)
{ {
case MPD::itDirectory: case MPD::itDirectory:
{ {
return "[" + ExtractTopName(item.name) + "]"; return "[" + getBasename(item.name) + "]";
} }
case MPD::itSong: case MPD::itSong:
{ {
@@ -605,7 +606,7 @@ std::string Browser::ItemToString(const MPD::Item &item, void *)
} }
case MPD::itPlaylist: case MPD::itPlaylist:
{ {
return Config.browser_playlist_prefix.Str() + ExtractTopName(item.name); return Config.browser_playlist_prefix.Str() + getBasename(item.name);
} }
default: default:
{ {

View File

@@ -293,82 +293,3 @@ void EscapeUnallowedChars(std::string &s)
} }
} }
} }
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] = ' ';
}
}
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')
--e;
++e;
if (e != s.length())
s.resize(e);
while (s[b] == ' ' || s[b] == '\n')
++b;
if (b != 0)
s = s.substr(b);
}

View File

@@ -35,13 +35,6 @@ template <size_t N> inline size_t static_strlen(const char (&)[N])
return N-1; return N-1;
} }
template <size_t N> void Replace(std::string &s, const char (&from)[N], const char *to)
{
size_t to_len = strlen(to);
for (size_t i = 0; (i = s.find(from, i)) != std::string::npos; i += to_len)
s.replace(i, N-1, to);
}
int StrToInt(const std::string &); int StrToInt(const std::string &);
long StrToLong(const std::string &); long StrToLong(const std::string &);
@@ -63,11 +56,5 @@ std::string Shorten(const std::basic_string<my_char_t> &s, size_t max_length);
void EscapeUnallowedChars(std::string &); void EscapeUnallowedChars(std::string &);
std::string unescapeHtmlUtf8(const std::string &data);
void StripHtmlTags(std::string &s);
void Trim(std::string &s);
#endif #endif

View File

@@ -352,12 +352,11 @@ void Display::Tags(const MPD::MutableSong &s, void *data, Menu<MPD::MutableSong>
} }
} }
void Display::Outputs (const MPD::Output &o, void * , Menu< MPD::Output > *menu) void Display::Outputs(const MPD::Output &o, void * , Menu< MPD::Output > *menu)
{ {
*menu << o.name(); *menu << o.name();
} }
void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu) void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
{ {
switch (item.type) switch (item.type)
@@ -369,7 +368,7 @@ void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
*menu << "[..]"; *menu << "[..]";
return; return;
} }
*menu << "[" << ExtractTopName(item.name) << "]"; *menu << "[" << getBasename(item.name) << "]";
return; return;
} }
case MPD::itSong: case MPD::itSong:
@@ -379,7 +378,7 @@ void Display::Items(const MPD::Item &item, void *data, Menu<MPD::Item> *menu)
Display::SongsInColumns(item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu)); Display::SongsInColumns(item.song, data, reinterpret_cast<Menu<MPD::Song> *>(menu));
return; return;
case MPD::itPlaylist: case MPD::itPlaylist:
*menu << Config.browser_playlist_prefix << ExtractTopName(item.name); *menu << Config.browser_playlist_prefix << getBasename(item.name);
return; return;
default: default:
return; return;

View File

@@ -28,7 +28,7 @@ namespace
{ {
const char *Timestamp() const char *Timestamp()
{ {
static char result[32]; char result[32];
time_t raw; time_t raw;
tm *t; tm *t;
time(&raw); time(&raw);

View File

@@ -39,16 +39,6 @@
#include "outputs.h" #include "outputs.h"
#include "visualizer.h" #include "visualizer.h"
bool ConnectToMPD()
{
if (!Mpd.Connect())
{
std::cout << "Couldn't connect to MPD (host = " << Mpd.GetHostname() << ", port = " << Mpd.GetPort() << "): " << Mpd.GetErrorMessage() << std::endl;
return false;
}
return true;
}
void ParseArgv(int argc, char **argv) void ParseArgv(int argc, char **argv)
{ {
bool quit = 0; bool quit = 0;
@@ -143,7 +133,7 @@ void ParseArgv(int argc, char **argv)
exit(0); exit(0);
} }
if (!ConnectToMPD()) if (!Action::ConnectToMPD())
exit(1); exit(1);
if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--screen")) if (!strcmp(argv[i], "-s") || !strcmp(argv[i], "--screen"))
@@ -203,8 +193,8 @@ void ParseArgv(int argc, char **argv)
now_playing_format = "{"; now_playing_format = "{";
now_playing_format += argv[i]; now_playing_format += argv[i];
now_playing_format += "}"; now_playing_format += "}";
Replace(now_playing_format, "\\n", "\n"); replace(now_playing_format, "\\n", "\n");
Replace(now_playing_format, "\\t", "\t"); replace(now_playing_format, "\\t", "\t");
} }
} }
std::cout << utf_to_locale_cpy(Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n"; std::cout << utf_to_locale_cpy(Mpd.GetCurrentlyPlayingSong().toString(now_playing_format)) << "\n";
@@ -280,59 +270,6 @@ void ParseArgv(int argc, char **argv)
exit(0); exit(0);
} }
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(ExtractTopName(a.name), ExtractTopName(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;
}
std::string Timestamp(time_t t) std::string Timestamp(time_t t)
{ {
char result[32]; char result[32];
@@ -365,10 +302,10 @@ void UpdateSongList(Menu<MPD::Song> *menu)
} }
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
std::string FindSharedDir(const MPD::SongList &v) std::string getSharedDirectory(const MPD::SongList &v)
{ {
if (v.empty()) // this should never happen, but in case... if (v.empty()) // this should never happen, but in case...
FatalError("empty SongList passed to FindSharedDir(const SongList &)!"); FatalError("empty SongList passed to getSharedDirectory(const SongList &)!");
size_t i = -1; size_t i = -1;
std::string first = v.front().getDirectory(); std::string first = v.front().getDirectory();
for (MPD::SongList::const_iterator it = ++v.begin(); it != v.end(); ++it) for (MPD::SongList::const_iterator it = ++v.begin(); it != v.end(); ++it)
@@ -384,63 +321,6 @@ std::string FindSharedDir(const MPD::SongList &v)
} }
#endif // HAVE_TAGLIB_H #endif // HAVE_TAGLIB_H
std::string FindSharedDir(const std::string &one, const std::string &two)
{
if (one == two)
return one;
size_t i = 0;
while (!one.compare(i, 1, two, i, 1))
++i;
i = one.rfind("/", i);
return i != std::string::npos ? one.substr(0, i) : "/";
}
std::string GetLineValue(std::string &line, char a, char b, bool once)
{
int pos[2] = { -1, -1 };
char x = a;
size_t i = 0;
while ((i = line.find(x, i)) != std::string::npos && pos[1] < 0)
{
if (i && line[i-1] == '\\')
{
i++;
continue;
}
if (once)
line[i] = 0;
pos[pos[0] >= 0] = i++;
if (x == a)
x = b;
}
++pos[0];
std::string result = pos[0] >= 0 && pos[1] >= 0 ? line.substr(pos[0], pos[1]-pos[0]) : "";
// replace \a and \b with a and b respectively
char r1[] = "\\ ", r2[] = " ";
r1[1] = r2[0] = a;
Replace(result, r1, r2);
if (a != b)
{
r1[1] = r2[0] = b;
Replace(result, r1, r2);
}
return result;
}
std::string ExtractTopName(const std::string &s)
{
size_t slash = s.rfind("/");
return slash != std::string::npos ? s.substr(++slash) : s;
}
std::string PathGoDownOneLevel(const std::string &path)
{
size_t i = path.rfind('/');
return i == std::string::npos ? "/" : path.substr(0, i);
}
std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width) std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width)
{ {
std::basic_string<my_char_t> s(str); std::basic_string<my_char_t> s(str);
@@ -473,14 +353,3 @@ std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, s
result = s; result = s;
return result; 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;
}

View File

@@ -27,47 +27,8 @@
#include "settings.h" #include "settings.h"
#include "status.h" #include "status.h"
bool ConnectToMPD();
void ParseArgv(int, char **); void ParseArgv(int, char **);
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 &, const MPD::Item &);
};
template <typename A, typename B> std::string StringPairToString(const std::pair<A, B> &pair, void *) template <typename A, typename B> std::string StringPairToString(const std::pair<A, B> &pair, void *)
{ {
return pair.first; return pair.first;
@@ -211,39 +172,26 @@ template <typename T> void ShowTag(T &buf, const std::string &tag)
buf << tag; buf << tag;
} }
inline bool Keypressed(int in, const int *key)
{
return in == key[0] || in == key[1];
}
std::string Timestamp(time_t t); std::string Timestamp(time_t t);
void UpdateSongList(Menu<MPD::Song> *); void UpdateSongList(Menu<MPD::Song> *);
std::string FindSharedDir(const std::string &, const std::string &);
#ifdef HAVE_TAGLIB_H #ifdef HAVE_TAGLIB_H
template <typename T> std::string FindSharedDir(Menu<T> *menu) template <typename T> std::string getSharedDirectory(Menu<T> *menu)
{ {
assert(!menu->Empty()); assert(!menu->Empty());
std::string dir; std::string dir;
dir = (*menu)[0].getDirectory(); dir = (*menu)[0].getDirectory();
for (size_t i = 1; i < menu->Size(); ++i) for (size_t i = 1; i < menu->Size(); ++i)
dir = FindSharedDir(dir, (*menu)[i].getDirectory()); {
dir = getSharedDirectory(dir, (*menu)[i].getDirectory());
if (dir == "/")
break;
}
return dir; return dir;
} }
std::string FindSharedDir(const MPD::SongList &);
#endif // HAVE_TAGLIB_H #endif // HAVE_TAGLIB_H
std::string ExtractTopName(const std::string &);
std::string PathGoDownOneLevel(const std::string &path);
std::string GetLineValue(std::string &, char = '"', char = '"', bool = 0);
std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width); std::basic_string<my_char_t> Scroller(const std::basic_string<my_char_t> &str, size_t &pos, size_t width);
bool askYesNoQuestion(const Buffer &question, void (*callback)());
bool isInteger(const char *s);
#endif #endif

View File

@@ -124,7 +124,7 @@ void Lastfm::Load()
locale_to_utf(artist); locale_to_utf(artist);
std::string file = artist + ".txt"; std::string file = artist + ".txt";
ToLower(file); lowercase(file);
EscapeUnallowedChars(file); EscapeUnallowedChars(file);
itsFilename = itsFolder + "/" + file; itsFilename = itsFolder + "/" + file;

View File

@@ -25,6 +25,7 @@
#include "conv.h" #include "conv.h"
#include "curl_handle.h" #include "curl_handle.h"
#include "settings.h" #include "settings.h"
#include "utility/html.h"
const char *LastfmService::baseURL = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method="; const char *LastfmService::baseURL = "http://ws.audioscrobbler.com/2.0/?api_key=d94e5b6e26469a2d1ffae8ef20131b79&method=";
@@ -55,7 +56,7 @@ LastfmService::Result LastfmService::fetch(Args &args)
if (actionFailed(data)) if (actionFailed(data))
{ {
StripHtmlTags(data); stripHtmlTags(data);
result.second = data; result.second = data;
return result; return result;
} }
@@ -91,8 +92,8 @@ bool LastfmService::actionFailed(const std::string &data)
void LastfmService::postProcess(std::string &data) void LastfmService::postProcess(std::string &data)
{ {
StripHtmlTags(data); stripHtmlTags(data);
Trim(data); trim(data);
} }
/***********************************************************************/ /***********************************************************************/
@@ -147,7 +148,7 @@ bool ArtistInfo::parse(std::string &data)
k += static_strlen("<url>"); k += static_strlen("<url>");
similars.push_back(std::make_pair(data.substr(i, j-i), data.substr(k, l-k))); similars.push_back(std::make_pair(data.substr(i, j-i), data.substr(k, l-k)));
StripHtmlTags(similars.back().first); stripHtmlTags(similars.back().first);
} }
a += static_strlen("<![CDATA["); a += static_strlen("<![CDATA[");

View File

@@ -27,6 +27,7 @@
#include "charset.h" #include "charset.h"
#include "conv.h" #include "conv.h"
#include "lyrics_fetcher.h" #include "lyrics_fetcher.h"
#include "utility/html.h"
LyricsFetcher *lyricsPlugins[] = LyricsFetcher *lyricsPlugins[] =
{ {
@@ -53,8 +54,8 @@ LyricsFetcher::Result LyricsFetcher::fetch(const std::string &artist, const std:
result.first = false; result.first = false;
std::string url = getURL(); std::string url = getURL();
Replace(url, "%artist%", artist.c_str()); replace(url, "%artist%", artist.c_str());
Replace(url, "%title%", title.c_str()); replace(url, "%title%", title.c_str());
std::string data; std::string data;
CURLcode code = Curl::perform(data, url); CURLcode code = Curl::perform(data, url);
@@ -98,8 +99,8 @@ bool LyricsFetcher::getContent(const char *open_tag, const char *close_tag, std:
void LyricsFetcher::postProcess(std::string &data) void LyricsFetcher::postProcess(std::string &data)
{ {
StripHtmlTags(data); stripHtmlTags(data);
Trim(data); trim(data);
} }
/***********************************************************************/ /***********************************************************************/
@@ -134,9 +135,9 @@ LyricsFetcher::Result LyricwikiFetcher::fetch(const std::string &artist, const s
return result; return result;
} }
Replace(data, "<br />", "\n"); replace(data, "<br />", "\n");
StripHtmlTags(data); stripHtmlTags(data);
Trim(data); trim(data);
result.second = data; result.second = data;
result.first = true; result.first = true;
@@ -225,8 +226,8 @@ void MetrolyricsFetcher::postProcess(std::string &data)
// some of lyrics have both \n chars and <br />, html tags // some of lyrics have both \n chars and <br />, html tags
// are always present whereas \n chars are not, so we need to // are always present whereas \n chars are not, so we need to
// throw them away to avoid having line breaks doubled. // throw them away to avoid having line breaks doubled.
Replace(data, "&#10;", ""); replace(data, "&#10;", "");
Replace(data, "<br />", "\n"); replace(data, "<br />", "\n");
data = unescapeHtmlUtf8(data); data = unescapeHtmlUtf8(data);
LyricsFetcher::postProcess(data); LyricsFetcher::postProcess(data);
} }

View File

@@ -29,6 +29,7 @@
#include "mpdpp.h" #include "mpdpp.h"
#include "playlist.h" #include "playlist.h"
#include "status.h" #include "status.h"
#include "utility/comparators.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;
@@ -162,7 +163,7 @@ void MediaLibrary::SwitchTo()
if (Config.titles_visibility) if (Config.titles_visibility)
{ {
std::string item_type = IntoStr(Config.media_lib_primary_tag); std::string item_type = IntoStr(Config.media_lib_primary_tag);
ToLower(item_type); lowercase(item_type);
Albums->SetTitle("Albums (sorted by " + item_type + ")"); Albums->SetTitle("Albums (sorted by " + item_type + ")");
} }
else else
@@ -635,7 +636,7 @@ void MediaLibrary::LocateSong(const MPD::Song &s)
if (primary_tag.empty()) if (primary_tag.empty())
{ {
std::string item_type = IntoStr(Config.media_lib_primary_tag); std::string item_type = IntoStr(Config.media_lib_primary_tag);
ToLower(item_type); lowercase(item_type);
ShowMessage("Can't use this function because the song has no %s tag set", item_type.c_str()); ShowMessage("Can't use this function because the song has no %s tag set", item_type.c_str());
return; return;
} }
@@ -726,7 +727,7 @@ void MediaLibrary::AddToPlaylist(bool add_n_play)
|| (w == Albums && Albums->Current().Date == AllTracksMarker)) || (w == Albums && Albums->Current().Date == AllTracksMarker))
{ {
std::string tag_type = IntoStr(Config.media_lib_primary_tag); std::string tag_type = IntoStr(Config.media_lib_primary_tag);
ToLower(tag_type); lowercase(tag_type);
ShowMessage("Songs with %s = \"%s\" added", tag_type.c_str(), Artists->Current().c_str()); ShowMessage("Songs with %s = \"%s\" added", tag_type.c_str(), Artists->Current().c_str());
} }
else if (w == Albums) else if (w == Albums)

View File

@@ -19,7 +19,7 @@
***************************************************************************/ ***************************************************************************/
#include "mutable_song.h" #include "mutable_song.h"
#include "string_utilities.h" #include "utility/string.h"
namespace MPD {// namespace MPD {//

View File

@@ -116,7 +116,7 @@ int main(int argc, char **argv)
if (argc > 1) if (argc > 1)
ParseArgv(argc, argv); ParseArgv(argc, argv);
if (!ConnectToMPD()) if (!Action::ConnectToMPD())
exit(1); exit(1);
if (Mpd.Version() < 14) if (Mpd.Version() < 14)

View File

@@ -27,6 +27,7 @@
#include "playlist.h" #include "playlist.h"
#include "song.h" #include "song.h"
#include "status.h" #include "status.h"
#include "utility/comparators.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;

View File

@@ -30,6 +30,7 @@
#include "mpdpp.h" #include "mpdpp.h"
#include "status.h" #include "status.h"
#include "tag_editor.h" #include "tag_editor.h"
#include "utility/comparators.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;

View File

@@ -27,6 +27,7 @@
#include "search_engine.h" #include "search_engine.h"
#include "settings.h" #include "settings.h"
#include "status.h" #include "status.h"
#include "utility/comparators.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;

View File

@@ -26,6 +26,7 @@
#include "playlist.h" #include "playlist.h"
#include "playlist_editor.h" #include "playlist_editor.h"
#include "sel_items_adder.h" #include "sel_items_adder.h"
#include "utility/comparators.h"
using Global::MainHeight; using Global::MainHeight;
using Global::MainStartY; using Global::MainStartY;

View File

@@ -104,7 +104,7 @@ namespace
if (equal == std::string::npos) if (equal == std::string::npos)
return ""; return "";
std::string result = s.substr(0, equal); std::string result = s.substr(0, equal);
Trim(result); trim(result);
return result; return result;
} }
@@ -445,7 +445,7 @@ void Configuration::Read()
if (!cl.empty() && cl[0] != '#') if (!cl.empty() && cl[0] != '#')
{ {
name = GetOptionName(cl); name = GetOptionName(cl);
v = GetLineValue(cl); v = getEnclosedString(cl, '"', '"', 0);
if (name == "ncmpcpp_directory") if (name == "ncmpcpp_directory")
{ {
@@ -1078,11 +1078,12 @@ void Configuration::GenerateColumns()
{ {
columns.clear(); columns.clear();
std::string width; std::string width;
while (!(width = GetLineValue(song_list_columns_format, '(', ')', 1)).empty()) size_t pos = 0;
while (!(width = getEnclosedString(song_list_columns_format, '(', ')', &pos)).empty())
{ {
Column col; Column col;
col.color = IntoColor(GetLineValue(song_list_columns_format, '[', ']', 1)); col.color = IntoColor(getEnclosedString(song_list_columns_format, '[', ']', &pos));
std::string tag_type = GetLineValue(song_list_columns_format, '{', '}', 1); std::string tag_type = getEnclosedString(song_list_columns_format, '{', '}', &pos);
col.fixed = *width.rbegin() == 'f'; col.fixed = *width.rbegin() == 'f';

View File

@@ -200,7 +200,7 @@ void NcmpcppErrorCallback(MPD::Connection *, int errorid, const char *msg, void
} }
else if ((errorid >> 8) == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser) else if ((errorid >> 8) == MPD_SERVER_ERROR_NO_EXIST && myScreen == myBrowser)
{ {
myBrowser->GetDirectory(PathGoDownOneLevel(myBrowser->CurrentDir())); myBrowser->GetDirectory(getParentDirectory(myBrowser->CurrentDir()));
myBrowser->Refresh(); myBrowser->Refresh();
} }
else else

View File

@@ -22,8 +22,8 @@
#define _STRBUFFER_H #define _STRBUFFER_H
#include "numeric_conversions.h" #include "numeric_conversions.h"
#include "tolower.h"
#include "window.h" #include "window.h"
#include "utility/string.h"
#include <list> #include <list>
@@ -260,8 +260,8 @@ template <typename C> bool basic_buffer<C>::SetFormatting(
std::basic_string<C> base = itsString; std::basic_string<C> base = itsString;
if (!case_sensitive) if (!case_sensitive)
{ {
ToLower(s); lowercase(s);
ToLower(base); lowercase(base);
} }
FormatPos fp; FormatPos fp;
for (size_t i = base.find(s); i != std::basic_string<C>::npos; i = base.find(s, i)) for (size_t i = base.find(s); i != std::basic_string<C>::npos; i = base.find(s, i))
@@ -294,8 +294,8 @@ template <typename C> void basic_buffer<C>::RemoveFormatting(
std::basic_string<C> base = itsString; std::basic_string<C> base = itsString;
if (!case_sensitive) if (!case_sensitive)
{ {
ToLower(pattern); lowercase(pattern);
ToLower(base); lowercase(base);
} }
FormatPos fp; FormatPos fp;
for (size_t i = base.find(pattern); i != std::basic_string<C>::npos; i = base.find(pattern, i)) for (size_t i = base.find(pattern); i != std::basic_string<C>::npos; i = base.find(pattern, i))

View File

@@ -38,6 +38,7 @@
#include "global.h" #include "global.h"
#include "song_info.h" #include "song_info.h"
#include "playlist.h" #include "playlist.h"
#include "utility/comparators.h"
using Global::myScreen; using Global::myScreen;
using Global::MainHeight; using Global::MainHeight;
@@ -637,7 +638,7 @@ void TagEditor::EnterPressed()
w->Refresh(); w->Refresh();
w = LeftColumn; w = LeftColumn;
LeftColumn->HighlightColor(Config.active_column_color); LeftColumn->HighlightColor(Config.active_column_color);
Mpd.UpdateDirectory(FindSharedDir(Tags)); Mpd.UpdateDirectory(getSharedDirectory(Tags));
} }
else else
Tags->Clear(); Tags->Clear();
@@ -927,7 +928,7 @@ void TagEditor::LocateSong(const MPD::Song &s)
Dirs->Reset(); // go to the first pos, which is "." (music dir root) Dirs->Reset(); // go to the first pos, which is "." (music dir root)
// highlight directory we need and get files from it // highlight directory we need and get files from it
std::string dir = ExtractTopName(s.getDirectory()); std::string dir = getBasename(s.getDirectory());
for (size_t i = 0; i < Dirs->Size(); ++i) for (size_t i = 0; i < Dirs->Size(); ++i)
{ {
if ((*Dirs)[i].first == dir) if ((*Dirs)[i].first == dir)
@@ -1131,7 +1132,7 @@ void TagEditor::LowerAllLetters(MPD::MutableSong &s)
unsigned i = 0; unsigned i = 0;
for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i) for (std::string tag; !(tag = (s.*m->Get)(i)).empty(); ++i)
{ {
ToLower(tag); lowercase(tag);
(s.*m->Set)(tag, i); (s.*m->Set)(tag, i);
} }
} }

View File

@@ -188,7 +188,7 @@ bool TinyTagEditor::getTags()
std::string ext = itsEdited.getURI(); std::string ext = itsEdited.getURI();
ext = ext.substr(ext.rfind(".")+1); ext = ext.substr(ext.rfind(".")+1);
ToLower(ext); lowercase(ext);
if (!isInitialized) if (!isInitialized)
Init(); Init();

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;
}

View File

@@ -18,25 +18,48 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <cassert> #ifndef _UTILITY_COMPARATORS
#include "string_utilities.h" #define _UTILITY_COMPARATORS
std::vector<std::string> split(const std::string &s, const std::string &delimiter) #include <string>
#include "mpdpp.h"
class CaseInsensitiveStringComparison
{ {
if (delimiter.empty()) bool hasTheWord(const std::string &s)
return { s };
std::vector<std::string> result;
size_t i = 0, j = 0;
while (true)
{ {
i = j; return (s.length() > 3)
j = s.find(delimiter, i); && (s[0] == 't' || s[0] == 'T')
if (j == std::string::npos) && (s[1] == 'h' || s[1] == 'H')
break; && (s[2] == 'e' || s[2] == 'E')
else && (s[3] == ' ');
result.push_back(s.substr(i, j-i));
j += delimiter.length();
} }
result.push_back(s.substr(i));
return result; 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

View File

@@ -18,18 +18,63 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#include <algorithm> #include "utility/html.h"
#include <wctype.h> #include "utility/string.h"
#include "tolower.h" std::string unescapeHtmlUtf8(const std::string &data)
void ToLower(std::string &s)
{ {
transform(s.begin(), s.end(), s.begin(), tolower); 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 ToLower(std::wstring &s) void stripHtmlTags(std::string &s)
{ {
transform(s.begin(), s.end(), s.begin(), towlower); 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] = ' ';
}
}

View File

@@ -18,13 +18,13 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef HAVE_TOLOWER_H #ifndef _UTILITY_HTML
#define HAVE_TOLOWER_H #define _UTILITY_HTML
#include <string> #include <string>
void ToLower(std::string &); std::string unescapeHtmlUtf8(const std::string &s);
void ToLower(std::wstring &);
#endif 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;
}

View File

@@ -18,12 +18,26 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/ ***************************************************************************/
#ifndef _STRING_UTILITIES #ifndef _UTILITY_STRING
#define _STRING_UTILITIES #define _UTILITY_STRING
#include <string> #include <string>
#include <vector> #include <vector>
std::vector<std::string> split(const std::string &s, const std::string &delimiter); 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);
#endif // _STRING_UTILITIES 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