2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009, 2011, 2013 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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
35 const char *const playing_states[] = {
48 /** @brief String values for @c origin field */
49 const char *const track_origins[] = {
57 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
59 /** @brief Insert queue entry @p n just after @p b
60 * @param b Insert after this entry
61 * @param n New entry to insert
63 void queue_insert_entry(struct queue_entry *b, struct queue_entry *n) {
70 /* remove an entry from a doubly-linked list */
71 void queue_delete_entry(struct queue_entry *node) {
72 node->next->prev = node->prev;
73 node->prev->next = node->next;
76 static int unmarshall_long(char *data, struct queue_entry *q,
78 void (*error_handler)(const char *, void *),
81 if(xstrtol(&VALUE(q, offset, long), data, 0, 0)) {
82 error_handler(format_error(ec_errno, errno, errbuf, sizeof errbuf), u);
88 static const char *marshall_long(const struct queue_entry *q, size_t offset) {
92 n = byte_snprintf(buffer, sizeof buffer, "%ld", VALUE(q, offset, long));
94 disorder_fatal(errno, "error converting int");
95 else if((size_t)n >= sizeof buffer)
96 disorder_fatal(0, "long converted to decimal is too long");
97 return xstrdup(buffer);
100 static void free_none(struct queue_entry attribute((unused)) *q,
101 size_t attribute((unused)) offset) {
104 #define free_long free_none
106 static int unmarshall_string(char *data, struct queue_entry *q,
108 void attribute((unused)) (*error_handler)(const char *, void *),
109 void attribute((unused)) *u) {
110 VALUE(q, offset, char *) = xstrdup(data);
114 static const char *marshall_string(const struct queue_entry *q, size_t offset) {
115 return VALUE(q, offset, char *);
118 static void free_string(struct queue_entry *q, size_t offset) {
119 xfree(VALUE(q, offset, char *));
122 static int unmarshall_time_t(char *data, struct queue_entry *q,
124 void (*error_handler)(const char *, void *),
129 if(xstrtoll(&ul, data, 0, 0)) {
130 error_handler(format_error(ec_errno, errno, errbuf, sizeof errbuf), u);
133 VALUE(q, offset, time_t) = ul;
137 static const char *marshall_time_t(const struct queue_entry *q, size_t offset) {
141 n = byte_snprintf(buffer, sizeof buffer,
142 "%"PRIdMAX, (intmax_t)VALUE(q, offset, time_t));
144 disorder_fatal(errno, "error converting time");
145 else if((size_t)n >= sizeof buffer)
146 disorder_fatal(0, "time converted to decimal is too long");
147 return xstrdup(buffer);
150 #define free_time_t free_none
152 static int unmarshall_state(char *data, struct queue_entry *q,
154 void (*error_handler)(const char *, void *),
158 if((n = table_find(playing_states, 0, sizeof (char *),
159 sizeof playing_states / sizeof *playing_states,
161 D(("state=[%s] n=%d", data, n));
162 error_handler("invalid state", u);
165 VALUE(q, offset, enum playing_state) = n;
169 static int unmarshall_origin(char *data, struct queue_entry *q,
171 void (*error_handler)(const char *, void *),
175 if((n = table_find(track_origins, 0, sizeof (char *),
176 sizeof track_origins / sizeof *track_origins,
178 D(("origin=[%s] n=%d", data, n));
179 error_handler("invalid origin", u);
182 VALUE(q, offset, enum track_origin) = n;
186 static const char *marshall_state(const struct queue_entry *q, size_t offset) {
187 return playing_states[VALUE(q, offset, enum playing_state)];
190 static const char *marshall_origin(const struct queue_entry *q, size_t offset) {
191 return track_origins[VALUE(q, offset, enum track_origin)];
194 #define free_state free_none
195 #define free_origin free_none
197 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h, free_##h }
199 /** @brief A field in a @ref queue_entry */
200 static const struct queue_field {
201 /** @brief Field name */
204 /** @brief Offset of value in @ref queue_entry structure */
207 /** @brief Marshaling function */
208 const char *(*marshall)(const struct queue_entry *q, size_t offset);
210 /** @brief Unmarshaling function */
211 int (*unmarshall)(char *data, struct queue_entry *q, size_t offset,
212 void (*error_handler)(const char *, void *),
215 /** @brief Destructor */
216 void (*free)(struct queue_entry *q, size_t offset);
218 /* Keep this table sorted. */
223 F(scratched, string),
226 F(submitter, string),
231 #define NFIELDS (sizeof fields / sizeof *fields)
233 int queue_unmarshall(struct queue_entry *q, const char *s,
234 void (*error_handler)(const char *, void *),
239 q->pid = -1; /* =none */
240 if(!(vec = split(s, &nvec, SPLIT_QUOTES, error_handler, u)))
242 rc = queue_unmarshall_vec(q, nvec, vec, error_handler, u);
243 free_strings(nvec, vec);
247 int queue_unmarshall_vec(struct queue_entry *q, int nvec, char **vec,
248 void (*error_handler)(const char *, void *),
253 error_handler("invalid marshalled queue format", u);
257 D(("key %s value %s", vec[0], vec[1]));
258 if((n = TABLE_FIND(fields, name, *vec)) < 0) {
259 error_handler("unknown key in queue data", u);
262 if(fields[n].unmarshall(vec[1], q, fields[n].offset, error_handler, u))
270 char *queue_marshall(const struct queue_entry *q) {
272 const char *vec[sizeof fields / sizeof *fields], *v;
276 for(n = 0; n < sizeof fields / sizeof *fields; ++n)
277 if((v = fields[n].marshall(q, fields[n].offset))) {
278 vec[n] = quoteutf8(v);
279 len += strlen(vec[n]) + strlen(fields[n].name) + 2;
282 s = r = xmalloc_noptr(len);
283 for(n = 0; n < sizeof fields / sizeof *fields; ++n)
286 s += strlen(strcpy(s, fields[n].name));
288 s += strlen(strcpy(s, vec[n]));
293 void queue_free(struct queue_entry *q, int rest) {
298 queue_free(q->next, rest);
299 for(n = 0; n < NFIELDS; ++n)
300 fields[n].free(q, fields[n].offset);