rewrite MPD::Song object

This commit is contained in:
Andrzej Rybczak
2012-08-27 23:22:15 +02:00
parent e0e787716f
commit a270fc8402
23 changed files with 524 additions and 725 deletions

View File

@@ -18,337 +18,313 @@
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <memory>
#include "charset.h"
#include "conv.h"
#include "error.h"
#include "song.h"
#include "conv.h"
namespace
namespace {//
unsigned calc_hash(const char* s, unsigned seed = 0)
{
unsigned calc_hash(const char *p)
{
unsigned hash = 5381;
while (*p)
hash = (hash << 5) + hash + *p++;
return hash;
}
unsigned hash = seed;
while (*s)
hash = hash * 101 + *s++;
return hash;
}
MPD::Song::Song(mpd_song *s, bool copy_ptr) : itsSong(s),
itsFile(0),
itsTags(0),
itsSlash(std::string::npos),
itsHash(0),
copyPtr(copy_ptr),
isLocalised(0)
{
if (itsSong)
SetHashAndSlash();
}
MPD::Song::Song(const Song &s) : itsSong(s.copyPtr ? s.itsSong : mpd_song_dup(s.itsSong)),
itsFile(s.itsFile ? strdup(s.itsFile) : 0),
itsTags(s.itsTags ? new TagMap(*s.itsTags) : 0),
itsNewName(s.itsNewName),
itsSlash(s.itsSlash),
itsHash(s.itsHash),
copyPtr(s.copyPtr),
isLocalised(s.isLocalised)
{
}
namespace MPD {//
MPD::Song::~Song()
struct SongImpl
{
if (itsSong)
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
}
std::string MPD::Song::GetLength(unsigned pos) const
{
if (pos > 0)
return "";
unsigned len = mpd_song_get_duration(itsSong);
return !len ? "-:--" : ShowTime(len);
}
std::string MPD::Song::GetPriority(unsigned pos) const
{
if (pos > 0)
return "";
return IntoStr(GetPrio());
}
void MPD::Song::Localize()
{
# ifdef HAVE_ICONV_H
if (isLocalised)
return;
const char *tag, *conv_tag;
conv_tag = tag = mpd_song_get_uri(itsSong);
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // file has been converted
{
itsFile = conv_tag;
SetHashAndSlash();
}
for (unsigned t = MPD_TAG_ARTIST; t <= MPD_TAG_DISC; ++t)
{
unsigned pos = 0;
for (; (tag = mpd_song_get_tag(itsSong, mpd_tag_type(t), pos)); ++pos)
{
conv_tag = tag;
utf_to_locale(conv_tag, 0);
if (tag != conv_tag) // tag has been converted
{
SetTag(mpd_tag_type(t), pos, conv_tag);
delete [] conv_tag;
}
}
}
isLocalised = 1;
# endif // HAVE_ICONV_H
}
void MPD::Song::Clear()
{
if (itsSong)
mpd_song_free(itsSong);
itsSong = 0;
mpd_song *m_song;
unsigned m_hash;
delete [] itsFile;
itsFile = 0;
SongImpl(mpd_song *s, unsigned hash) : m_song(s), m_hash(hash) { }
~SongImpl() { mpd_song_free(m_song); }
delete itsTags;
itsTags = 0;
itsNewName.clear();
itsSlash = std::string::npos;
itsHash = 0;
isLocalised = 0;
copyPtr = 0;
const char *getTag(mpd_tag_type type, unsigned idx)
{
const char *tag = mpd_song_get_tag(m_song, type, idx);
return tag ? tag : "";
}
};
Song::Song(mpd_song *s)
{
assert(s);
pimpl = std::shared_ptr<SongImpl>(new SongImpl(s, calc_hash(mpd_song_get_uri(s))));
}
bool MPD::Song::Empty() const
std::string Song::getURI(unsigned idx) const
{
return !itsSong;
}
bool MPD::Song::isFromDB() const
{
return (MyFilename()[0] != '/') || itsSlash == std::string::npos;
}
bool MPD::Song::isStream() const
{
return !strncmp(MyFilename(), "http://", 7);
}
std::string MPD::Song::GetFile(unsigned pos) const
{
return pos > 0 ? "" : MyFilename();
}
std::string MPD::Song::GetName(unsigned pos) const
{
if (pos > 0)
assert(pimpl);
if (idx > 0)
return "";
std::string name = GetTag(MPD_TAG_NAME, 0);
if (!name.empty())
return name;
else if (itsSlash != std::string::npos)
return MyFilename()+itsSlash+1;
else
return MyFilename();
return mpd_song_get_uri(pimpl->m_song);
}
std::string MPD::Song::GetDirectory(unsigned pos) const
std::string Song::getName(unsigned idx) const
{
if (pos > 0 || isStream())
assert(pimpl);
mpd_song *s = pimpl->m_song;
const char *res = mpd_song_get_tag(s, MPD_TAG_NAME, idx);
if (!res && idx > 0)
return "";
else if (itsSlash == std::string::npos)
const char *uri = mpd_song_get_uri(s);
const char *name = strrchr(uri, '/');
if (name)
return name+1;
else
return uri;
}
std::string Song::getDirectory(unsigned idx) const
{
assert(pimpl);
if (idx > 0 || isStream())
return "";
const char *uri = mpd_song_get_uri(pimpl->m_song);
const char *name = strrchr(uri, '/');
if (name)
return std::string(uri, name-uri);
else
return "/";
}
std::string Song::getArtist(unsigned idx) const
{
assert(pimpl);
return pimpl->getTag(MPD_TAG_ARTIST, idx);
}
std::string Song::getTitle(unsigned idx) const
{
assert(pimpl);
return pimpl->getTag(MPD_TAG_TITLE, idx);
}
std::string Song::getAlbum(unsigned idx) const
{
assert(pimpl);
return pimpl->getTag(MPD_TAG_ALBUM, idx);
}
std::string Song::getAlbumArtist(unsigned idx) const
{
assert(pimpl);
return pimpl->getTag(MPD_TAG_ALBUM_ARTIST, idx);
}
std::string Song::getTrack(unsigned idx) const
{
assert(pimpl);
std::string track = pimpl->getTag(MPD_TAG_TRACK, idx);
if ((track.length() == 1 && track[0] != '0')
|| (track.length() > 3 && track[1] == '/'))
return "0"+track;
else
return std::string(MyFilename(), itsSlash);
return track;
}
std::string MPD::Song::GetArtist(unsigned pos) const
std::string Song::getTrackNumber(unsigned idx) const
{
return GetTag(MPD_TAG_ARTIST, pos);
}
std::string MPD::Song::GetTitle(unsigned pos) const
{
return GetTag(MPD_TAG_TITLE, pos);
}
std::string MPD::Song::GetAlbum(unsigned pos) const
{
return GetTag(MPD_TAG_ALBUM, pos);
}
std::string MPD::Song::GetAlbumArtist(unsigned pos) const
{
return GetTag(MPD_TAG_ALBUM_ARTIST, pos);
}
std::string MPD::Song::GetTrack(unsigned pos) const
{
std::string track = GetTag(MPD_TAG_TRACK, pos);
return (track.length() == 1 && track[0] != '0') || (track.length() > 3 && track[1] == '/') ? "0"+track : track;
}
std::string MPD::Song::GetTrackNumber(unsigned pos) const
{
std::string track = GetTag(MPD_TAG_TRACK, pos);
assert(pimpl);
std::string track = pimpl->getTag(MPD_TAG_TRACK, idx);
size_t slash = track.find('/');
if (slash != std::string::npos)
track.resize(slash);
return track.length() == 1 && track[0] != '0' ? "0"+track : track;
if (track.length() == 1 && track[0] != '0')
return "0"+track;
else
return track;
}
std::string MPD::Song::GetDate(unsigned pos) const
std::string Song::getDate(unsigned idx) const
{
return GetTag(MPD_TAG_DATE, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_DATE, idx);
}
std::string MPD::Song::GetGenre(unsigned pos) const
std::string Song::getGenre(unsigned idx) const
{
return GetTag(MPD_TAG_GENRE, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_GENRE, idx);
}
std::string MPD::Song::GetComposer(unsigned pos) const
std::string Song::getComposer(unsigned idx) const
{
return GetTag(MPD_TAG_COMPOSER, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_COMPOSER, idx);
}
std::string MPD::Song::GetPerformer(unsigned pos) const
std::string Song::getPerformer(unsigned idx) const
{
return GetTag(MPD_TAG_PERFORMER, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_PERFORMER, idx);
}
std::string MPD::Song::GetDisc(unsigned pos) const
std::string Song::getDisc(unsigned idx) const
{
return GetTag(MPD_TAG_DISC, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_DISC, idx);
}
std::string MPD::Song::GetComment(unsigned pos) const
std::string Song::getComment(unsigned idx) const
{
return GetTag(MPD_TAG_COMMENT, pos);
assert(pimpl);
return pimpl->getTag(MPD_TAG_COMMENT, idx);
}
std::string MPD::Song::GetTags(GetFunction f) const
std::string Song::getLength(unsigned idx) const
{
unsigned pos = 0;
assert(pimpl);
if (idx > 0)
return "";
unsigned len = mpd_song_get_duration(pimpl->m_song);
if (len > 0)
return ShowTime(len);
else
return "-:--";
}
std::string Song::getPriority(unsigned idx) const
{
assert(pimpl);
if (idx > 0)
return "";
char buf[10];
snprintf(buf, sizeof(buf), "%d", getPrio());
return buf;
}
std::string MPD::Song::getTags(GetFunction f, const std::string tag_separator) const
{
assert(pimpl);
unsigned idx = 0;
std::string result;
for (std::string tag; !(tag = (this->*f)(pos)).empty(); ++pos)
for (std::string tag; !(tag = (this->*f)(idx)).empty(); ++idx)
{
if (!result.empty())
result += ", ";
result += tag_separator;
result += tag;
}
return result;
}
void MPD::Song::SetArtist(const std::string &str, unsigned pos)
unsigned Song::getHash() const
{
SetTag(MPD_TAG_ARTIST, pos, str);
assert(pimpl);
return pimpl->m_hash;
}
void MPD::Song::SetTitle(const std::string &str, unsigned pos)
unsigned Song::getDuration() const
{
SetTag(MPD_TAG_TITLE, pos, str);
assert(pimpl);
return mpd_song_get_duration(pimpl->m_song);
}
void MPD::Song::SetAlbum(const std::string &str, unsigned pos)
unsigned Song::getPosition() const
{
SetTag(MPD_TAG_ALBUM, pos, str);
assert(pimpl);
return mpd_song_get_pos(pimpl->m_song);
}
void MPD::Song::SetAlbumArtist(const std::string &str, unsigned pos)
unsigned Song::getID() const
{
SetTag(MPD_TAG_ALBUM_ARTIST, pos, str);
assert(pimpl);
return mpd_song_get_id(pimpl->m_song);
}
void MPD::Song::SetTrack(const std::string &str, unsigned pos)
unsigned Song::getPrio() const
{
SetTag(MPD_TAG_TRACK, pos, str);
assert(pimpl);
return mpd_song_get_prio(pimpl->m_song);
}
void MPD::Song::SetTrack(unsigned track, unsigned pos)
time_t Song::getMTime() const
{
SetTag(MPD_TAG_TRACK, pos, IntoStr(track));
assert(pimpl);
return mpd_song_get_last_modified(pimpl->m_song);
}
void MPD::Song::SetDate(const std::string &str, unsigned pos)
bool Song::isFromDatabase() const
{
SetTag(MPD_TAG_DATE, pos, str);
assert(pimpl);
const char *uri = mpd_song_get_uri(pimpl->m_song);
return uri[0] != '/' || !strrchr(uri, '/');
}
void MPD::Song::SetGenre(const std::string &str, unsigned pos)
bool Song::isStream() const
{
SetTag(MPD_TAG_GENRE, pos, str);
assert(pimpl);
return !strncmp(mpd_song_get_uri(pimpl->m_song), "http://", 7);
}
void MPD::Song::SetComposer(const std::string &str, unsigned pos)
bool Song::empty() const
{
SetTag(MPD_TAG_COMPOSER, pos, str);
return pimpl == 0;
}
void MPD::Song::SetPerformer(const std::string &str, unsigned pos)
std::string Song::toString(const std::string &fmt, const std::string &tag_separator, const std::string &escape_chars) const
{
SetTag(MPD_TAG_PERFORMER, pos, str);
assert(pimpl);
std::string::const_iterator it = fmt.begin();
return ParseFormat(it, tag_separator, escape_chars);
}
void MPD::Song::SetDisc(const std::string &str, unsigned pos)
std::string Song::ShowTime(unsigned length)
{
SetTag(MPD_TAG_DISC, pos, str);
int hours = length/3600;
length -= hours*3600;
int minutes = length/60;
length -= minutes*60;
int seconds = length;
char buf[10];
if (hours > 0)
snprintf(buf, sizeof(buf), "%d:%02d:%02d", hours, minutes, seconds);
else
snprintf(buf, sizeof(buf), "%d:%02d", minutes, seconds);
return buf;
}
void MPD::Song::SetComment(const std::string &str, unsigned pos)
bool MPD::Song::isFormatOk(const std::string &type, const std::string &fmt)
{
SetTag(MPD_TAG_COMMENT, pos, str);
}
void MPD::Song::SetTags(SetFunction f, const std::string &value)
{
unsigned pos = 0;
// tag editor can save multiple instances of performer and composer
// tag, so we need to split them and allow it to read them separately.
if (f == &Song::SetComposer || f == &Song::SetPerformer)
int braces = 0;
for (std::string::const_iterator it = fmt.begin(); it != fmt.end(); ++it)
{
for (size_t i = 0; i != std::string::npos; i = value.find(",", i))
if (*it == '{')
++braces;
else if (*it == '}')
--braces;
}
if (braces)
{
std::cerr << type << ": number of opening and closing braces does not equal!\n";
return false;
}
for (size_t i = fmt.find('%'); i != std::string::npos; i = fmt.find('%', i))
{
if (isdigit(fmt[++i]))
while (isdigit(fmt[++i])) { }
if (!toGetFunction(fmt[i]))
{
if (i)
++i;
while (value[i] == ' ')
++i;
size_t j = value.find(",", i);
(this->*f)(value.substr(i, j-i), pos++);
std::cerr << type << ": invalid character at position " << IntoStr(i+1) << ": '" << fmt[i] << "'\n";
return false;
}
}
else
(this->*f)(value, pos++);
// there should be empty tag at the end since if we are
// reading them, original tag from mpd_song at the position
// after the last one locally set can be non-empty and in this
// case GetTags() would read it, which is undesirable.
(this->*f)("", pos);
return true;
}
std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *escape_chars) const
std::string Song::ParseFormat(std::string::const_iterator &it, const std::string &tag_separator, const std::string &escape_chars) const
{
std::string result;
bool has_some_tags = 0;
@@ -357,7 +333,7 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
{
while (*it == '{')
{
std::string tags = ParseFormat(it, escape_chars);
std::string tags = ParseFormat(it, tag_separator, escape_chars);
if (!tags.empty())
{
has_some_tags = 1;
@@ -386,12 +362,14 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
if (get)
{
std::string tag = GetTags(get);
if (escape_chars) // prepend format escape character to all given chars to escape
for (const char *ch = escape_chars; *ch; ++ch)
for (size_t i = 0; (i = tag.find(*ch, i)) != std::string::npos; i += 2)
tag.replace(i, 1, std::string(1, FormatEscapeCharacter) + *ch);
if (!tag.empty() && (get != &MPD::Song::GetLength || GetTotalLength()))
std::string tag = getTags(get, tag_separator);
if (!escape_chars.empty()) // prepend format escape character to all given chars to escape
{
for (size_t i = 0; i < escape_chars.length(); ++i)
for (size_t j = 0; (j = tag.find(escape_chars[i], j)) != std::string::npos; j += 2)
tag.replace(j, 1, std::string(1, FormatEscapeCharacter) + escape_chars[i]);
}
if (!tag.empty() && (get != &MPD::Song::getLength || getDuration() > 0))
{
if (delimiter)
{
@@ -420,7 +398,7 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
--brace_counter;
}
if (*++it == '|')
return ParseFormat(++it, escape_chars);
return ParseFormat(++it, tag_separator, escape_chars);
else
return "";
}
@@ -441,117 +419,4 @@ std::string MPD::Song::ParseFormat(std::string::const_iterator &it, const char *
}
}
std::string MPD::Song::toString(const std::string &format, const char *escape_chars) const
{
std::string::const_iterator it = format.begin();
return ParseFormat(it, escape_chars);
}
MPD::Song &MPD::Song::operator=(const MPD::Song &s)
{
if (this == &s)
return *this;
if (itsSong)
mpd_song_free(itsSong);
delete [] itsFile;
delete itsTags;
itsSong = s.copyPtr ? s.itsSong : (s.itsSong ? mpd_song_dup(s.itsSong) : 0);
itsFile = s.itsFile ? strdup(s.itsFile) : 0;
itsTags = s.itsTags ? new TagMap(*s.itsTags) : 0;
itsNewName = s.itsNewName;
itsSlash = s.itsSlash;
itsHash = s.itsHash;
copyPtr = s.copyPtr;
isLocalised = s.isLocalised;
return *this;
}
std::string MPD::Song::ShowTime(int length)
{
std::ostringstream ss;
int hours = length/3600;
length -= hours*3600;
int minutes = length/60;
length -= minutes*60;
int seconds = length;
if (hours > 0)
{
ss << hours << ":"
<< std::setw(2) << std::setfill('0') << minutes << ":"
<< std::setw(2) << std::setfill('0') << seconds;
}
else
{
ss << minutes << ":"
<< std::setw(2) << std::setfill('0') << seconds;
}
return ss.str();
}
bool MPD::Song::isFormatOk(const std::string &type, const std::string &s)
{
int braces = 0;
for (std::string::const_iterator it = s.begin(); it != s.end(); ++it)
{
if (*it == '{')
++braces;
else if (*it == '}')
--braces;
}
if (braces)
{
std::cerr << type << ": number of opening and closing braces does not equal!\n";
return false;
}
for (size_t i = s.find('%'); i != std::string::npos; i = s.find('%', i))
{
if (isdigit(s[++i]))
while (isdigit(s[++i])) { }
if (!toGetFunction(s[i]))
{
std::cerr << type << ": invalid character at position " << IntoStr(i+1) << ": '" << s[i] << "'\n";
return false;
}
}
return true;
}
void MPD::Song::SetHashAndSlash()
{
const char *filename = MyFilename();
if (!isStream())
{
const char *tmp = strrchr(filename, '/');
itsSlash = tmp ? tmp-filename : std::string::npos;
}
if (!itsFile)
itsHash = calc_hash(filename);
}
const char *MPD::Song::MyFilename() const
{
return itsFile ? itsFile : mpd_song_get_uri(itsSong);
}
void MPD::Song::SetTag(mpd_tag_type type, unsigned pos, const std::string &value)
{
if (!itsTags)
itsTags = new TagMap;
(*itsTags)[std::make_pair(type, pos)] = value;
}
std::string MPD::Song::GetTag(mpd_tag_type type, unsigned pos) const
{
if (itsTags)
{
TagMap::const_iterator it = itsTags->find(std::make_pair(type, pos));
if (it != itsTags->end())
return it->second;
}
const char *tag = mpd_song_get_tag(itsSong, type, pos);
return tag ? tag : "";
}