2 * Copyright (c)2004 Cat's Eye Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * Neither the name of Cat's Eye Technologies nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * $Id: decode.c,v 1.11 2005/02/07 06:39:59 cpressey Exp $
42 #include <libaura/mem.h>
43 #include <libaura/buffer.h>
45 #define NEEDS_DFUI_STRUCTURE_DEFINITIONS
47 #undef NEEDS_DFUI_STRUCTURE_DEFINITIONS
54 * This function returns a newly-allocated string. It is the
55 * caller's responsibility that it be free()'ed.
58 dfui_decode_string(struct aura_buffer *e)
64 while (isdigit(aura_buffer_peek_char(e)) && !aura_buffer_eof(e)) {
65 len = len * 10 + aura_buffer_scan_char(e) - '0';
68 str = aura_malloc(len + 1, "decoded string");
70 if (!aura_buffer_expect(e, ":")) return(NULL);
71 while (len > 0 && !aura_buffer_eof(e)) {
72 str[i++] = aura_buffer_scan_char(e);
82 dfui_decode_int(struct aura_buffer *e)
86 while (isdigit(aura_buffer_peek_char(e)) && !aura_buffer_eof(e)) {
87 x = x * 10 + aura_buffer_scan_char(e) - '0';
89 if (aura_buffer_expect(e, " ")) {
97 dfui_decode_bool(struct aura_buffer *e)
101 c = aura_buffer_scan_char(e);
107 else /* XXX ??? error */
114 dfui_decode_info(struct aura_buffer *e)
116 char *name, *short_desc, *long_desc;
119 name = dfui_decode_string(e);
120 short_desc = dfui_decode_string(e);
121 long_desc = dfui_decode_string(e);
123 i = dfui_info_new(name, short_desc, long_desc);
133 dfui_decode_field(struct aura_buffer *e)
137 struct dfui_field *fi;
139 id = dfui_decode_string(e);
140 i = dfui_decode_info(e);
142 fi = dfui_field_new(id, i);
143 fi->option_head = dfui_decode_options(e);
144 fi->property_head = dfui_decode_properties(e);
151 dfui_decode_fields(struct aura_buffer *e)
153 struct dfui_field *head = NULL, *fi;
155 if (!aura_buffer_expect(e, "f{")) return(NULL);
156 while (aura_buffer_peek_char(e) != '}') {
157 fi = dfui_decode_field(e);
161 aura_buffer_expect(e, "}");
167 dfui_decode_option(struct aura_buffer *e)
171 value = dfui_decode_string(e);
173 return(dfui_option_new(value));
177 dfui_decode_options(struct aura_buffer *e)
179 struct dfui_option *head = NULL, *o;
181 if (!aura_buffer_expect(e, "O{")) return(NULL);
182 while (aura_buffer_peek_char(e) != '}') {
183 o = dfui_decode_option(e);
187 aura_buffer_expect(e, "}");
193 dfui_decode_action(struct aura_buffer *e)
197 struct dfui_action *a;
199 id = dfui_decode_string(e);
200 i = dfui_decode_info(e);
201 a = dfui_action_new(id, i);
202 a->property_head = dfui_decode_properties(e);
209 dfui_decode_actions(struct aura_buffer *e)
211 struct dfui_action *head = NULL, *a;
213 if (!aura_buffer_expect(e, "a{")) return(NULL);
214 while (aura_buffer_peek_char(e) != '}') {
215 a = dfui_decode_action(e);
219 aura_buffer_expect(e, "}");
224 struct dfui_celldata *
225 dfui_decode_celldata(struct aura_buffer *e)
229 struct dfui_celldata *c;
231 field_id = dfui_decode_string(e);
232 value = dfui_decode_string(e);
234 c = dfui_celldata_new(field_id, value);
242 struct dfui_celldata *
243 dfui_decode_celldatas(struct aura_buffer *e)
245 struct dfui_celldata *c, *head = NULL;
247 if (!aura_buffer_expect(e, "d{")) return(NULL);
248 while (aura_buffer_peek_char(e) != '}') {
249 c = dfui_decode_celldata(e);
253 aura_buffer_expect(e, "}");
258 struct dfui_property *
259 dfui_decode_property(struct aura_buffer *e)
262 struct dfui_property *h;
264 name = dfui_decode_string(e);
265 value = dfui_decode_string(e);
267 h = dfui_property_new(name, value);
275 struct dfui_property *
276 dfui_decode_properties(struct aura_buffer *e)
278 struct dfui_property *h, *head = NULL;
280 if (!aura_buffer_expect(e, "p{")) return(NULL);
281 while (aura_buffer_peek_char(e) != '}') {
282 h = dfui_decode_property(e);
286 aura_buffer_expect(e, "}");
291 struct dfui_dataset *
292 dfui_decode_dataset(struct aura_buffer *e)
294 struct dfui_dataset *ds;
296 ds = dfui_dataset_new();
297 ds->celldata_head = dfui_decode_celldatas(e);
302 struct dfui_dataset *
303 dfui_decode_datasets(struct aura_buffer *e)
305 struct dfui_dataset *head = NULL, *ds;
307 if (!aura_buffer_expect(e, "D{")) return(NULL);
308 while (aura_buffer_peek_char(e) != '}') {
309 ds = dfui_decode_dataset(e);
313 aura_buffer_expect(e, "}");
319 dfui_decode_form(struct aura_buffer *e)
325 if (!aura_buffer_expect(e, "F{")) return(NULL);
327 id = dfui_decode_string(e);
328 i = dfui_decode_info(e);
330 f = dfui_form_new(id, i);
332 dfui_form_set_multiple(f, dfui_decode_bool(e));
333 dfui_form_set_extensible(f, dfui_decode_bool(e));
335 f->field_head = dfui_decode_fields(e);
336 f->action_head = dfui_decode_actions(e);
337 f->dataset_head = dfui_decode_datasets(e);
338 f->property_head = dfui_decode_properties(e);
340 aura_buffer_expect(e, "}");
346 struct dfui_response *
347 dfui_decode_response(struct aura_buffer *e)
351 struct dfui_response *r;
353 if (!aura_buffer_expect(e, "R{")) return(NULL);
355 form_id = dfui_decode_string(e);
356 action_id = dfui_decode_string(e);
357 r = dfui_response_new(form_id, action_id);
358 r->dataset_head = dfui_decode_datasets(e);
362 aura_buffer_expect(e, "}");
367 struct dfui_progress *
368 dfui_decode_progress(struct aura_buffer *e)
371 int amount, streaming;
373 struct dfui_progress *pr;
375 i = dfui_decode_info(e);
376 amount = dfui_decode_int(e);
377 streaming = dfui_decode_int(e);
378 msg_line = dfui_decode_string(e);
380 pr = dfui_progress_new(i, amount);
381 dfui_progress_set_streaming(pr, streaming);
382 dfui_progress_set_msg_line(pr, msg_line);