ExtM3uPlaylistPlugin.cxx 3.16 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

	bool CheckFirstLine() {
42 43
		const char *line = tis.ReadLine();
		return line != nullptr && strcmp(line, "#EXTM3U") == 0;
44 45
	}

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

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

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

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

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

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

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

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

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

	/* 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)
98
		tag.AddItem(TAG_NAME, name);
99

100
	return tag.Commit();
101 102
}

103
DetachedSong *
104
ExtM3uPlaylist::NextSong()
105
{
106
	Tag tag;
107
	char *line_s;
108 109

	do {
110 111
		line_s = tis.ReadLine();
		if (line_s == nullptr)
112
			return nullptr;
113

114 115
		StripRight(line_s);

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

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

124
	return new DetachedSong(line_s, std::move(tag));
125 126 127 128
}

static const char *const extm3u_suffixes[] = {
	"m3u",
129
	nullptr
130 131 132 133
};

static const char *const extm3u_mime_types[] = {
	"audio/x-mpegurl",
134
	nullptr
135 136 137
};

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

	nullptr,
	nullptr,
	nullptr,
	extm3u_open_stream,

	nullptr,
	extm3u_suffixes,
	extm3u_mime_types,
148
};