ExtM3uPlaylistPlugin.cxx 3.15 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 "ExtM3uPlaylistPlugin.hxx"
22 23
#include "../PlaylistPlugin.hxx"
#include "../SongEnumerator.hxx"
24
#include "DetachedSong.hxx"
25
#include "tag/Tag.hxx"
26
#include "tag/TagBuilder.hxx"
27
#include "util/StringUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "input/TextInputStream.hxx"
29

30 31 32
#include <string.h>
#include <stdlib.h>

33
class ExtM3uPlaylist final : public SongEnumerator {
34 35
	TextInputStream tis;

36
public:
37
	ExtM3uPlaylist(InputStream &is)
38 39
		:tis(is) {
	}
40 41 42 43 44 45 46

	bool CheckFirstLine() {
		std::string line;
		return tis.ReadLine(line) &&
			strcmp(line.c_str(), "#EXTM3U") == 0;
	}

47
	virtual DetachedSong *NextSong() override;
48 49
};

50
static SongEnumerator *
51
extm3u_open_stream(InputStream &is)
52
{
53
	ExtM3uPlaylist *playlist = new ExtM3uPlaylist(is);
54

55
	if (!playlist->CheckFirstLine()) {
56 57
		/* no EXTM3U header: fall back to the plain m3u
		   plugin */
58
		delete playlist;
59 60 61
		return NULL;
	}

62
	return playlist;
63 64 65 66 67 68 69
}

/**
 * Parse a EXTINF line.
 *
 * @param line the rest of the input line after the colon
 */
70
static Tag
71 72 73 74 75 76 77 78 79
extm3u_parse_tag(const char *line)
{
	long duration;
	char *endptr;
	const char *name;

	duration = strtol(line, &endptr, 10);
	if (endptr[0] != ',')
		/* malformed line */
80
		return Tag();
81 82 83 84 85

	if (duration < 0)
		/* 0 means unknown duration */
		duration = 0;

86
	name = strchug_fast(endptr + 1);
87 88 89
	if (*name == 0 && duration == 0)
		/* no information available; don't allocate a tag
		   object */
90
		return Tag();
91

92 93
	TagBuilder tag;
	tag.SetTime(duration);
94 95 96 97 98

	/* unfortunately, there is no real specification for the
	   EXTM3U format, so we must assume that the string after the
	   comma is opaque, and is just the song name*/
	if (*name != 0)
99
		tag.AddItem(TAG_NAME, name);
100

101
	return tag.Commit();
102 103
}

104
DetachedSong *
105
ExtM3uPlaylist::NextSong()
106
{
107
	Tag tag;
108 109
	std::string line;
	const char *line_s;
110 111

	do {
112
		if (!tis.ReadLine(line))
113
			return NULL;
114

115
		line_s = line.c_str();
116

117
		if (StringStartsWith(line_s, "#EXTINF:")) {
118
			tag = extm3u_parse_tag(line_s + 8);
119 120 121
			continue;
		}

122
		line_s = strchug_fast(line_s);
123
	} while (line_s[0] == '#' || *line_s == 0);
124

125
	return new DetachedSong(line_s, std::move(tag));
126 127 128 129 130 131 132 133 134 135 136 137 138
}

static const char *const extm3u_suffixes[] = {
	"m3u",
	NULL
};

static const char *const extm3u_mime_types[] = {
	"audio/x-mpegurl",
	NULL
};

const struct playlist_plugin extm3u_playlist_plugin = {
139 140 141 142 143 144 145 146 147 148
	"extm3u",

	nullptr,
	nullptr,
	nullptr,
	extm3u_open_stream,

	nullptr,
	extm3u_suffixes,
	extm3u_mime_types,
149
};