QueueSave.cxx 2.79 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
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "QueueSave.hxx"
22 23
#include "Queue.hxx"
#include "PlaylistError.hxx"
24
#include "DetachedSong.hxx"
25
#include "SongSave.hxx"
26 27
#include "SongLoader.hxx"
#include "playlist/PlaylistSong.hxx"
28
#include "fs/TextFile.hxx"
29
#include "util/StringUtil.hxx"
30
#include "util/Error.hxx"
31
#include "fs/Traits.hxx"
32
#include "Log.hxx"
33 34 35

#include <stdlib.h>

36 37
#define PRIO_LABEL "Prio: "

38
static void
39
queue_save_database_song(FILE *fp, int idx, const DetachedSong &song)
40
{
41
	fprintf(fp, "%i:%s\n", idx, song.GetURI());
42 43
}

44
static void
45
queue_save_full_song(FILE *fp, const DetachedSong &song)
46 47 48 49 50
{
	song_save(fp, song);
}

static void
51
queue_save_song(FILE *fp, int idx, const DetachedSong &song)
52
{
53
	if (song.IsInDatabase())
54 55 56 57 58
		queue_save_database_song(fp, idx, song);
	else
		queue_save_full_song(fp, song);
}

59
void
60
queue_save(FILE *fp, const Queue &queue)
61
{
62 63
	for (unsigned i = 0; i < queue.GetLength(); i++) {
		uint8_t prio = queue.GetPriorityAtPosition(i);
64 65 66
		if (prio != 0)
			fprintf(fp, PRIO_LABEL "%u\n", prio);

67
		queue_save_song(fp, i, queue.Get(i));
68
	}
69 70
}

71
void
72 73
queue_load_song(TextFile &file, const SongLoader &loader,
		const char *line, Queue &queue)
74
{
75
	if (queue.IsFull())
76
		return;
77

78
	uint8_t priority = 0;
79
	if (StringStartsWith(line, PRIO_LABEL)) {
80
		priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10);
81

82
		line = file.ReadLine();
83
		if (line == nullptr)
84 85 86
			return;
	}

87
	DetachedSong *song;
88

89
	if (StringStartsWith(line, SONG_BEGIN)) {
90
		const char *uri = line + sizeof(SONG_BEGIN) - 1;
91

92
		Error error;
93
		song = song_load(file, uri, error);
94
		if (song == nullptr) {
95
			LogError(error);
96 97 98 99 100 101
			return;
		}
	} else {
		char *endptr;
		long ret = strtol(line, &endptr, 10);
		if (ret < 0 || *endptr != ':' || endptr[1] == 0) {
102 103
			LogError(playlist_domain,
				 "Malformed playlist line in state file");
104 105
			return;
		}
106

107
		const char *uri = endptr + 1;
108

109 110 111 112 113 114
		song = new DetachedSong(uri);
	}

	if (!playlist_check_translate_song(*song, nullptr, loader)) {
		delete song;
		return;
115
	}
116

117 118
	queue.Append(std::move(*song), priority);
	delete song;
119
}