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/>.
34 const char *const playing_states
[] = {
47 /** @brief String values for @c origin field */
48 const char *const track_origins
[] = {
56 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
58 /* add new entry @n@ to a doubly linked list just after @b@ */
59 void queue_insert_entry(struct queue_entry
*b
, struct queue_entry
*n
) {
66 /* remove an entry from a doubly-linked list */
67 void queue_delete_entry(struct queue_entry
*node
) {
68 node
->next
->prev
= node
->prev
;
69 node
->prev
->next
= node
->next
;
72 static int unmarshall_long(char *data
, struct queue_entry
*q
,
74 void (*error_handler
)(const char *, void *),
76 if(xstrtol(&VALUE(q
, offset
, long), data
, 0, 0)) {
77 error_handler(strerror(errno
), u
);
83 static const char *marshall_long(const struct queue_entry
*q
, size_t offset
) {
87 n
= byte_snprintf(buffer
, sizeof buffer
, "%ld", VALUE(q
, offset
, long));
89 fatal(errno
, "error converting int");
90 else if((size_t)n
>= sizeof buffer
)
91 fatal(0, "long converted to decimal is too long");
92 return xstrdup(buffer
);
95 static int unmarshall_string(char *data
, struct queue_entry
*q
,
97 void attribute((unused
)) (*error_handler
)(const char *, void *),
98 void attribute((unused
)) *u
) {
99 VALUE(q
, offset
, char *) = data
;
103 static const char *marshall_string(const struct queue_entry
*q
, size_t offset
) {
104 return VALUE(q
, offset
, char *);
107 static int unmarshall_time_t(char *data
, struct queue_entry
*q
,
109 void (*error_handler
)(const char *, void *),
113 if(xstrtoll(&ul
, data
, 0, 0)) {
114 error_handler(strerror(errno
), u
);
117 VALUE(q
, offset
, time_t) = ul
;
121 static const char *marshall_time_t(const struct queue_entry
*q
, size_t offset
) {
125 n
= byte_snprintf(buffer
, sizeof buffer
,
126 "%"PRIdMAX
, (intmax_t)VALUE(q
, offset
, time_t));
128 fatal(errno
, "error converting time");
129 else if((size_t)n
>= sizeof buffer
)
130 fatal(0, "time converted to decimal is too long");
131 return xstrdup(buffer
);
134 static int unmarshall_state(char *data
, struct queue_entry
*q
,
136 void (*error_handler
)(const char *, void *),
140 if((n
= table_find(playing_states
, 0, sizeof (char *),
141 sizeof playing_states
/ sizeof *playing_states
,
143 D(("state=[%s] n=%d", data
, n
));
144 error_handler("invalid state", u
);
147 VALUE(q
, offset
, enum playing_state
) = n
;
151 static int unmarshall_origin(char *data
, struct queue_entry
*q
,
153 void (*error_handler
)(const char *, void *),
157 if((n
= table_find(track_origins
, 0, sizeof (char *),
158 sizeof track_origins
/ sizeof *track_origins
,
160 D(("origin=[%s] n=%d", data
, n
));
161 error_handler("invalid origin", u
);
164 VALUE(q
, offset
, enum track_origin
) = n
;
168 static const char *marshall_state(const struct queue_entry
*q
, size_t offset
) {
169 return playing_states
[VALUE(q
, offset
, enum playing_state
)];
172 static const char *marshall_origin(const struct queue_entry
*q
, size_t offset
) {
173 return track_origins
[VALUE(q
, offset
, enum track_origin
)];
176 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
178 static const struct field
{
181 const char *(*marshall
)(const struct queue_entry
*q
, size_t offset
);
182 int (*unmarshall
)(char *data
, struct queue_entry
*q
, size_t offset
,
183 void (*error_handler
)(const char *, void *),
186 /* Keep this table sorted. */
191 F(scratched
, string
),
194 F(submitter
, string
),
200 int queue_unmarshall(struct queue_entry
*q
, const char *s
,
201 void (*error_handler
)(const char *, void *),
206 if(!(vec
= split(s
, &nvec
, SPLIT_QUOTES
, error_handler
, u
)))
208 return queue_unmarshall_vec(q
, nvec
, vec
, error_handler
, u
);
211 int queue_unmarshall_vec(struct queue_entry
*q
, int nvec
, char **vec
,
212 void (*error_handler
)(const char *, void *),
217 error_handler("invalid marshalled queue format", u
);
221 D(("key %s value %s", vec
[0], vec
[1]));
222 if((n
= TABLE_FIND(fields
, name
, *vec
)) < 0) {
223 error_handler("unknown key in queue data", u
);
226 if(fields
[n
].unmarshall(vec
[1], q
, fields
[n
].offset
, error_handler
, u
))
234 char *queue_marshall(const struct queue_entry
*q
) {
236 const char *vec
[sizeof fields
/ sizeof *fields
], *v
;
240 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
241 if((v
= fields
[n
].marshall(q
, fields
[n
].offset
))) {
242 vec
[n
] = quoteutf8(v
);
243 len
+= strlen(vec
[n
]) + strlen(fields
[n
].name
) + 2;
246 s
= r
= xmalloc_noptr(len
);
247 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
250 s
+= strlen(strcpy(s
, fields
[n
].name
));
252 s
+= strlen(strcpy(s
, vec
[n
]));