2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 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 2 of the License, or
8 * (at your option) any later version.
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.
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
37 const char *playing_states
[] = {
50 #define VALUE(q, offset, type) *(type *)((char *)q + offset)
52 static int unmarshall_long(char *data
, struct queue_entry
*q
,
54 void (*error_handler
)(const char *, void *),
56 if(xstrtol(&VALUE(q
, offset
, long), data
, 0, 0)) {
57 error_handler(strerror(errno
), u
);
63 static const char *marshall_long(const struct queue_entry
*q
, size_t offset
) {
67 n
= byte_snprintf(buffer
, sizeof buffer
, "%ld", VALUE(q
, offset
, long));
69 fatal(errno
, "error converting int");
70 else if((size_t)n
>= sizeof buffer
)
71 fatal(0, "long converted to decimal is too long");
72 return xstrdup(buffer
);
75 static int unmarshall_string(char *data
, struct queue_entry
*q
,
77 void attribute((unused
)) (*error_handler
)(const char *, void *),
78 void attribute((unused
)) *u
) {
79 VALUE(q
, offset
, char *) = data
;
83 static const char *marshall_string(const struct queue_entry
*q
, size_t offset
) {
84 return VALUE(q
, offset
, char *);
87 static int unmarshall_time_t(char *data
, struct queue_entry
*q
,
89 void (*error_handler
)(const char *, void *),
93 if(xstrtoll(&ul
, data
, 0, 0)) {
94 error_handler(strerror(errno
), u
);
97 VALUE(q
, offset
, time_t) = ul
;
101 static const char *marshall_time_t(const struct queue_entry
*q
, size_t offset
) {
105 n
= byte_snprintf(buffer
, sizeof buffer
,
106 "%"PRIdMAX
, (intmax_t)VALUE(q
, offset
, time_t));
108 fatal(errno
, "error converting time");
109 else if((size_t)n
>= sizeof buffer
)
110 fatal(0, "time converted to decimal is too long");
111 return xstrdup(buffer
);
114 static int unmarshall_state(char *data
, struct queue_entry
*q
,
116 void (*error_handler
)(const char *, void *),
120 if((n
= table_find(playing_states
, 0, sizeof (char *),
121 sizeof playing_states
/ sizeof *playing_states
,
123 D(("state=[%s] n=%d", data
, n
));
124 error_handler("invalid state", u
);
127 VALUE(q
, offset
, enum playing_state
) = n
;
131 static const char *marshall_state(const struct queue_entry
*q
, size_t offset
) {
132 return playing_states
[VALUE(q
, offset
, enum playing_state
)];
135 #define F(n, h) { #n, offsetof(struct queue_entry, n), marshall_##h, unmarshall_##h }
137 static const struct field
{
140 const char *(*marshall
)(const struct queue_entry
*q
, size_t offset
);
141 int (*unmarshall
)(char *data
, struct queue_entry
*q
, size_t offset
,
142 void (*error_handler
)(const char *, void *),
145 /* Keep this table sorted. */
149 F(scratched
, string
),
152 F(submitter
, string
),
158 int queue_unmarshall(struct queue_entry
*q
, const char *s
,
159 void (*error_handler
)(const char *, void *),
164 if(!(vec
= split(s
, &nvec
, SPLIT_QUOTES
, error_handler
, u
)))
166 return queue_unmarshall_vec(q
, nvec
, vec
, error_handler
, u
);
169 int queue_unmarshall_vec(struct queue_entry
*q
, int nvec
, char **vec
,
170 void (*error_handler
)(const char *, void *),
175 error_handler("invalid marshalled queue format", u
);
179 D(("key %s value %s", vec
[0], vec
[1]));
180 if((n
= TABLE_FIND(fields
, struct field
, name
, *vec
)) < 0) {
181 error_handler("unknown key in queue data", u
);
184 if(fields
[n
].unmarshall(vec
[1], q
, fields
[n
].offset
, error_handler
, u
))
192 char *queue_marshall(const struct queue_entry
*q
) {
194 const char *vec
[sizeof fields
/ sizeof *fields
], *v
;
198 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
199 if((v
= fields
[n
].marshall(q
, fields
[n
].offset
))) {
200 vec
[n
] = quoteutf8(v
);
201 len
+= strlen(vec
[n
]) + strlen(fields
[n
].name
) + 2;
204 s
= r
= xmalloc_noptr(len
);
205 for(n
= 0; n
< sizeof fields
/ sizeof *fields
; ++n
)
208 s
+= strlen(strcpy(s
, fields
[n
].name
));
210 s
+= strlen(strcpy(s
, vec
[n
]));