CueParser.cxx 6.29 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 20
 * 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.
 */

#include "CueParser.hxx"
21
#include "tag/ParseName.hxx"
22
#include "util/Alloc.hxx"
23
#include "util/StringStrip.hxx"
24
#include "util/CharUtil.hxx"
25 26

#include <assert.h>
Max Kellermann's avatar
Max Kellermann committed
27
#include <string.h>
28 29 30 31 32 33
#include <stdlib.h>

static const char *
cue_next_word(char *p, char **pp)
{
	assert(p >= *pp);
34
	assert(!IsWhitespaceNotNull(*p));
35 36

	const char *word = p;
37
	while (!IsWhitespaceOrNull(*p))
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
		++p;

	*p = 0;
	*pp = p + 1;
	return word;
}

static const char *
cue_next_quoted(char *p, char **pp)
{
	assert(p >= *pp);
	assert(p[-1] == '"');

	char *end = strchr(p, '"');
	if (end == nullptr) {
		/* syntax error - ignore it silently */
		*pp = p + strlen(p);
		return p;
	}

	*end = 0;
	*pp = end + 1;

	return p;
}

static const char *
cue_next_token(char **pp)
{
67
	char *p = StripLeft(*pp);
68 69 70 71 72 73 74 75 76
	if (*p == 0)
		return nullptr;

	return cue_next_word(p, pp);
}

static const char *
cue_next_value(char **pp)
{
77
	char *p = StripLeft(*pp);
78 79 80 81 82 83 84 85 86 87
	if (*p == 0)
		return nullptr;

	if (*p == '"')
		return cue_next_quoted(p + 1, pp);
	else
		return cue_next_word(p, pp);
}

static void
88
cue_add_tag(TagBuilder &tag, TagType type, char *p)
89 90 91
{
	const char *value = cue_next_value(&p);
	if (value != nullptr)
Max Kellermann's avatar
Max Kellermann committed
92
		tag.AddItem(type, value);
93 94 95 96

}

static void
97
cue_parse_rem(char *p, TagBuilder &tag)
98 99 100 101 102
{
	const char *type = cue_next_token(&p);
	if (type == nullptr)
		return;

103
	TagType type2 = tag_name_parse_i(type);
104 105 106 107
	if (type2 != TAG_NUM_OF_ITEM_TYPES)
		cue_add_tag(tag, type2, p);
}

108
TagBuilder *
109
CueParser::GetCurrentTag() noexcept
110 111
{
	if (state == HEADER)
112
		return &header_tag;
113
	else if (state == TRACK)
114
		return &song_tag;
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	else
		return nullptr;
}

static int
cue_parse_position(const char *p)
{
	char *endptr;
	unsigned long minutes = strtoul(p, &endptr, 10);
	if (endptr == p || *endptr != ':')
		return -1;

	p = endptr + 1;
	unsigned long seconds = strtoul(p, &endptr, 10);
	if (endptr == p || *endptr != ':')
		return -1;

	p = endptr + 1;
	unsigned long frames = strtoul(p, &endptr, 10);
	if (endptr == p || *endptr != 0)
		return -1;

	return minutes * 60000 + seconds * 1000 + frames * 1000 / 75;
}

void
141
CueParser::Commit() noexcept
142 143 144 145 146 147 148 149 150
{
	/* the caller of this library must call cue_parser_get() often
	   enough */
	assert(finished == nullptr);
	assert(!end);

	if (current == nullptr)
		return;

151 152
	assert(!current->GetTag().IsDefined());
	current->SetTag(song_tag.Commit());
153

154 155 156
	finished = std::move(previous);
	previous = std::move(current);
	current.reset();
157 158 159
}

void
160
CueParser::Feed2(char *p) noexcept
161 162 163 164 165 166 167 168 169
{
	assert(!end);
	assert(p != nullptr);

	const char *command = cue_next_token(&p);
	if (command == nullptr)
		return;

	if (strcmp(command, "REM") == 0) {
170
		TagBuilder *tag = GetCurrentTag();
171 172
		if (tag != nullptr)
			cue_parse_rem(p, *tag);
173 174 175 176 177 178 179
	} else if (strcmp(command, "PERFORMER") == 0) {
		/* MPD knows a "performer" tag, but it is not a good
		   match for this CUE tag; from the Hydrogenaudio
		   Knowledgebase: "At top-level this will specify the
		   CD artist, while at track-level it specifies the
		   track artist." */

180
		TagType type = state == TRACK
181 182 183
			? TAG_ARTIST
			: TAG_ALBUM_ARTIST;

184
		TagBuilder *tag = GetCurrentTag();
185 186
		if (tag != nullptr)
			cue_add_tag(*tag, type, p);
187 188
	} else if (strcmp(command, "TITLE") == 0) {
		if (state == HEADER)
189
			cue_add_tag(header_tag, TAG_ALBUM, p);
190
		else if (state == TRACK)
191
			cue_add_tag(song_tag, TAG_TITLE, p);
192 193 194 195 196 197 198 199 200 201 202 203
	} else if (strcmp(command, "FILE") == 0) {
		Commit();

		const char *new_filename = cue_next_value(&p);
		if (new_filename == nullptr)
			return;

		const char *type = cue_next_token(&p);
		if (type == nullptr)
			return;

		if (strcmp(type, "WAVE") != 0 &&
204
		    strcmp(type, "FLAC") != 0 && /* non-standard */
205 206 207 208 209 210 211
		    strcmp(type, "MP3") != 0 &&
		    strcmp(type, "AIFF") != 0) {
			state = IGNORE_FILE;
			return;
		}

		state = WAVE;
212
		filename = new_filename;
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	} else if (state == IGNORE_FILE) {
		return;
	} else if (strcmp(command, "TRACK") == 0) {
		Commit();

		const char *nr = cue_next_token(&p);
		if (nr == nullptr)
			return;

		const char *type = cue_next_token(&p);
		if (type == nullptr)
			return;

		if (strcmp(type, "AUDIO") != 0) {
			state = IGNORE_TRACK;
			return;
		}

		state = TRACK;
232
		ignore_index = false;
233
		current = std::make_unique<DetachedSong>(filename);
234
		assert(!current->GetTag().IsDefined());
235 236 237 238

		song_tag = header_tag;
		song_tag.AddItem(TAG_TRACK, nr);

239 240 241
	} else if (state == IGNORE_TRACK) {
		return;
	} else if (state == TRACK && strcmp(command, "INDEX") == 0) {
242 243 244
		if (ignore_index)
			return;

245 246 247 248 249 250 251 252 253 254 255 256
		const char *nr = cue_next_token(&p);
		if (nr == nullptr)
			return;

		const char *position = cue_next_token(&p);
		if (position == nullptr)
			return;

		int position_ms = cue_parse_position(position);
		if (position_ms < 0)
			return;

257
		if (previous != nullptr && previous->GetStartTime().ToMS() < (unsigned)position_ms)
258
			previous->SetEndTime(SongTime::FromMS(position_ms));
259

260
		current->SetStartTime(SongTime::FromMS(position_ms));
261
		if(strcmp(nr, "00") != 0 || previous == nullptr)
262
			ignore_index = true;
263 264 265 266
	}
}

void
267
CueParser::Feed(const char *line) noexcept
268 269 270 271
{
	assert(!end);
	assert(line != nullptr);

272
	char *allocated = xstrdup(line);
273
	Feed2(allocated);
274
	free(allocated);
275 276 277
}

void
278
CueParser::Finish() noexcept
279 280 281 282 283 284 285 286 287
{
	if (end)
		/* has already been called, ignore */
		return;

	Commit();
	end = true;
}

288
std::unique_ptr<DetachedSong>
289
CueParser::Get() noexcept
290 291 292 293 294 295
{
	if (finished == nullptr && end) {
		/* cue_parser_finish() has been called already:
		   deliver all remaining (partial) results */
		assert(current == nullptr);

296 297
		finished = std::move(previous);
		previous.reset();
298 299
	}

300 301 302
	auto result = std::move(finished);
	finished.reset();
	return result;
303
}