2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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/>.
32 const char *playing_states
[] = {
45 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
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
) {
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
;
61 static int unmarshall_long(char *data
, struct queue_entry
*q
,
63 void (*error_handler
)(const char *, void *),
65 if(xstrtol(&VALUE(q
, offset
, long), data
, 0, 0)) {
66 error_handler(strerror(errno
), u
);
72 static const char *marshall_long(const struct queue_entry
*q
, size_t offset
) {
76 n
= byte_snprintf(buffer
, sizeof buffer
, "%ld", VALUE(q
, offset
, long));
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
);
84 static int unmarshall_string(char *data
, struct queue_entry
*q
,
86 void attribute((unused
)) (*error_handler
)(const char *, void *),
87 void attribute((unused
)) *u
) {
88 VALUE(q
, offset
, char *) = data
;
92 static const char *marshall_string(const struct queue_entry
*q
, size_t offset
) {
93 return VALUE(q
, offset
, char *);
96 static int unmarshall_time_t(char *data
, struct queue_entry
*q
,
98 void (*error_handler
)(const char *, void *),
102 if(xstrtoll(&ul
, data
, 0, 0)) {
103 error_handler(strerror(errno
), u
);
106 VALUE(q
, offset
, time_t) = ul
;
110 static const char *marshall_time_t(const struct queue_entry
*q
, size_t offset
) {
114 n
= byte_snprintf(buffer
, sizeof buffer
,
115 "%"PRIdMAX
, (intmax_t)VALUE(q
, offset
, time_t));
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
);
123 static int unmarshall_state(char *data
, struct queue_entry
*q
,
125 void (*error_handler
)(const char *, void *),
129 if((n
= table_find(playing_states
, 0, sizeof (char *),
130 sizeof playing_states
/ sizeof *playing_states
,
132 D(("state=[%s] n=%d", data
, n
));
133 error_handler("invalid state", u
);
136 VALUE(q
, offset
, enum playing_state
) = n
;
140 static const char *marshall_state(const struct queue_entry
*q
, size_t offset
) {
141 return playing_states
[VALUE(q
, offset
, enum playing_state
)];
144 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
146 static const struct field
{
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 *),
154 /* Keep this table sorted. */
158 F(scratched
, string
),
161 F(submitter
, string
),
167 int queue_unmarshall(struct queue_entry
*q
, const char *s
,
168 void (*error_handler
)(const char *, void *),
173 if(!(vec
= split(s
, &nvec
, SPLIT_QUOTES
, error_handler
, u
)))
175 return queue_unmarshall_vec(q
, nvec
, vec
, error_handler
, u
);
178 int queue_unmarshall_vec(struct queue_entry
*q
, int nvec
, char **vec
,
179 void (*error_handler
)(const char *, void *),
184 error_handler("invalid marshalled queue format", u
);
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
);
193 if(fields
[n
].unmarshall(vec
[1], q
, fields
[n
].offset
, error_handler
, u
))
201 char *queue_marshall(const struct queue_entry
*q
) {
203 const char *vec
[sizeof fields
/ sizeof *fields
], *v
;
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;
213 s
= r
= xmalloc_noptr(len
);
214 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
217 s
+= strlen(strcpy(s
, fields
[n
].name
));
219 s
+= strlen(strcpy(s
, vec
[n
]));