2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 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 /** @brief Insert queue entry @p n just after @p b
59 * @param b Insert after this entry
60 * @param n New entry to insert
62 void queue_insert_entry(struct queue_entry
*b
, struct queue_entry
*n
) {
69 /* remove an entry from a doubly-linked list */
70 void queue_delete_entry(struct queue_entry
*node
) {
71 node
->next
->prev
= node
->prev
;
72 node
->prev
->next
= node
->next
;
75 static int unmarshall_long(char *data
, struct queue_entry
*q
,
77 void (*error_handler
)(const char *, void *),
79 if(xstrtol(&VALUE(q
, offset
, long), data
, 0, 0)) {
80 error_handler(strerror(errno
), u
);
86 static const char *marshall_long(const struct queue_entry
*q
, size_t offset
) {
90 n
= byte_snprintf(buffer
, sizeof buffer
, "%ld", VALUE(q
, offset
, long));
92 fatal(errno
, "error converting int");
93 else if((size_t)n
>= sizeof buffer
)
94 fatal(0, "long converted to decimal is too long");
95 return xstrdup(buffer
);
98 static int unmarshall_string(char *data
, struct queue_entry
*q
,
100 void attribute((unused
)) (*error_handler
)(const char *, void *),
101 void attribute((unused
)) *u
) {
102 VALUE(q
, offset
, char *) = data
;
106 static const char *marshall_string(const struct queue_entry
*q
, size_t offset
) {
107 return VALUE(q
, offset
, char *);
110 static int unmarshall_time_t(char *data
, struct queue_entry
*q
,
112 void (*error_handler
)(const char *, void *),
116 if(xstrtoll(&ul
, data
, 0, 0)) {
117 error_handler(strerror(errno
), u
);
120 VALUE(q
, offset
, time_t) = ul
;
124 static const char *marshall_time_t(const struct queue_entry
*q
, size_t offset
) {
128 n
= byte_snprintf(buffer
, sizeof buffer
,
129 "%"PRIdMAX
, (intmax_t)VALUE(q
, offset
, time_t));
131 fatal(errno
, "error converting time");
132 else if((size_t)n
>= sizeof buffer
)
133 fatal(0, "time converted to decimal is too long");
134 return xstrdup(buffer
);
137 static int unmarshall_state(char *data
, struct queue_entry
*q
,
139 void (*error_handler
)(const char *, void *),
143 if((n
= table_find(playing_states
, 0, sizeof (char *),
144 sizeof playing_states
/ sizeof *playing_states
,
146 D(("state=[%s] n=%d", data
, n
));
147 error_handler("invalid state", u
);
150 VALUE(q
, offset
, enum playing_state
) = n
;
154 static int unmarshall_origin(char *data
, struct queue_entry
*q
,
156 void (*error_handler
)(const char *, void *),
160 if((n
= table_find(track_origins
, 0, sizeof (char *),
161 sizeof track_origins
/ sizeof *track_origins
,
163 D(("origin=[%s] n=%d", data
, n
));
164 error_handler("invalid origin", u
);
167 VALUE(q
, offset
, enum track_origin
) = n
;
171 static const char *marshall_state(const struct queue_entry
*q
, size_t offset
) {
172 return playing_states
[VALUE(q
, offset
, enum playing_state
)];
175 static const char *marshall_origin(const struct queue_entry
*q
, size_t offset
) {
176 return track_origins
[VALUE(q
, offset
, enum track_origin
)];
179 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
181 static const struct field
{
184 const char *(*marshall
)(const struct queue_entry
*q
, size_t offset
);
185 int (*unmarshall
)(char *data
, struct queue_entry
*q
, size_t offset
,
186 void (*error_handler
)(const char *, void *),
189 /* Keep this table sorted. */
194 F(scratched
, string
),
197 F(submitter
, string
),
203 int queue_unmarshall(struct queue_entry
*q
, const char *s
,
204 void (*error_handler
)(const char *, void *),
209 q
->pid
= -1; /* =none */
210 if(!(vec
= split(s
, &nvec
, SPLIT_QUOTES
, error_handler
, u
)))
212 return queue_unmarshall_vec(q
, nvec
, vec
, error_handler
, u
);
215 int queue_unmarshall_vec(struct queue_entry
*q
, int nvec
, char **vec
,
216 void (*error_handler
)(const char *, void *),
221 error_handler("invalid marshalled queue format", u
);
225 D(("key %s value %s", vec
[0], vec
[1]));
226 if((n
= TABLE_FIND(fields
, name
, *vec
)) < 0) {
227 error_handler("unknown key in queue data", u
);
230 if(fields
[n
].unmarshall(vec
[1], q
, fields
[n
].offset
, error_handler
, u
))
238 char *queue_marshall(const struct queue_entry
*q
) {
240 const char *vec
[sizeof fields
/ sizeof *fields
], *v
;
244 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
245 if((v
= fields
[n
].marshall(q
, fields
[n
].offset
))) {
246 vec
[n
] = quoteutf8(v
);
247 len
+= strlen(vec
[n
]) + strlen(fields
[n
].name
) + 2;
250 s
= r
= xmalloc_noptr(len
);
251 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
254 s
+= strlen(strcpy(s
, fields
[n
].name
));
256 s
+= strlen(strcpy(s
, vec
[n
]));