PlsPlaylistPlugin.cxx 4.29 KB
Newer Older
Qball Cow's avatar
Qball Cow committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
Qball Cow's avatar
Qball Cow committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
21
#include "PlsPlaylistPlugin.hxx"
22 23
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
24
#include "input/TextInputStream.hxx"
25
#include "DetachedSong.hxx"
26
#include "tag/TagBuilder.hxx"
27 28 29
#include "util/ASCII.hxx"
#include "util/StringUtil.hxx"
#include "util/DivideString.hxx"
30
#include "util/Error.hxx"
31
#include "util/Domain.hxx"
Qball Cow's avatar
Qball Cow committed
32

33 34
#include <string>

35
#include <stdlib.h>
36

37 38
static constexpr Domain pls_domain("pls");

39 40
static bool
FindPlaylistSection(TextInputStream &is)
Qball Cow's avatar
Qball Cow committed
41
{
42 43 44 45 46
	char *line;
	while ((line = is.ReadLine()) != nullptr) {
		line = Strip(line);
		if (StringEqualsCaseASCII(line, "[playlist]"))
			return true;
Qball Cow's avatar
Qball Cow committed
47 48
	}

49 50
	return false;
}
Qball Cow's avatar
Qball Cow committed
51

52 53 54 55
static bool
ParsePls(TextInputStream &is, std::forward_list<DetachedSong> &songs)
{
	assert(songs.empty());
56

57 58
	if (!FindPlaylistSection(is))
		return false;
59

60
	unsigned n_entries = 0;
Qball Cow's avatar
Qball Cow committed
61

62 63 64
	struct Entry {
		std::string file, title;
		int length;
65

66 67
		Entry():length(-1) {}
	};
Qball Cow's avatar
Qball Cow committed
68

69
	static constexpr unsigned MAX_ENTRIES = 65536;
Qball Cow's avatar
Qball Cow committed
70

71 72 73 74 75
	std::vector<Entry> entries;

	char *line;
	while ((line = is.ReadLine()) != nullptr) {
		line = Strip(line);
76

77 78 79 80 81 82
		if (*line == 0 || *line == ';')
			continue;

		if (*line == '[')
			/* another section starts; we only want
			   [Playlist], so stop here */
Qball Cow's avatar
Qball Cow committed
83
			break;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

		const DivideString ds(line, '=', true);
		if (!ds.IsDefined())
			continue;

		const char *const name = ds.GetFirst();
		const char *const value = ds.GetSecond();

		if (StringEqualsCaseASCII(name, "NumberOfEntries")) {
			n_entries = strtoul(value, nullptr, 10);
			if (n_entries == 0)
				/* empty file - nothing remains to be
				   done */
				return true;

			if (n_entries > MAX_ENTRIES)
				n_entries = MAX_ENTRIES;
			entries.resize(n_entries);
		} else if (StringEqualsCaseASCII(name, "File", 4)) {
			unsigned i = strtoul(name + 4, nullptr, 10);
			if (i >= 1 && i <= (n_entries > 0 ? n_entries : MAX_ENTRIES)) {
				if (entries.size() < i)
					entries.resize(i);
				entries[i - 1].file = value;
			}
		} else if (StringEqualsCaseASCII(name, "Title", 5)) {
			unsigned i = strtoul(name + 5, nullptr, 10);
			if (i >= 1 && i <= (n_entries > 0 ? n_entries : MAX_ENTRIES)) {
				if (entries.size() < i)
					entries.resize(i);
				entries[i - 1].title = value;
			}
		} else if (StringEqualsCaseASCII(name, "Length", 6)) {
			unsigned i = strtoul(name + 6, nullptr, 10);
			if (i >= 1 && i <= (n_entries > 0 ? n_entries : MAX_ENTRIES)) {
				if (entries.size() < i)
					entries.resize(i);
				entries[i - 1].length = atoi(value);
			}
123
		}
124
	}
125

126 127 128
	if (n_entries == 0)
		/* no "NumberOfEntries" found */
		return false;
Qball Cow's avatar
Qball Cow committed
129

130 131 132
	auto i = songs.before_begin();
	for (const auto &entry : entries) {
		const char *uri = entry.file.c_str();
Qball Cow's avatar
Qball Cow committed
133

134 135 136 137 138 139 140 141
		TagBuilder tag;
		if (!entry.title.empty())
			tag.AddItem(TAG_TITLE, entry.title.c_str());

		if (entry.length > 0)
			tag.SetDuration(SignedSongTime::FromS(entry.length));

		i = songs.emplace_after(i, uri, tag.Commit());
Qball Cow's avatar
Qball Cow committed
142 143
	}

144 145 146 147 148 149 150 151 152 153 154 155 156
	return true;
}

static bool
ParsePls(InputStream &is, std::forward_list<DetachedSong> &songs)
{
	TextInputStream tis(is);
	return ParsePls(tis, songs);
}

static SongEnumerator *
pls_open_stream(InputStream &is)
{
157
	std::forward_list<DetachedSong> songs;
158 159
	if (!ParsePls(is, songs))
		return nullptr;
Qball Cow's avatar
Qball Cow committed
160

161
	return new MemorySongEnumerator(std::move(songs));
Qball Cow's avatar
Qball Cow committed
162 163 164 165
}

static const char *const pls_suffixes[] = {
	"pls",
166
	nullptr
Qball Cow's avatar
Qball Cow committed
167 168 169 170
};

static const char *const pls_mime_types[] = {
	"audio/x-scpls",
171
	nullptr
Qball Cow's avatar
Qball Cow committed
172 173 174
};

const struct playlist_plugin pls_playlist_plugin = {
175
	"pls",
Qball Cow's avatar
Qball Cow committed
176

177 178 179 180
	nullptr,
	nullptr,
	nullptr,
	pls_open_stream,
Qball Cow's avatar
Qball Cow committed
181

182 183 184
	nullptr,
	pls_suffixes,
	pls_mime_types,
Qball Cow's avatar
Qball Cow committed
185
};