X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/5aff007d8fcfb4c6cc3c3627ae15f45562db7a0d..02ba7921a8b083f421a747b89c65a87d0afd5841:/lib/queue.c diff --git a/lib/queue.c b/lib/queue.c index 2b10f7a..333fe7d 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -2,27 +2,24 @@ * This file is part of DisOrder. * Copyright (C) 2004-2008 Richard Kettlewell * - * This program is free software; you can redistribute it and/or modify + * 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 + * the Free Software Foundation, either version 3 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. - * + * + * 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 + * along with this program. If not, see . */ +/** @file lib/queue.c + * @brief Track queues + */ +#include "common.h" -#include -#include "types.h" - -#include -#include #include #include @@ -34,7 +31,7 @@ #include "table.h" #include "printf.h" -const char *playing_states[] = { +const char *const playing_states[] = { "failed", "isscratch", "no_player", @@ -47,8 +44,31 @@ const char *playing_states[] = { "unplayed" }; +/** @brief String values for @c origin field */ +const char *const track_origins[] = { + "adopted", + "picked", + "random", + "scheduled", + "scratch", +}; + #define VALUE(q, offset, type) *(type *)((char *)q + offset) +/* add new entry @n@ to a doubly linked list just after @b@ */ +void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) { + n->prev = b; + n->next = b->next; + n->next->prev = n; + n->prev->next = n; +} + +/* remove an entry from a doubly-linked list */ +void queue_delete_entry(struct queue_entry *node) { + node->next->prev = node->prev; + node->prev->next = node->next; +} + static int unmarshall_long(char *data, struct queue_entry *q, size_t offset, void (*error_handler)(const char *, void *), @@ -128,10 +148,31 @@ static int unmarshall_state(char *data, struct queue_entry *q, return 0; } +static int unmarshall_origin(char *data, struct queue_entry *q, + size_t offset, + void (*error_handler)(const char *, void *), + void *u) { + int n; + + if((n = table_find(track_origins, 0, sizeof (char *), + sizeof track_origins / sizeof *track_origins, + data)) < 0) { + D(("origin=[%s] n=%d", data, n)); + error_handler("invalid origin", u); + return -1; + } + VALUE(q, offset, enum track_origin) = n; + return 0; +} + static const char *marshall_state(const struct queue_entry *q, size_t offset) { return playing_states[VALUE(q, offset, enum playing_state)]; } +static const char *marshall_origin(const struct queue_entry *q, size_t offset) { + return track_origins[VALUE(q, offset, enum track_origin)]; +} + #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h } static const struct field { @@ -145,6 +186,7 @@ static const struct field { /* Keep this table sorted. */ F(expected, time_t), F(id, string), + F(origin, origin), F(played, time_t), F(scratched, string), F(sofar, long), @@ -177,7 +219,7 @@ int queue_unmarshall_vec(struct queue_entry *q, int nvec, char **vec, } while(*vec) { D(("key %s value %s", vec[0], vec[1])); - if((n = TABLE_FIND(fields, struct field, name, *vec)) < 0) { + if((n = TABLE_FIND(fields, name, *vec)) < 0) { error_handler("unknown key in queue data", u); return -1; } else {