AsxPlaylistPlugin.cxx 4.11 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
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 "AsxPlaylistPlugin.hxx"
22 23
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
24
#include "tag/TagBuilder.hxx"
25
#include "util/ASCII.hxx"
26
#include "util/Error.hxx"
27
#include "util/StringView.hxx"
28
#include "lib/expat/ExpatParser.hxx"
29
#include "Log.hxx"
30 31

/**
32
 * This is the state object for our XML parser.
33
 */
34
struct AsxParser {
35 36 37 38
	/**
	 * The list of songs (in reverse order because that's faster
	 * while adding).
	 */
39
	std::forward_list<DetachedSong> songs;
40 41 42 43 44 45 46 47 48 49 50 51 52

	/**
	 * The current position in the XML file.
	 */
	enum {
		ROOT, ENTRY,
	} state;

	/**
	 * The current tag within the "entry" element.  This is only
	 * valid if state==ENTRY.  TAG_NUM_OF_ITEM_TYPES means there
	 * is no (known) tag.
	 */
53
	TagType tag_type;
54 55

	/**
56
	 * The current song URI.  It is set by the "ref" element.
57
	 */
58
	std::string location;
59

60 61
	TagBuilder tag_builder;

62
	AsxParser()
63
		:state(ROOT) {}
64

65 66
};

67 68 69
static void XMLCALL
asx_start_element(void *user_data, const XML_Char *element_name,
		  const XML_Char **atts)
70
{
71
	AsxParser *parser = (AsxParser *)user_data;
72 73

	switch (parser->state) {
74
	case AsxParser::ROOT:
75
		if (StringEqualsCaseASCII(element_name, "entry")) {
76
			parser->state = AsxParser::ENTRY;
77
			parser->location.clear();
78
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
79 80 81 82
		}

		break;

83
	case AsxParser::ENTRY:
84
		if (StringEqualsCaseASCII(element_name, "ref")) {
85 86
			const char *href =
				ExpatParser::GetAttributeCase(atts, "href");
87 88
			if (href != nullptr)
				parser->location = href;
89
		} else if (StringEqualsCaseASCII(element_name, "author"))
90 91
			/* is that correct?  or should it be COMPOSER
			   or PERFORMER? */
92
			parser->tag_type = TAG_ARTIST;
93
		else if (StringEqualsCaseASCII(element_name, "title"))
94
			parser->tag_type = TAG_TITLE;
95 96 97 98 99

		break;
	}
}

100 101
static void XMLCALL
asx_end_element(void *user_data, const XML_Char *element_name)
102
{
103
	AsxParser *parser = (AsxParser *)user_data;
104 105

	switch (parser->state) {
106
	case AsxParser::ROOT:
107 108
		break;

109
	case AsxParser::ENTRY:
110
		if (StringEqualsCaseASCII(element_name, "entry")) {
111 112 113
			if (!parser->location.empty())
				parser->songs.emplace_front(std::move(parser->location),
							    parser->tag_builder.Commit());
114

115
			parser->state = AsxParser::ROOT;
116
		} else
117
			parser->tag_type = TAG_NUM_OF_ITEM_TYPES;
118 119 120 121 122

		break;
	}
}

123 124
static void XMLCALL
asx_char_data(void *user_data, const XML_Char *s, int len)
125
{
126
	AsxParser *parser = (AsxParser *)user_data;
127 128

	switch (parser->state) {
129
	case AsxParser::ROOT:
130 131
		break;

132
	case AsxParser::ENTRY:
133
		if (parser->tag_type != TAG_NUM_OF_ITEM_TYPES)
134 135
			parser->tag_builder.AddItem(parser->tag_type,
						    StringView(s, len));
136 137 138 139 140 141 142 143 144 145

		break;
	}
}

/*
 * The playlist object
 *
 */

146
static SongEnumerator *
147
asx_open_stream(InputStreamPtr &&is)
148
{
149
	AsxParser parser;
150

151 152 153 154 155 156
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(asx_start_element, asx_end_element);
		expat.SetCharacterDataHandler(asx_char_data);

		Error error;
157
		if (!expat.Parse(*is, error)) {
158
			LogError(error);
159
			return nullptr;
160 161 162
		}
	}

163
	parser.songs.reverse();
164
	return new MemorySongEnumerator(std::move(parser.songs));
165 166 167 168
}

static const char *const asx_suffixes[] = {
	"asx",
169
	nullptr
170 171 172 173
};

static const char *const asx_mime_types[] = {
	"video/x-ms-asf",
174
	nullptr
175 176 177
};

const struct playlist_plugin asx_playlist_plugin = {
178
	"asx",
179

180 181 182 183
	nullptr,
	nullptr,
	nullptr,
	asx_open_stream,
184

185 186 187
	nullptr,
	asx_suffixes,
	asx_mime_types,
188
};