chiark / gitweb /
Doxygen-clean
[disorder] / lib / queue.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2004-2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
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 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
18
05b75f8d 19#include "common.h"
460b9539 20
460b9539 21#include <errno.h>
460b9539 22#include <stddef.h>
460b9539 23
24#include "mem.h"
25#include "queue.h"
26#include "log.h"
460b9539 27#include "split.h"
28#include "syscalls.h"
460b9539 29#include "table.h"
460b9539 30#include "printf.h"
460b9539 31
32const 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
460b9539 45#define VALUE(q, offset, type) *(type *)((char *)q + offset)
46
05b75f8d
RK
47/* add new entry @n@ to a doubly linked list just after @b@ */
48void 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 */
56void queue_delete_entry(struct queue_entry *node) {
57 node->next->prev = node->prev;
58 node->prev->next = node->next;
59}
60
460b9539 61static 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
72static 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
84static 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
92static const char *marshall_string(const struct queue_entry *q, size_t offset) {
93 return VALUE(q, offset, char *);
94}
95
96static 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
110static 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
123static 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
140static 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
146static 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
167int 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
178int 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]));
ba937f01 189 if((n = TABLE_FIND(fields, name, *vec)) < 0) {
460b9539 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
460b9539 201char *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
460b9539 224/*
225Local Variables:
226c-basic-offset:2
227comment-column:40
228fill-column:79
229End:
230*/