CueParser.cxx 6.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * 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 "config.h"
#include "CueParser.hxx"
22
#include "util/StringUtil.hxx"
23
#include "util/CharUtil.hxx"
24
#include "Song.hxx"
25
#include "tag/Tag.hxx"
26 27 28 29

#include <glib.h>

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

CueParser::CueParser()
Max Kellermann's avatar
Max Kellermann committed
34
	:state(HEADER), tag(new Tag()),
35 36 37 38 39 40 41
	 current(nullptr),
	 previous(nullptr),
	 finished(nullptr),
	 end(false) {}

CueParser::~CueParser()
{
Max Kellermann's avatar
Max Kellermann committed
42
	delete tag;
43 44

	if (current != nullptr)
45
		current->Free();
46 47

	if (previous != nullptr)
48
		previous->Free();
49 50

	if (finished != nullptr)
51
		finished->Free();
52 53 54 55 56 57
}

static const char *
cue_next_word(char *p, char **pp)
{
	assert(p >= *pp);
58
	assert(!IsWhitespaceNotNull(*p));
59 60

	const char *word = p;
61
	while (!IsWhitespaceOrNull(*p))
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		++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)
{
	char *p = strchug_fast(*pp);
	if (*p == 0)
		return nullptr;

	return cue_next_word(p, pp);
}

static const char *
cue_next_value(char **pp)
{
	char *p = strchug_fast(*pp);
	if (*p == 0)
		return nullptr;

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

static void
112
cue_add_tag(Tag &tag, TagType type, char *p)
113 114 115
{
	const char *value = cue_next_value(&p);
	if (value != nullptr)
Max Kellermann's avatar
Max Kellermann committed
116
		tag.AddItem(type, value);
117 118 119 120

}

static void
Max Kellermann's avatar
Max Kellermann committed
121
cue_parse_rem(char *p, Tag &tag)
122 123 124 125 126
{
	const char *type = cue_next_token(&p);
	if (type == nullptr)
		return;

127
	TagType type2 = tag_name_parse_i(type);
128 129 130 131
	if (type2 != TAG_NUM_OF_ITEM_TYPES)
		cue_add_tag(tag, type2, p);
}

Max Kellermann's avatar
Max Kellermann committed
132
Tag *
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
CueParser::GetCurrentTag()
{
	if (state == HEADER)
		return tag;
	else if (state == TRACK)
		return current->tag;
	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
CueParser::Commit()
{
	/* the caller of this library must call cue_parser_get() often
	   enough */
	assert(finished == nullptr);
	assert(!end);

	if (current == nullptr)
		return;

	finished = previous;
	previous = current;
	current = nullptr;
}

void
CueParser::Feed2(char *p)
{
	assert(!end);
	assert(p != nullptr);

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

	if (strcmp(command, "REM") == 0) {
Max Kellermann's avatar
Max Kellermann committed
191
		Tag *current_tag = GetCurrentTag();
192
		if (current_tag != nullptr)
Max Kellermann's avatar
Max Kellermann committed
193
			cue_parse_rem(p, *current_tag);
194 195 196 197 198 199 200
	} 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." */

201
		TagType type = state == TRACK
202 203 204
			? TAG_ARTIST
			: TAG_ALBUM_ARTIST;

Max Kellermann's avatar
Max Kellermann committed
205
		Tag *current_tag = GetCurrentTag();
206
		if (current_tag != nullptr)
Max Kellermann's avatar
Max Kellermann committed
207
			cue_add_tag(*current_tag, type, p);
208 209
	} else if (strcmp(command, "TITLE") == 0) {
		if (state == HEADER)
Max Kellermann's avatar
Max Kellermann committed
210
			cue_add_tag(*tag, TAG_ALBUM, p);
211
		else if (state == TRACK)
Max Kellermann's avatar
Max Kellermann committed
212
			cue_add_tag(*current->tag, TAG_TITLE, p);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	} 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 &&
		    strcmp(type, "MP3") != 0 &&
		    strcmp(type, "AIFF") != 0) {
			state = IGNORE_FILE;
			return;
		}

		state = WAVE;
232
		filename = new_filename;
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
	} 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;
252
		current = Song::NewRemote(filename.c_str());
253
		assert(current->tag == nullptr);
Max Kellermann's avatar
Max Kellermann committed
254 255
		current->tag = new Tag(*tag);
		current->tag->AddItem(TAG_TRACK, nr);
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
		last_updated = false;
	} else if (state == IGNORE_TRACK) {
		return;
	} else if (state == TRACK && strcmp(command, "INDEX") == 0) {
		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;

		if (!last_updated && previous != nullptr &&
		    previous->start_ms < (unsigned)position_ms) {
			last_updated = true;
			previous->end_ms = position_ms;
			previous->tag->time =
				(previous->end_ms - previous->start_ms + 500) / 1000;
		}

		current->start_ms = position_ms;
	}
}

void
CueParser::Feed(const char *line)
{
	assert(!end);
	assert(line != nullptr);

	char *allocated = g_strdup(line);
	Feed2(allocated);
	g_free(allocated);
}

void
CueParser::Finish()
{
	if (end)
		/* has already been called, ignore */
		return;

	Commit();
	end = true;
}

306
Song *
307 308 309 310 311 312 313 314 315 316 317
CueParser::Get()
{
	if (finished == nullptr && end) {
		/* cue_parser_finish() has been called already:
		   deliver all remaining (partial) results */
		assert(current == nullptr);

		finished = previous;
		previous = nullptr;
	}

318
	Song *song = finished;
319 320 321
	finished = nullptr;
	return song;
}