Song.cxx 2.48 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
21
#include "Song.hxx"
22
#include "Directory.hxx"
23
#include "tag/Tag.hxx"
24
#include "util/VarSize.hxx"
25
#include "DetachedSong.hxx"
26
#include "db/LightSong.hxx"
27

Max Kellermann's avatar
Max Kellermann committed
28
#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
29
#include <string.h>
Max Kellermann's avatar
Max Kellermann committed
30

31
inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent)
32 33
	:parent(&_parent), mtime(0),
	 start_time(SongTime::zero()), end_time(SongTime::zero())
34 35 36 37 38 39 40 41
{
	memcpy(uri, _uri, uri_length + 1);
}

inline Song::~Song()
{
}

42
static Song *
43
song_alloc(const char *uri, Directory &parent)
44
{
45
	size_t uri_length;
46

47 48 49
	assert(uri);
	uri_length = strlen(uri);
	assert(uri_length);
50

51 52 53
	return NewVarSize<Song>(sizeof(Song::uri),
				uri_length + 1,
				uri, uri_length, parent);
54
}
55

56
Song *
57
Song::NewFrom(DetachedSong &&other, Directory &parent)
58
{
59
	Song *song = song_alloc(other.GetURI(), parent);
60
	song->tag = std::move(other.WritableTag());
61
	song->mtime = other.GetLastModified();
62 63
	song->start_time = other.GetStartTime();
	song->end_time = other.GetEndTime();
64
	return song;
65 66
}

67
Song *
68
Song::NewFile(const char *path, Directory &parent)
69 70 71 72
{
	return song_alloc(path, parent);
}

73
void
74
Song::Free()
Avuton Olrich's avatar
Avuton Olrich committed
75
{
76
	DeleteVarSize(this);
77 78
}

79
std::string
80
Song::GetURI() const noexcept
Avuton Olrich's avatar
Avuton Olrich committed
81
{
82
	assert(*uri);
83

84
	if (parent->IsRoot())
85 86 87 88 89 90 91 92 93 94 95
		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;
	}
96
}
97

98
LightSong
99
Song::Export() const noexcept
100
{
101 102 103 104
	LightSong dest;
	dest.directory = parent->IsRoot()
		? nullptr : parent->GetPath();
	dest.uri = uri;
105
	dest.real_uri = nullptr;
106 107
	dest.tag = &tag;
	dest.mtime = mtime;
108 109
	dest.start_time = start_time;
	dest.end_time = end_time;
110
	return dest;
111
}