EmbeddedCuePlaylistPlugin.cxx 4.05 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 20 21 22 23 24 25 26
 * 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.
 */

/** \file
 *
 * Playlist plugin that reads embedded cue sheets from the "CUESHEET"
 * tag of a music file.
 */

#include "config.h"
27
#include "EmbeddedCuePlaylistPlugin.hxx"
28 29
#include "../PlaylistPlugin.hxx"
#include "../SongEnumerator.hxx"
30
#include "../cue/CueParser.hxx"
31
#include "tag/TagHandler.hxx"
32 33
#include "tag/TagId3.hxx"
#include "tag/ApeTag.hxx"
34
#include "DetachedSong.hxx"
35
#include "TagFile.hxx"
36
#include "fs/Traits.hxx"
37
#include "fs/AllocatedPath.hxx"
38
#include "util/ASCII.hxx"
39

40 41
#include <string.h>

42 43
class EmbeddedCuePlaylist final : public SongEnumerator {
public:
44 45 46 47 48
	/**
	 * This is an override for the CUE's "FILE".  An embedded CUE
	 * sheet must always point to the song file it is contained
	 * in.
	 */
Max Kellermann's avatar
Max Kellermann committed
49
	std::string filename;
50

51 52 53
	/**
	 * The value of the file's "CUESHEET" tag.
	 */
54
	std::string cuesheet;
55 56 57 58 59 60

	/**
	 * The offset of the next line within "cuesheet".
	 */
	char *next;

61
	CueParser *parser;
62

63
public:
64
	EmbeddedCuePlaylist()
65
		:parser(nullptr) {
66 67
	}

68
	virtual ~EmbeddedCuePlaylist() {
69 70 71
		delete parser;
	}

72
	virtual DetachedSong *NextSong() override;
73 74 75 76 77
};

static void
embcue_tag_pair(const char *name, const char *value, void *ctx)
{
78
	EmbeddedCuePlaylist *playlist = (EmbeddedCuePlaylist *)ctx;
79

80
	if (playlist->cuesheet.empty() &&
81
	    StringEqualsCaseASCII(name, "cuesheet"))
82
		playlist->cuesheet = value;
83 84 85
}

static const struct tag_handler embcue_tag_handler = {
86 87 88
	nullptr,
	nullptr,
	embcue_tag_pair,
89 90
};

91
static SongEnumerator *
92
embcue_playlist_open_uri(const char *uri,
93 94
			 gcc_unused Mutex &mutex,
			 gcc_unused Cond &cond)
95
{
96
	if (!PathTraitsUTF8::IsAbsolute(uri))
97
		/* only local files supported */
98
		return nullptr;
99

100 101 102 103
	const auto path_fs = AllocatedPath::FromUTF8(uri);
	if (path_fs.IsNull())
		return nullptr;

104
	const auto playlist = new EmbeddedCuePlaylist();
105

106
	tag_file_scan(path_fs, embcue_tag_handler, playlist);
107
	if (playlist->cuesheet.empty()) {
108
		tag_ape_scan2(path_fs, &embcue_tag_handler, playlist);
109
		if (playlist->cuesheet.empty())
110
			tag_id3_scan(path_fs, &embcue_tag_handler, playlist);
111 112
	}

113
	if (playlist->cuesheet.empty()) {
114
		/* no "CUESHEET" tag found */
115
		delete playlist;
116
		return nullptr;
117 118
	}

119
	playlist->filename = PathTraitsUTF8::GetBase(uri);
120

121
	playlist->next = &playlist->cuesheet[0];
122
	playlist->parser = new CueParser();
123

124
	return playlist;
125 126
}

127
DetachedSong *
128
EmbeddedCuePlaylist::NextSong()
129
{
130
	DetachedSong *song = parser->Get();
131
	if (song != nullptr)
132 133
		return song;

134 135 136
	while (*next != 0) {
		const char *line = next;
		char *eol = strpbrk(next, "\r\n");
137
		if (eol != nullptr) {
138 139
			/* null-terminate the line */
			*eol = 0;
140
			next = eol + 1;
141 142 143
		} else
			/* last line; put the "next" pointer to the
			   end of the buffer */
144
			next += strlen(line);
145

146 147
		parser->Feed(line);
		song = parser->Get();
148 149 150 151
		if (song != nullptr) {
			song->SetURI(filename);
			return song;
		}
152 153
	}

154 155
	parser->Finish();
	song = parser->Get();
156
	if (song != nullptr)
157
		song->SetURI(filename);
158
	return song;
159 160 161 162 163 164 165 166 167 168 169
}

static const char *const embcue_playlist_suffixes[] = {
	/* a few codecs that are known to be supported; there are
	   probably many more */
	"flac",
	"mp3", "mp2",
	"mp4", "mp4a", "m4b",
	"ape",
	"wv",
	"ogg", "oga",
170
	nullptr
171 172 173
};

const struct playlist_plugin embcue_playlist_plugin = {
174
	"embcue",
175 176 177 178 179 180 181

	nullptr,
	nullptr,
	embcue_playlist_open_uri,
	nullptr,

	nullptr,
182
	embcue_playlist_suffixes,
183
	nullptr,
184
};