playlist: moved saving/loading code to queue_save.c

Create a new library which saves/loads the queue to/from the state
file.
This commit is contained in:
Max Kellermann 2009-01-23 16:35:04 +01:00
parent 8afe24c1de
commit 349d249867
4 changed files with 125 additions and 26 deletions

View File

@ -95,6 +95,7 @@ mpd_headers = \
playlist_save.h \ playlist_save.h \
queue.h \ queue.h \
queue_print.h \ queue_print.h \
queue_save.h \
replay_gain.h \ replay_gain.h \
sig_handlers.h \ sig_handlers.h \
song.h \ song.h \
@ -181,6 +182,7 @@ mpd_SOURCES = \
playlist_save.c \ playlist_save.c \
queue.c \ queue.c \
queue_print.c \ queue_print.c \
queue_save.c \
replay_gain.c \ replay_gain.c \
sig_handlers.c \ sig_handlers.c \
song.c \ song.c \

View File

@ -19,6 +19,7 @@
#include "playlist.h" #include "playlist.h"
#include "playlist_save.h" #include "playlist_save.h"
#include "queue_print.h" #include "queue_print.h"
#include "queue_save.h"
#include "player_control.h" #include "player_control.h"
#include "command.h" #include "command.h"
#include "ls.h" #include "ls.h"
@ -158,16 +159,6 @@ void showPlaylist(struct client *client)
0, queue_length(&playlist.queue)); 0, queue_length(&playlist.queue));
} }
static void playlist_save(FILE *fp)
{
for (unsigned i = 0; i < queue_length(&playlist.queue); i++) {
const struct song *song = queue_get(&playlist.queue, i);
char *uri = song_get_uri(song);
fprintf(fp, "%i:%s\n", i, uri);
g_free(uri);
}
}
void savePlaylistState(FILE *fp) void savePlaylistState(FILE *fp)
{ {
fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE); fprintf(fp, "%s", PLAYLIST_STATE_FILE_STATE);
@ -195,14 +186,13 @@ void savePlaylistState(FILE *fp)
fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE, fprintf(fp, "%s%i\n", PLAYLIST_STATE_FILE_CROSSFADE,
(int)(getPlayerCrossFade())); (int)(getPlayerCrossFade()));
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN); fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_BEGIN);
playlist_save(fp); queue_save(fp, &playlist.queue);
fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END); fprintf(fp, "%s\n", PLAYLIST_STATE_FILE_PLAYLIST_END);
} }
static void loadPlaylistFromStateFile(FILE *fp, char *buffer, static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
int state, int current, int seek_time) int state, int current, int seek_time)
{ {
char *temp;
int song; int song;
if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) { if (!fgets(buffer, PLAYLIST_BUFFER_SIZE, fp)) {
@ -213,20 +203,8 @@ static void loadPlaylistFromStateFile(FILE *fp, char *buffer,
while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) { while (!g_str_has_prefix(buffer, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
g_strchomp(buffer); g_strchomp(buffer);
temp = strtok(buffer, ":"); song = queue_load_song(&playlist.queue, buffer);
if (temp == NULL) { if (song >= 0 && song == current) {
g_warning("Malformed playlist line in state file");
break;
}
song = atoi(temp);
if (!(temp = strtok(NULL, ""))) {
g_warning("Malformed playlist line in state file");
break;
}
if (addToPlaylist(temp, NULL) == PLAYLIST_RESULT_SUCCESS
&& current == song) {
if (state != PLAYER_STATE_STOP) { if (state != PLAYER_STATE_STOP) {
playPlaylist(queue_length(&playlist.queue) - 1); playPlaylist(queue_length(&playlist.queue) - 1);
} }

78
src/queue_save.c Normal file
View File

@ -0,0 +1,78 @@
/*
* Copyright (C) 2003-2009 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "queue_save.h"
#include "queue.h"
#include "song.h"
#include "ls.h"
#include "database.h"
#include <stdlib.h>
void
queue_save(FILE *fp, const struct queue *queue)
{
for (unsigned i = 0; i < queue_length(queue); i++) {
const struct song *song = queue_get(queue, i);
char *uri = song_get_uri(song);
fprintf(fp, "%i:%s\n", i, uri);
g_free(uri);
}
}
static struct song *
get_song(const char *uri)
{
struct song *song;
song = db_get_song(uri);
if (song != NULL)
return song;
if (uri_has_scheme(uri))
return song_remote_new(uri);
return NULL;
}
int
queue_load_song(struct queue *queue, const char *line)
{
long ret;
char *endptr;
struct song *song;
if (queue_is_full(queue))
return -1;
ret = strtol(line, &endptr, 10);
if (ret < 0 || *endptr != ':' || endptr[1] == 0) {
g_warning("Malformed playlist line in state file");
return -1;
}
line = endptr + 1;
song = get_song(line);
if (song == NULL)
return -1;
queue_append(queue, song);
return ret;
}

41
src/queue_save.h Normal file
View File

@ -0,0 +1,41 @@
/*
* Copyright (C) 2003-2009 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This library saves the queue into the state file, and also loads it
* back into memory.
*/
#ifndef QUEUE_SAVE_H
#define QUEUE_SAVE_H
#include <stdio.h>
struct queue;
void
queue_save(FILE *fp, const struct queue *queue);
/**
* Loads one song from the state file line and returns its number.
* Returns -1 on failure.
*/
int
queue_load_song(struct queue *queue, const char *line);
#endif