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