Song.cxx 2.41 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "Song.hxx"
21
#include "Directory.hxx"
22
#include "tag/Tag.hxx"
23
#include "util/VarSize.hxx"
24 25
#include "song/DetachedSong.hxx"
#include "song/LightSong.hxx"
26

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

30
inline Song::Song(const char *_uri, size_t uri_length, Directory &_parent)
31
	:parent(&_parent)
32 33 34 35 36 37 38 39
{
	memcpy(uri, _uri, uri_length + 1);
}

inline Song::~Song()
{
}

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

45 46 47
	assert(uri);
	uri_length = strlen(uri);
	assert(uri_length);
48

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

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

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

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

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

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

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