2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 const char *playing_states[] = {
47 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
49 /* add new entry @n@ to a doubly linked list just after @b@ */
50 void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) {
57 /* remove an entry from a doubly-linked list */
58 void queue_delete_entry(struct queue_entry *node) {
59 node->next->prev = node->prev;
60 node->prev->next = node->next;
63 static int unmarshall_long(char *data, struct queue_entry *q,
65 void (*error_handler)(const char *, void *),
67 if(xstrtol(&VALUE(q, offset, long), data, 0, 0)) {
68 error_handler(strerror(errno), u);
74 static const char *marshall_long(const struct queue_entry *q, size_t offset) {
78 n = byte_snprintf(buffer, sizeof buffer, "%ld", VALUE(q, offset, long));
80 fatal(errno, "error converting int");
81 else if((size_t)n >= sizeof buffer)
82 fatal(0, "long converted to decimal is too long");
83 return xstrdup(buffer);
86 static int unmarshall_string(char *data, struct queue_entry *q,
88 void attribute((unused)) (*error_handler)(const char *, void *),
89 void attribute((unused)) *u) {
90 VALUE(q, offset, char *) = data;
94 static const char *marshall_string(const struct queue_entry *q, size_t offset) {
95 return VALUE(q, offset, char *);
98 static int unmarshall_time_t(char *data, struct queue_entry *q,
100 void (*error_handler)(const char *, void *),
104 if(xstrtoll(&ul, data, 0, 0)) {
105 error_handler(strerror(errno), u);
108 VALUE(q, offset, time_t) = ul;
112 static const char *marshall_time_t(const struct queue_entry *q, size_t offset) {
116 n = byte_snprintf(buffer, sizeof buffer,
117 "%"PRIdMAX, (intmax_t)VALUE(q, offset, time_t));
119 fatal(errno, "error converting time");
120 else if((size_t)n >= sizeof buffer)
121 fatal(0, "time converted to decimal is too long");
122 return xstrdup(buffer);
125 static int unmarshall_state(char *data, struct queue_entry *q,
127 void (*error_handler)(const char *, void *),
131 if((n = table_find(playing_states, 0, sizeof (char *),
132 sizeof playing_states / sizeof *playing_states,
134 D(("state=[%s] n=%d", data, n));
135 error_handler("invalid state", u);
138 VALUE(q, offset, enum playing_state) = n;
142 static const char *marshall_state(const struct queue_entry *q, size_t offset) {
143 return playing_states[VALUE(q, offset, enum playing_state)];
146 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
148 static const struct field {
151 const char *(*marshall)(const struct queue_entry *q, size_t offset);
152 int (*unmarshall)(char *data, struct queue_entry *q, size_t offset,
153 void (*error_handler)(const char *, void *),
156 /* Keep this table sorted. */
160 F(scratched, string),
163 F(submitter, string),
169 int queue_unmarshall(struct queue_entry *q, const char *s,
170 void (*error_handler)(const char *, void *),
175 if(!(vec = split(s, &nvec, SPLIT_QUOTES, error_handler, u)))
177 return queue_unmarshall_vec(q, nvec, vec, error_handler, u);
180 int queue_unmarshall_vec(struct queue_entry *q, int nvec, char **vec,
181 void (*error_handler)(const char *, void *),
186 error_handler("invalid marshalled queue format", u);
190 D(("key %s value %s", vec[0], vec[1]));
191 if((n = TABLE_FIND(fields, name, *vec)) < 0) {
192 error_handler("unknown key in queue data", u);
195 if(fields[n].unmarshall(vec[1], q, fields[n].offset, error_handler, u))
203 char *queue_marshall(const struct queue_entry *q) {
205 const char *vec[sizeof fields / sizeof *fields], *v;
209 for(n = 0; n < sizeof fields / sizeof *fields; ++n)
210 if((v = fields[n].marshall(q, fields[n].offset))) {
211 vec[n] = quoteutf8(v);
212 len += strlen(vec[n]) + strlen(fields[n].name) + 2;
215 s = r = xmalloc_noptr(len);
216 for(n = 0; n < sizeof fields / sizeof *fields; ++n)
219 s += strlen(strcpy(s, fields[n].name));
221 s += strlen(strcpy(s, vec[n]));