/*
 * Copyright (c)2004 Cat's Eye Technologies.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *   Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 
 *   Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 * 
 *   Neither the name of Cat's Eye Technologies nor the names of its
 *   contributors may be used to endorse or promote products derived
 *   from this software without specific prior written permission. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 */

/*
 * decode.c
 * $Id: decode.c,v 1.2 2004/07/10 01:06:43 cpressey Exp $
 */

#include <ctype.h>
#include <stdlib.h>

#include "dfui.h"
#include "encoding.h"
#include "mem.h"
#include "dump.h"

/*** BASIC TYPES ***/

char *
dfui_decode_string(struct dfui_strpkg *p)
{
	char *str;
	int i = 0;
	int len = 0;

	while (isdigit(dfui_strpkg_char(p)) && !dfui_strpkg_at_end(p)) {
		len = len * 10 + dfui_strpkg_char_advance(p) - '0';
	}

	str = malloc(len + 1);
	
	if (!dfui_strpkg_expect(p, ":")) return(NULL);
	while (len > 0 && !dfui_strpkg_at_end(p)) {
		str[i++] = dfui_strpkg_char_advance(p);
		len--;
	}

	str[i] = '\0';

	return(str);
}

int
dfui_decode_int(struct dfui_strpkg *p)
{
	int x = 0;

	while (isdigit(dfui_strpkg_char(p)) && !dfui_strpkg_at_end(p)) {
		x = x * 10 + dfui_strpkg_char_advance(p) - '0';
	}
	if (dfui_strpkg_expect(p, " ")) {
		return(x);
	} else {
		return(0);
	}
}

int
dfui_decode_bool(struct dfui_strpkg *p)
{
	char c;
	
	c = dfui_strpkg_char_advance(p);

	if (c == 'Y')
		return(1);
	else if (c == 'N')
		return(0);
	else /* XXX ??? error */
		return(0);
}

/*** FORM TYPES ***/

dfui_id_t
dfui_decode_id(struct dfui_strpkg *p)
{
	char *str;
	dfui_id_t id;
	
	str = dfui_decode_string(p);
	id = dfui_id_new(str);
	free(str);

	return(id);
}

struct dfui_info *
dfui_decode_info(struct dfui_strpkg *p)
{
	char *name, *short_desc, *long_desc;
	struct dfui_info *i;

	name = dfui_decode_string(p);
	short_desc = dfui_decode_string(p);
	long_desc = dfui_decode_string(p);

	i = dfui_info_new(name, short_desc, long_desc);
	
	free(name);
	free(short_desc);
	free(long_desc);
	
	return(i);
}

struct dfui_field *
dfui_decode_field(struct dfui_strpkg *p)
{
	dfui_id_t id;
	struct dfui_info *i;
	struct dfui_field *fi;

	id = dfui_decode_id(p);
	i = dfui_decode_info(p);

	fi = dfui_field_new(id, i);
	fi->editable = dfui_decode_bool(p);
	fi->obscured = dfui_decode_bool(p);
	fi->option_head = dfui_decode_options(p);
	fi->hint_head = dfui_decode_celldatas(p);

	return(fi);
}

struct dfui_field *
dfui_decode_fields(struct dfui_strpkg *p)
{
	struct dfui_field *head = NULL, *fi;

	if (!dfui_strpkg_expect(p, "f{")) return(NULL);
	while (dfui_strpkg_char(p) != '}') {
		fi = dfui_decode_field(p);
		fi->next = head;
		head = fi;
	}
	dfui_strpkg_expect(p, "}");
	
	return(head);
}

struct dfui_option *
dfui_decode_option(struct dfui_strpkg *p)
{
	char *value;

	value = dfui_decode_string(p);

	return(dfui_option_new(value));
}

struct dfui_option *
dfui_decode_options(struct dfui_strpkg *p)
{
	struct dfui_option *head = NULL, *o;

	if (!dfui_strpkg_expect(p, "O{")) return(NULL);
	while (dfui_strpkg_char(p) != '}') {
		o = dfui_decode_option(p);
		o->next = head;
		head = o;
	}
	dfui_strpkg_expect(p, "}");
	
	return(head);
}

struct dfui_action *
dfui_decode_action(struct dfui_strpkg *p)
{
	dfui_id_t id;
	struct dfui_info *i;
	struct dfui_action *a;

	id = dfui_decode_id(p);
	i = dfui_decode_info(p);
	a = dfui_action_new(id, i);
	a->hint_head = dfui_decode_celldatas(p);

	return(a);
}

struct dfui_action *
dfui_decode_actions(struct dfui_strpkg *p)
{
	struct dfui_action *head = NULL, *a;

	if (!dfui_strpkg_expect(p, "a{")) return(NULL);
	while (dfui_strpkg_char(p) != '}') {
		a = dfui_decode_action(p);
		a->next = head;
		head = a;
	}
	dfui_strpkg_expect(p, "}");
	
	return(head);
}

struct dfui_celldata *
dfui_decode_celldata(struct dfui_strpkg *p)
{
	dfui_id_t field_id;
	char *value;
	struct dfui_celldata *c;
	
	field_id = dfui_decode_id(p);
	value = dfui_decode_string(p);
	
	c = dfui_celldata_new(field_id, value);

	free(value);

	return(c);
}

struct dfui_celldata *
dfui_decode_celldatas(struct dfui_strpkg *p)
{
	struct dfui_celldata *c, *head = NULL;

	if (!dfui_strpkg_expect(p, "d{")) return(NULL);
	while (dfui_strpkg_char(p) != '}') {
		c = dfui_decode_celldata(p);
		c->next = head;
		head = c;
	}
	dfui_strpkg_expect(p, "}");
	
	return(head);
}

struct dfui_dataset *
dfui_decode_dataset(struct dfui_strpkg *p)
{
	struct dfui_dataset *ds;

	ds = dfui_dataset_new();
	ds->celldata_head = dfui_decode_celldatas(p);
	
	return(ds);
}

struct dfui_dataset *
dfui_decode_datasets(struct dfui_strpkg *p)
{
	struct dfui_dataset *head = NULL, *ds;

	if (!dfui_strpkg_expect(p, "D{")) return(NULL);
	while (dfui_strpkg_char(p) != '}') {
		ds = dfui_decode_dataset(p);
		ds->next = head;
		head = ds;
	}
	dfui_strpkg_expect(p, "}");
	
	return(head);
}

struct dfui_form *
dfui_decode_form(struct dfui_strpkg *p)
{
	dfui_id_t id;
	struct dfui_info *i;
	struct dfui_form *f;

	if (!dfui_strpkg_expect(p, "F{")) return(NULL);

	id = dfui_decode_id(p);
	i = dfui_decode_info(p);
	
	f = dfui_form_new(id, i);

	dfui_form_set_multiple(f, dfui_decode_bool(p));
	dfui_form_set_extensible(f, dfui_decode_bool(p));

	f->field_head = dfui_decode_fields(p);
	f->action_head = dfui_decode_actions(p);
	f->dataset_head = dfui_decode_datasets(p);
	f->hint_head = dfui_decode_celldatas(p);

	dfui_strpkg_expect(p, "}");
	
	return(f);
}

struct dfui_response *
dfui_decode_response(struct dfui_strpkg *p)
{
	dfui_id_t form_id;
	dfui_id_t action_id;
	struct dfui_response *r;

	if (!dfui_strpkg_expect(p, "R{")) return(NULL);

	form_id = dfui_decode_id(p);
	action_id = dfui_decode_id(p);
	r = dfui_response_new(form_id, action_id);
	r->dataset_head = dfui_decode_datasets(p);

	dfui_strpkg_expect(p, "}");

	return(r);
}

struct dfui_progress *
dfui_decode_progress(struct dfui_strpkg *p)
{
	struct dfui_info *i;
	int amount;
	struct dfui_progress *pr;

	i = dfui_decode_info(p);
	amount = dfui_decode_int(p);

	pr = dfui_progress_new(i, amount);
	
	return(pr);
}
