AsxPlaylistPlugin.cxx 4.01 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "AsxPlaylistPlugin.hxx"
21 22
#include "../PlaylistPlugin.hxx"
#include "../MemorySongEnumerator.hxx"
23
#include "tag/Builder.hxx"
24
#include "util/ASCII.hxx"
25
#include "util/StringView.hxx"
26
#include "lib/expat/ExpatParser.hxx"
27
#include "Log.hxx"
28 29

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

	/**
	 * 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.
	 */
51
	TagType tag_type;
52 53

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

58 59
	TagBuilder tag_builder;

60
	AsxParser()
61
		:state(ROOT) {}
62

63 64
};

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

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

		break;

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

		break;
	}
}

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

	switch (parser->state) {
104
	case AsxParser::ROOT:
105 106
		break;

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

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

		break;
	}
}

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

	switch (parser->state) {
127
	case AsxParser::ROOT:
128 129
		break;

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

		break;
	}
}

/*
 * The playlist object
 *
 */

144
static std::unique_ptr<SongEnumerator>
145
asx_open_stream(InputStreamPtr &&is)
146
{
147
	AsxParser parser;
148

149 150 151 152
	{
		ExpatParser expat(&parser);
		expat.SetElementHandler(asx_start_element, asx_end_element);
		expat.SetCharacterDataHandler(asx_char_data);
153
		expat.Parse(*is);
154 155
	}

156
	parser.songs.reverse();
157
	return std::make_unique<MemorySongEnumerator>(std::move(parser.songs));
158 159 160 161
}

static const char *const asx_suffixes[] = {
	"asx",
162
	nullptr
163 164 165 166
};

static const char *const asx_mime_types[] = {
	"video/x-ms-asf",
167
	nullptr
168 169 170
};

const struct playlist_plugin asx_playlist_plugin = {
171
	"asx",
172

173 174 175 176
	nullptr,
	nullptr,
	nullptr,
	asx_open_stream,
177

178 179 180
	nullptr,
	asx_suffixes,
	asx_mime_types,
181
};