chiark / gitweb /
Do some basic compatibility checking when installing a new server
[disorder] / lib / queue.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 Richard Kettlewell
4  *
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 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/queue.c
19  * @brief Track queues
20  */
21 #include "common.h"
22
23 #include <errno.h>
24 #include <stddef.h>
25
26 #include "mem.h"
27 #include "queue.h"
28 #include "log.h"
29 #include "split.h"
30 #include "syscalls.h"
31 #include "table.h"
32 #include "printf.h"
33
34 const char *const playing_states[] = {
35   "failed",
36   "isscratch",
37   "no_player",
38   "ok",
39   "paused",
40   "quitting",
41   "random",
42   "scratched",
43   "started",
44   "unplayed"
45 };
46
47 /** @brief String values for @c origin field */
48 const char *const track_origins[] = {
49   "adopted",
50   "picked",
51   "random",
52   "scheduled",
53   "scratch",
54 };
55
56 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
57
58 /* add new entry @n@ to a doubly linked list just after @b@ */
59 void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) {
60   n->prev = b;
61   n->next = b->next;
62   n->next->prev = n;
63   n->prev->next = n;
64 }
65
66 /* remove an entry from a doubly-linked list */
67 void queue_delete_entry(struct queue_entry *node) {
68   node->next->prev = node->prev;
69   node->prev->next = node->next;
70 }
71
72 static int unmarshall_long(char *data, struct queue_entry *q,
73                            size_t offset,
74                            void (*error_handler)(const char *, void *),
75                            void *u) {
76   if(xstrtol(&VALUE(q, offset, long), data, 0, 0)) {
77     error_handler(strerror(errno), u);
78     return -1;
79   }
80   return 0;
81 }
82
83 static const char *marshall_long(const struct queue_entry *q, size_t offset) {
84   char buffer[256];
85   int n;
86
87   n = byte_snprintf(buffer, sizeof buffer, "%ld", VALUE(q, offset, long));
88   if(n < 0)
89     fatal(errno, "error converting int");
90   else if((size_t)n >= sizeof buffer)
91     fatal(0, "long converted to decimal is too long");
92   return xstrdup(buffer);
93 }
94
95 static int unmarshall_string(char *data, struct queue_entry *q,
96                              size_t offset,
97                              void attribute((unused)) (*error_handler)(const char *, void *),
98                              void attribute((unused)) *u) {
99   VALUE(q, offset, char *) = data;
100   return 0;
101 }
102
103 static const char *marshall_string(const struct queue_entry *q, size_t offset) {
104   return VALUE(q, offset, char *);
105 }
106
107 static int unmarshall_time_t(char *data, struct queue_entry *q,
108                              size_t offset,
109                              void (*error_handler)(const char *, void *),
110                              void *u) {
111   long_long ul;
112
113   if(xstrtoll(&ul, data, 0, 0)) {
114     error_handler(strerror(errno), u);
115     return -1;
116   }
117   VALUE(q, offset, time_t) = ul;
118   return 0;
119 }
120
121 static const char *marshall_time_t(const struct queue_entry *q, size_t offset) {
122   char buffer[256];
123   int n;
124
125   n = byte_snprintf(buffer, sizeof buffer,
126                     "%"PRIdMAX, (intmax_t)VALUE(q, offset, time_t));
127   if(n < 0)
128     fatal(errno, "error converting time");
129   else if((size_t)n >= sizeof buffer)
130     fatal(0, "time converted to decimal is too long");
131   return xstrdup(buffer);
132 }
133
134 static int unmarshall_state(char *data, struct queue_entry *q,
135                             size_t offset,
136                             void (*error_handler)(const char *, void *),
137                             void *u) {
138   int n;
139
140   if((n = table_find(playing_states, 0, sizeof (char *),
141                      sizeof playing_states / sizeof *playing_states,
142                      data)) < 0) {
143     D(("state=[%s] n=%d", data, n));
144     error_handler("invalid state", u);
145     return -1;
146   }
147   VALUE(q, offset, enum playing_state) = n;
148   return 0;
149 }
150
151 static int unmarshall_origin(char *data, struct queue_entry *q,
152                              size_t offset,
153                              void (*error_handler)(const char *, void *),
154                              void *u) {
155   int n;
156
157   if((n = table_find(track_origins, 0, sizeof (char *),
158                      sizeof track_origins / sizeof *track_origins,
159                      data)) < 0) {
160     D(("origin=[%s] n=%d", data, n));
161     error_handler("invalid origin", u);
162     return -1;
163   }
164   VALUE(q, offset, enum track_origin) = n;
165   return 0;
166 }
167
168 static const char *marshall_state(const struct queue_entry *q, size_t offset) {
169   return playing_states[VALUE(q, offset, enum playing_state)];
170 }
171
172 static const char *marshall_origin(const struct queue_entry *q, size_t offset) {
173   return track_origins[VALUE(q, offset, enum track_origin)];
174 }
175
176 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
177
178 static const struct field {
179   const char *name;
180   size_t offset;
181   const char *(*marshall)(const struct queue_entry *q, size_t offset);
182   int (*unmarshall)(char *data, struct queue_entry *q, size_t offset,
183                     void (*error_handler)(const char *, void *),
184                     void *u);
185 } fields[] = {
186   /* Keep this table sorted. */
187   F(expected, time_t),
188   F(id, string),
189   F(origin, origin),
190   F(played, time_t),
191   F(scratched, string),
192   F(sofar, long),
193   F(state, state),
194   F(submitter, string),
195   F(track, string),
196   F(when, time_t),
197   F(wstat, long)
198 };
199
200 int queue_unmarshall(struct queue_entry *q, const char *s,
201                      void (*error_handler)(const char *, void *),
202                      void *u) {
203   char **vec;
204   int nvec;
205
206   if(!(vec = split(s, &nvec, SPLIT_QUOTES, error_handler, u)))
207     return -1;
208   return queue_unmarshall_vec(q, nvec, vec, error_handler, u);
209 }
210
211 int queue_unmarshall_vec(struct queue_entry *q, int nvec, char **vec,
212                          void (*error_handler)(const char *, void *),
213                          void *u) {
214   int n;
215
216   if(nvec % 2 != 0) {
217     error_handler("invalid marshalled queue format", u);
218     return -1;
219   }
220   while(*vec) {
221     D(("key %s value %s", vec[0], vec[1]));
222     if((n = TABLE_FIND(fields, name, *vec)) < 0) {
223       error_handler("unknown key in queue data", u);
224       return -1;
225     } else {
226       if(fields[n].unmarshall(vec[1], q, fields[n].offset, error_handler, u))
227         return -1;
228     }
229     vec += 2;
230   }
231   return 0;
232 }
233
234 char *queue_marshall(const struct queue_entry *q) {
235   unsigned n;
236   const char *vec[sizeof fields / sizeof *fields], *v;
237   char *r, *s;
238   size_t len = 1;
239
240   for(n = 0; n < sizeof fields / sizeof *fields; ++n)
241     if((v = fields[n].marshall(q, fields[n].offset))) {
242       vec[n] = quoteutf8(v);
243       len += strlen(vec[n]) + strlen(fields[n].name) + 2;
244     } else
245       vec[n] = 0;
246   s = r = xmalloc_noptr(len);
247   for(n = 0; n < sizeof fields / sizeof *fields; ++n)
248     if(vec[n]) {
249       *s++ = ' ';
250       s += strlen(strcpy(s, fields[n].name));
251       *s++ = ' ';
252       s += strlen(strcpy(s, vec[n]));
253     }
254   return r;
255 }
256
257 /*
258 Local Variables:
259 c-basic-offset:2
260 comment-column:40
261 fill-column:79
262 End:
263 */