Song.cxx 2.49 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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>
30
#include <stdlib.h>
Max Kellermann's avatar
Max Kellermann committed
31

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

inline Song::~Song()
{
}

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

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

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

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

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

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

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

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

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