/*
 * 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. 
 */

/*
 * encode.c
 * $Id: encode.c,v 1.2 2004/07/10 02:35:22 cpressey Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "dfui.h"
#include "encoding.h"

/*** BASIC TYPES ***/

void
dfui_encode_string(struct dfui_strpkg *p, char *str)
{
	char fmt[16];
	
	sprintf(fmt, "%d", strlen(str));
	dfui_strpkg_write(p, fmt);
	dfui_strpkg_write(p, ":");
	dfui_strpkg_write(p, str);
}

void
dfui_encode_int(struct dfui_strpkg *p, int i)
{
	char fmt[16];
	
	sprintf(fmt, "%d", i);
	dfui_strpkg_write(p, fmt);
	dfui_strpkg_write(p, " ");
}

void
dfui_encode_bool(struct dfui_strpkg *p, int b)
{
	if (b)
		dfui_strpkg_write(p, "Y");
	else
		dfui_strpkg_write(p, "N");
}

/*** FORM TYPES ***/

void
dfui_encode_id(struct dfui_strpkg *p, dfui_id_t id)
{
	dfui_encode_string(p, (char *)id);
}

void
dfui_encode_info(struct dfui_strpkg *p, struct dfui_info *i)
{
	dfui_encode_string(p, i->name);
	dfui_encode_string(p, i->short_desc);
	dfui_encode_string(p, i->long_desc);
}

void
dfui_encode_form(struct dfui_strpkg *p, struct dfui_form *f)
{
	dfui_strpkg_write(p, "F{");
	dfui_encode_id(p, f->id);
	dfui_encode_info(p, f->info);

	dfui_encode_bool(p, f->multiple);
	dfui_encode_bool(p, f->extensible);
	
	dfui_encode_fields(p, f->field_head);
	dfui_encode_actions(p, f->action_head);
	dfui_encode_datasets(p, f->dataset_head);
	dfui_encode_celldatas(p, f->hint_head);

	dfui_strpkg_write(p, "}");
}

void
dfui_encode_fields(struct dfui_strpkg *p, struct dfui_field *head)
{
	struct dfui_field *fi;

	dfui_strpkg_write(p, "f{");
	for (fi = head; fi != NULL; fi = fi->next) {
		dfui_encode_field(p, fi);
	}
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_field(struct dfui_strpkg *p, struct dfui_field *fi)
{
	dfui_encode_id(p, fi->id);
	dfui_encode_info(p, fi->info);
	dfui_encode_bool(p, fi->editable);
	dfui_encode_bool(p, fi->obscured);
	dfui_encode_options(p, fi->option_head);
	dfui_encode_celldatas(p, fi->hint_head);
}

void
dfui_encode_options(struct dfui_strpkg *p, struct dfui_option *head)
{
	struct dfui_option *o;

	dfui_strpkg_write(p, "O{");
	for (o = head; o != NULL; o = o->next) {
		dfui_encode_option(p, o);
	}
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_option(struct dfui_strpkg *p, struct dfui_option *o)
{
	dfui_encode_string(p, o->value);
}

void
dfui_encode_actions(struct dfui_strpkg *p, struct dfui_action *head)
{
	struct dfui_action *a;

	dfui_strpkg_write(p, "a{");
	for (a = head; a != NULL; a = a->next) {
		dfui_encode_action(p, a);
	}
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_action(struct dfui_strpkg *p, struct dfui_action *a)
{
	dfui_encode_id(p, a->id);
	dfui_encode_info(p, a->info);
	dfui_encode_celldatas(p, a->hint_head);
}

void
dfui_encode_datasets(struct dfui_strpkg *p, struct dfui_dataset *head)
{
	struct dfui_dataset *ds;

	dfui_strpkg_write(p, "D{");
	for (ds = head; ds != NULL; ds = ds->next) {
		dfui_encode_dataset(p, ds);
	}
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_dataset(struct dfui_strpkg *p, struct dfui_dataset *ds)
{
	dfui_encode_celldatas(p, ds->celldata_head);
}

void
dfui_encode_celldatas(struct dfui_strpkg *p, struct dfui_celldata *c)
{
	dfui_strpkg_write(p, "d{");
	while (c != NULL) {
		dfui_encode_celldata(p, c);
		c = c->next;
	}
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_celldata(struct dfui_strpkg *p, struct dfui_celldata *c)
{
	dfui_encode_id(p, c->field_id);
	dfui_encode_string(p, c->value);
}

void
dfui_encode_response(struct dfui_strpkg *p, struct dfui_response *r)
{
	dfui_strpkg_write(p, "R{");
	dfui_encode_id(p, r->form_id);
	dfui_encode_id(p, r->action_id);
	dfui_encode_datasets(p, r->dataset_head);
	dfui_strpkg_write(p, "}");
}

void
dfui_encode_progress(struct dfui_strpkg *p, struct dfui_progress *pr)
{
	dfui_encode_info(p, pr->info);
	dfui_encode_int(p, pr->amount);
}
