/***********************************************************************/
/*                                                                     */
/*                                 GALAX                               */
/*                              XQuery Engine                          */
/*                                                                     */
/*  Copyright 2001-2003, Lucent Technologies.                          */
/*  Distributed only by permission.                                    */
/*                                                                     */
/***********************************************************************/

/* $Id: c-example.c,v 1.3 2003/06/12 02:30:44 simeon Exp $ */

/* A sample C client for the Caml functions */

#include <stdio.h>
#include "glx.h"


/* 
   Example 1 

   Build a query context from a file.
   Load a document.
   Add a global variable to query context, bound to the loaded document.
   Evaluate a query contained in a string.
   Serialize result to standard output

*/

void example1(module_context mod_ctxt)
{
  itemlist res;
  module_context mod_ctxt1;
  glx_err err; 

  err = glx_eval_global_variables(mod_ctxt, &mod_ctxt1);
  err = glx_eval_statement_from_string(mod_ctxt1, 
              "for $s in $report//section[section.title = \"Procedure\"] return ($s//incision)[2]/instrument", &res);
  err = glx_serialize_to_output_channel(res);
  itemlist_free(res);
  item_free(mod_ctxt1);
}

/* 
   Example 2

   Build a query context from a file.
   Load a document.
   Evaluate a query contained in a file.
   Serialize result to standard output.

*/
void example2(module_context mod_ctxt)
{
  query_context query_ctxt;
  itemlist res;
  xs_string xmlstr;
  module_context mod_ctxt1;
  glx_err err;

  err = glx_eval_global_variables(mod_ctxt, &mod_ctxt1);
  err = glx_import_main_module(mod_ctxt1, "seq.xq", &query_ctxt); 
  err = glx_eval_statements(query_ctxt, &res);
  err = glx_serialize_to_output_channel(res);
  itemlist_free(res);
  item_free(mod_ctxt1);
}

/* 
   Example 3

   Build a query context from a file.
   Create some XML values. 
   Evaluate a query function contained in the query context by
   applying to XML values.
   Serialize result to a string & print. 

*/
void example3 (module_context mod_ctxt, itemlist xmldoc) 
{
  module_context mod_ctxt1, mod_ctxt2;
  itemlist res;
  xs_string xmlstr;
  xs_int xmlint;
  itemlist args[2];
  glx_err err;

  err = glx_eval_global_variables(mod_ctxt, &mod_ctxt1);
  err = glx_import_library_module(mod_ctxt1, "xq-example1.xq", &mod_ctxt2); 
  err = glx_xs_string("a string", &xmlstr); 
  err = glx_xs_int(12, &xmlint);
  args[0] = itemlist_cons(xmlint, itemlist_empty()); 
  args[1] = itemlist_cons(xmlstr, itemlist_empty()); 
  err = glx_eval_query_function(mod_ctxt2, "test2", args, 2, &res); 
  err = glx_serialize_to_output_channel(res);
  itemlist_free(res);
  item_free(mod_ctxt1);
}

int main(int argc, char ** argv)
{

  char *fake_argv[2];
  module_context mod_ctxt, mod_ctxt1;
  itemlist xmldoc;
  itemlist res;
  xs_string xmlstr; 
  processing_context pc;
  glx_err err;
 
  glx_init();

  err = glx_default_processing_context(&pc);
  err = glx_load_standard_library(pc, &mod_ctxt);
  err = glx_import_library_module(mod_ctxt,"seq_context.xq", &mod_ctxt1);
  err = glx_load_document("report.xml", &xmldoc);
  
  example1(mod_ctxt1);
  example2(mod_ctxt1);
  example3(mod_ctxt1, xmldoc);

  return 0;
}
