AsxPlaylistPlugin.cxx 4.05 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 "lib/expat/ExpatParser.hxx"
28
#include "Log.hxx"
29 30

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

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

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

59 60
	TagBuilder tag_builder;

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

64 65
};

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

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

		break;

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

		break;
	}
}

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

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

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

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

		break;
	}
}

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

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

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

		break;
	}
}

/*
 * The playlist object
 *
 */

144
static SongEnumerator *
145
asx_open_stream(InputStream &is)
146
{
147
	AsxParser parser;
148

149 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;
		if (!expat.Parse(is, error)) {
			LogError(error);
157
			return nullptr;
158 159 160
		}
	}

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

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

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

const struct playlist_plugin asx_playlist_plugin = {
176
	"asx",
177

178 179 180 181
	nullptr,
	nullptr,
	nullptr,
	asx_open_stream,
182

183 184 185
	nullptr,
	asx_suffixes,
	asx_mime_types,
186
};