mpd/src/Song.cxx

106 lines
2.3 KiB
C++
Raw Normal View History

/*
2014-01-13 22:30:36 +01:00
* Copyright (C) 2003-2014 The Music Player Daemon Project
* http://www.musicpd.org
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
2013-07-28 13:25:12 +02:00
#include "Song.hxx"
2013-01-02 22:52:08 +01:00
#include "Directory.hxx"
2013-09-05 18:22:02 +02:00
#include "tag/Tag.hxx"
#include "util/Alloc.hxx"
#include "DetachedSong.hxx"
#include <assert.h>
2013-07-30 20:11:57 +02:00
#include <string.h>
#include <stdlib.h>
2013-07-28 13:25:12 +02:00
static Song *
song_alloc(const char *uri, Directory *parent)
{
2009-10-13 18:01:06 +02:00
size_t uri_length;
2009-10-13 18:01:06 +02:00
assert(uri);
uri_length = strlen(uri);
assert(uri_length);
2013-07-28 13:25:12 +02:00
Song *song = (Song *)
xalloc(sizeof(*song) - sizeof(song->uri) + uri_length + 1);
2012-08-08 20:12:20 +02:00
song->tag = nullptr;
2009-10-13 18:01:06 +02:00
memcpy(song->uri, uri, uri_length + 1);
song->parent = parent;
2009-07-06 11:32:31 +02:00
song->mtime = 0;
song->start_ms = song->end_ms = 0;
return song;
}
2013-07-28 13:25:12 +02:00
Song *
Song::NewFrom(DetachedSong &&other, Directory *parent)
{
Song *song = song_alloc(other.GetURI(), parent);
song->tag = new Tag(std::move(other.WritableTag()));
song->mtime = other.GetLastModified();
song->start_ms = other.GetStartMS();
song->end_ms = other.GetEndMS();
return song;
}
2013-07-28 13:25:12 +02:00
Song *
Song::NewFile(const char *path, Directory *parent)
{
return song_alloc(path, parent);
}
void
2013-07-28 13:25:12 +02:00
Song::Free()
{
2013-07-30 20:11:57 +02:00
delete tag;
free(this);
}
2013-10-17 01:01:15 +02:00
std::string
2013-07-28 13:25:12 +02:00
Song::GetURI() const
{
2013-07-28 13:25:12 +02:00
assert(*uri);
2008-08-26 08:27:11 +02:00
if (parent == nullptr || parent->IsRoot())
2013-10-17 01:01:15 +02:00
return std::string(uri);
else {
const char *path = parent->GetPath();
std::string result;
result.reserve(strlen(path) + 1 + strlen(uri));
result.assign(path);
result.push_back('/');
result.append(uri);
return result;
}
}
double
2013-07-28 13:25:12 +02:00
Song::GetDuration() const
{
2013-07-28 13:25:12 +02:00
if (end_ms > 0)
return (end_ms - start_ms) / 1000.0;
2013-07-28 13:25:12 +02:00
if (tag == nullptr)
return 0;
2013-07-28 13:25:12 +02:00
return tag->time - start_ms / 1000.0;
}