PlsPlaylistPlugin.cxx 4.31 KB
Newer Older
Qball Cow's avatar
Qball Cow committed
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "input/InputStream.hxx"
26
#include "DetachedSong.hxx"
27
#include "tag/TagBuilder.hxx"
28 29 30
#include "util/ASCII.hxx"
#include "util/StringUtil.hxx"
#include "util/DivideString.hxx"
Qball Cow's avatar
Qball Cow committed
31

32 33
#include <string>

34
#include <stdlib.h>
35

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

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

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

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

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

59 60 61
	struct Entry {
		std::string file, title;
		int length;
62

63 64
		Entry():length(-1) {}
	};
Qball Cow's avatar
Qball Cow committed
65

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

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

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

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

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

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

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

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

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

141 142 143 144
	return true;
}

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

	return true;
154 155 156
}

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

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

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

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

const struct playlist_plugin pls_playlist_plugin = {
177
	"pls",
Qball Cow's avatar
Qball Cow committed
178

179 180 181 182
	nullptr,
	nullptr,
	nullptr,
	pls_open_stream,
Qball Cow's avatar
Qball Cow committed
183

184 185 186
	nullptr,
	pls_suffixes,
	pls_mime_types,
Qball Cow's avatar
Qball Cow committed
187
};