Song.cxx 2.43 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 26
#include "song/DetachedSong.hxx"
#include "song/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
	:parent(&_parent)
33 34 35 36 37 38 39 40
{
	memcpy(uri, _uri, uri_length + 1);
}

inline Song::~Song()
{
}

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

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

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

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

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

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

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

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

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