chiark / gitweb /
cd11a50beb874aab4651582f9b276a5801425953
[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
19 #include "common.h"
20
21 #include <errno.h>
22 #include <stddef.h>
23
24 #include "mem.h"
25 #include "queue.h"
26 #include "log.h"
27 #include "split.h"
28 #include "syscalls.h"
29 #include "table.h"
30 #include "printf.h"
31
32 const char *playing_states[] = {
33   "failed",
34   "isscratch",
35   "no_player",
36   "ok",
37   "paused",
38   "quitting",
39   "random",
40   "scratched",
41   "started",
42   "unplayed"
43 };
44
45 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
46
47 /* add new entry @n@ to a doubly linked list just after @b@ */
48 void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) {
49   n->prev = b;
50   n->next = b->next;
51   n->next->prev = n;
52   n->prev->next = n;
53 }
54
55 /* remove an entry from a doubly-linked list */
56 void queue_delete_entry(struct queue_entry *node) {
57   node->next->prev = node->prev;
58   node->prev->next = node->next;
59 }
60
61 static int unmarshall_long(char *data, struct queue_entry *q,
62                            size_t offset,
63                            void (*error_handler)(const char *, void *),
64                            void *u) {
65   if(xstrtol(&VALUE(q, offset, long), data, 0, 0)) {
66     error_handler(strerror(errno), u);
67     return -1;
68   }
69   return 0;
70 }
71
72 static const char *marshall_long(const struct queue_entry *q, size_t offset) {
73   char buffer[256];
74   int n;
75
76   n = byte_snprintf(buffer, sizeof buffer, "%ld", VALUE(q, offset, long));
77   if(n < 0)
78     fatal(errno, "error converting int");
79   else if((size_t)n >= sizeof buffer)
80     fatal(0, "long converted to decimal is too long");
81   return xstrdup(buffer);
82 }
83
84 static int unmarshall_string(char *data, struct queue_entry *q,
85                              size_t offset,
86                              void attribute((unused)) (*error_handler)(const char *, void *),
87                              void attribute((unused)) *u) {
88   VALUE(q, offset, char *) = data;
89   return 0;
90 }
91
92 static const char *marshall_string(const struct queue_entry *q, size_t offset) {
93   return VALUE(q, offset, char *);
94 }
95
96 static int unmarshall_time_t(char *data, struct queue_entry *q,
97                              size_t offset,
98                              void (*error_handler)(const char *, void *),
99                              void *u) {
100   long_long ul;
101
102   if(xstrtoll(&ul, data, 0, 0)) {
103     error_handler(strerror(errno), u);
104     return -1;
105   }
106   VALUE(q, offset, time_t) = ul;
107   return 0;
108 }
109
110 static const char *marshall_time_t(const struct queue_entry *q, size_t offset) {
111   char buffer[256];
112   int n;
113
114   n = byte_snprintf(buffer, sizeof buffer,
115                     "%"PRIdMAX, (intmax_t)VALUE(q, offset, time_t));
116   if(n < 0)
117     fatal(errno, "error converting time");
118   else if((size_t)n >= sizeof buffer)
119     fatal(0, "time converted to decimal is too long");
120   return xstrdup(buffer);
121 }
122
123 static int unmarshall_state(char *data, struct queue_entry *q,
124                             size_t offset,
125                             void (*error_handler)(const char *, void *),
126                             void *u) {
127   int n;
128
129   if((n = table_find(playing_states, 0, sizeof (char *),
130                      sizeof playing_states / sizeof *playing_states,
131                      data)) < 0) {
132     D(("state=[%s] n=%d", data, n));
133     error_handler("invalid state", u);
134     return -1;
135   }
136   VALUE(q, offset, enum playing_state) = n;
137   return 0;
138 }
139
140 static const char *marshall_state(const struct queue_entry *q, size_t offset) {
141   return playing_states[VALUE(q, offset, enum playing_state)];
142 }
143
144 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
145
146 static const struct field {
147   const char *name;
148   size_t offset;
149   const char *(*marshall)(const struct queue_entry *q, size_t offset);
150   int (*unmarshall)(char *data, struct queue_entry *q, size_t offset,
151                     void (*error_handler)(const char *, void *),
152                     void *u);
153 } fields[] = {
154   /* Keep this table sorted. */
155   F(expected, time_t),
156   F(id, string),
157   F(played, time_t),
158   F(scratched, string),
159   F(sofar, long),
160   F(state, state),
161   F(submitter, string),
162   F(track, string),
163   F(when, time_t),
164   F(wstat, long)
165 };
166
167 int queue_unmarshall(struct queue_entry *q, const char *s,
168                      void (*error_handler)(const char *, void *),
169                      void *u) {
170   char **vec;
171   int nvec;
172
173   if(!(vec = split(s, &nvec, SPLIT_QUOTES, error_handler, u)))
174     return -1;
175   return queue_unmarshall_vec(q, nvec, vec, error_handler, u);
176 }
177
178 int queue_unmarshall_vec(struct queue_entry *q, int nvec, char **vec,
179                          void (*error_handler)(const char *, void *),
180                          void *u) {
181   int n;
182
183   if(nvec % 2 != 0) {
184     error_handler("invalid marshalled queue format", u);
185     return -1;
186   }
187   while(*vec) {
188     D(("key %s value %s", vec[0], vec[1]));
189     if((n = TABLE_FIND(fields, name, *vec)) < 0) {
190       error_handler("unknown key in queue data", u);
191       return -1;
192     } else {
193       if(fields[n].unmarshall(vec[1], q, fields[n].offset, error_handler, u))
194         return -1;
195     }
196     vec += 2;
197   }
198   return 0;
199 }
200
201 char *queue_marshall(const struct queue_entry *q) {
202   unsigned n;
203   const char *vec[sizeof fields / sizeof *fields], *v;
204   char *r, *s;
205   size_t len = 1;
206
207   for(n = 0; n < sizeof fields / sizeof *fields; ++n)
208     if((v = fields[n].marshall(q, fields[n].offset))) {
209       vec[n] = quoteutf8(v);
210       len += strlen(vec[n]) + strlen(fields[n].name) + 2;
211     } else
212       vec[n] = 0;
213   s = r = xmalloc_noptr(len);
214   for(n = 0; n < sizeof fields / sizeof *fields; ++n)
215     if(vec[n]) {
216       *s++ = ' ';
217       s += strlen(strcpy(s, fields[n].name));
218       *s++ = ' ';
219       s += strlen(strcpy(s, vec[n]));
220     }
221   return r;
222 }
223
224 /*
225 Local Variables:
226 c-basic-offset:2
227 comment-column:40
228 fill-column:79
229 End:
230 */