/*
Copyright (c) 2000-2004, 2007 Tony Garnock-Jones <tonyg@kcbbs.gen.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <assert.h>

#include "ubf_a.h"

typedef struct Params {
  UBF_A_CharSink sink;
  void *sink_arg;
  int wrote_integer;
} Params;

static void emit(Params *p, char c) {
  p->sink(c, p->sink_arg);
}

static void quoteString(Params *p, char quotechar, char const *str) {
  emit(p, quotechar);
  while (*str) {
    if (*str == '\\' || *str == quotechar)
      emit(p, '\\');
    emit(p, *str);
    str++;
  }
  emit(p, quotechar);
}

static void encode_num(Params *p, signed long long num) {
  char buf[128];
  char *str;
  if (p->wrote_integer && num >= 0)
    emit(p, ' ');
  sprintf(buf, "%lld", num);
  for (str = buf; *str != '\0'; str++)
    emit(p, *str);
}

static UBF_A_Error encode(Params *p, UBF_A_Object const *o) {
  UBF_A_Error status = UBF_A_NoError;
  int new_wrote_integer = 0;

  if (o == NULL) {
    emit(p, '#');
  } else {
    switch (o->kind) {
      case UBF_A_NUMBER:
	encode_num(p, o->body.num);
	new_wrote_integer = 1;
	break;

      case UBF_A_STRING:
	quoteString(p, '"', o->body.data.vec);
	break;

      case UBF_A_SYMBOL:
	quoteString(p, '\'', o->body.data.vec);
	break;

      case UBF_A_BINARY: {
	int i;

	encode_num(p, o->body.data.length);
	emit(p, '~');
	for (i = 0; i < o->body.data.length; i++)
	  emit(p, o->body.data.vec[i]);
	emit(p, '~');
	break;
      }

      case UBF_A_TAG:
	status = encode(p, o->body.tag.value);
	if (status) break;
	quoteString(p, '`', o->body.tag.key);
	break;

      case UBF_A_PAIR:
	status = encode(p, o->body.pair.tail);
	if (status) break;
	status = encode(p, o->body.pair.head);
	if (status) break;
	emit(p, '&');
	break;

      case UBF_A_VECTOR: {
	int i;

	emit(p, '{');
	for (i = 0; i < o->body.vector.length; i++) {
	  status = encode(p, o->body.vector.vec[i]);
	  if (status) break;
	}
	if (status) break;
	emit(p, '}');
	break;
      }

      default:
	status = UBF_A_UnknownObjectKind;
    }
  }

  p->wrote_integer = new_wrote_integer;
  return status;
}

UBF_A_Error ubf_a_encode(UBF_A_Object const *o, UBF_A_CharSink sink, void *sink_arg) {
  Params p;
  UBF_A_Error status = UBF_A_NoError;
  p.sink = sink;
  p.sink_arg = sink_arg;
  p.wrote_integer = 0;
  status = encode(&p, o);
  emit(&p, '$');
  return status;
}

