PlsPlaylistPlugin.cxx 4.32 KB
Newer Older
Qball Cow's avatar
Qball Cow committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 "PlsPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
23
#include "input/TextInputStream.hxx"
24
#include "input/InputStream.hxx"
25
#include "song/DetachedSong.hxx"
26
#include "tag/Builder.hxx"
27
#include "util/ASCII.hxx"
28
#include "util/StringStrip.hxx"
29
#include "util/DivideString.hxx"
Qball Cow's avatar
Qball Cow committed
30

31 32
#include <string>

33
#include <stdlib.h>
34

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

45 46
	return false;
}
Qball Cow's avatar
Qball Cow committed
47

48 49 50 51
static bool
ParsePls(TextInputStream &is, std::forward_list<DetachedSong> &songs)
{
	assert(songs.empty());
52

53 54
	if (!FindPlaylistSection(is))
		return false;
55

56
	unsigned n_entries = 0;
Qball Cow's avatar
Qball Cow committed
57

58 59
	struct Entry {
		std::string file, title;
60
		int length{-1};
61

62
		Entry() = default;
63
	};
Qball Cow's avatar
Qball Cow committed
64

65
	static constexpr unsigned MAX_ENTRIES = 65536;
Qball Cow's avatar
Qball Cow committed
66

67 68 69 70 71
	std::vector<Entry> entries;

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

73 74 75 76 77 78
		if (*line == 0 || *line == ';')
			continue;

		if (*line == '[')
			/* another section starts; we only want
			   [Playlist], so stop here */
Qball Cow's avatar
Qball Cow committed
79
			break;
80 81 82 83 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

		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);
			}
119
		}
120
	}
121

122 123 124
	if (n_entries == 0)
		/* no "NumberOfEntries" found */
		return false;
Qball Cow's avatar
Qball Cow committed
125

126 127 128
	auto i = songs.before_begin();
	for (const auto &entry : entries) {
		const char *uri = entry.file.c_str();
Qball Cow's avatar
Qball Cow committed
129

130 131 132 133 134 135 136 137
		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
138 139
	}

140 141 142 143
	return true;
}

static bool
144
ParsePls(InputStreamPtr &&is, std::forward_list<DetachedSong> &songs)
145
{
146 147 148 149 150 151 152
	TextInputStream tis(std::move(is));
	if (!ParsePls(tis, songs)) {
		is = tis.StealInputStream();
		return false;
	}

	return true;
153 154
}

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

162
	return std::make_unique<MemorySongEnumerator>(std::move(songs));
Qball Cow's avatar
Qball Cow committed
163 164
}

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

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

175 176 177 178
const PlaylistPlugin pls_playlist_plugin =
	PlaylistPlugin("pls", pls_open_stream)
	.WithSuffixes(pls_suffixes)
	.WithMimeTypes(pls_mime_types);