structs-guile/main.c

#include <libguile.h>
#include "structs.h"
#include "main.h"
static SCM body_type;
static SCM page_type;

void init_body_type(void)
{
  body_type =
    scm_make_foreign_object_type(scm_from_utf8_symbol("body"),
                                 scm_list_1(scm_from_utf8_symbol("data")),
                                 NULL);
}

void init_page_type(void)
{
  page_type =
    scm_make_foreign_object_type(scm_from_utf8_symbol("page"),
                                 scm_list_1(scm_from_utf8_symbol("data")),
                                 NULL);
}

void init_foreign_types(void)
{
  init_body_type();
  init_page_type();
}

SCM make_body(SCM format, SCM text)
{
  struct body *body;
  body = (struct body *) scm_gc_malloc(sizeof(struct body), "body");
  body->format = scm_to_stringn(format, NULL, "UTF-8", SCM_FAILED_CONVERSION_ERROR);
  body->text = scm_to_stringn(text, NULL, "UTF-8", SCM_FAILED_CONVERSION_ERROR);
  return scm_make_foreign_object_1(body_type, body);
}

SCM make_page(SCM title, SCM body)
{
  struct page *page;
  page = (struct page *) scm_gc_malloc(sizeof(struct page), "page");
  page->title = scm_to_stringn(title, NULL, "UTF-8", SCM_FAILED_CONVERSION_ERROR);
  page->body = (struct body *) scm_foreign_object_ref(body, 0);

  return scm_make_foreign_object_1(page_type, page);
}

SCM create_page(SCM page)
{
  CCreate((struct page *) scm_foreign_object_ref(page, 0));
  return SCM_UNSPECIFIED;
}

static void ready(void *data, int argc, char **argv)
{
  init_foreign_types();

  scm_c_define_gsubr("make-body",   2, 0, 0, make_body);
  scm_c_define_gsubr("make-page",   2, 0, 0, make_page);
  scm_c_define_gsubr("create-page", 1, 0, 0, create_page);

  scm_shell(argc, argv);
}

int main(int argc, char **argv)
{
  scm_boot_guile(argc, argv, ready, 0);
  return 0;
}