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

/* $Id: glx.h,v 1.5 2003/06/27 16:52:00 mff Exp $ */

#include "itemlist.h"

/**********************/
/* Atomic value types */
/**********************/

typedef item atomicvalue;
typedef item xs_string;
typedef item xs_boolean;
typedef item xs_int;
typedef item xs_integer;
typedef item xs_decimal;
typedef item xs_float;
typedef item xs_double;
typedef item xs_anyURI;
typedef item xs_untyped;
typedef item xs_QName;

/**************/
/* Node types */
/**************/

typedef item node;
typedef item document;
typedef item element;
typedef item text;
typedef item attribute;
typedef item comment;
typedef item processing_instruction;

/***************/
/* Other types */
/***************/

typedef item processing_context;
typedef item external_context;
typedef item module_context;
/* typedef struct { 
  module_context mc;
  itemlist stmts;
  } query_context; */
typedef item query_context;
typedef itemlist atomicvalue_list;
typedef itemlist attribute_list;
typedef itemlist nodelist;

/**************/
/* Exceptions */
/**************/
typedef int glx_err; 
extern char *glx_error_string;

/* C-API initialization */

extern void glx_init();

/*****************************/
/* Atomic value constructors */
/*****************************/

extern glx_err glx_xs_string(char *s, xs_string *); 
extern glx_err glx_xs_boolean(int b, xs_boolean *);
extern glx_err glx_xs_int(int i, xs_int *); 
extern glx_err glx_xs_integer(int i, xs_integer *); 
extern glx_err glx_xs_decimal(int i, xs_decimal *); 
extern glx_err glx_xs_float(double f, xs_float *); 
extern glx_err glx_xs_double(double f, xs_double *); 
extern glx_err glx_xs_anyURI(char* u, xs_anyURI *);
extern glx_err glx_xs_QName(module_context c, char *qname, xs_QName *);

/*********************/
/* Node constructors */
/*********************/

extern glx_err glx_document(char *uri, itemlist nodes, document *);
extern glx_err glx_element(xs_QName qname, attribute_list attributes, itemlist nodes, xs_QName qname1, element *);
extern glx_err glx_text(xs_string str, text *);
extern glx_err glx_attribute(xs_QName qname, xs_string string, xs_QName qname1, attribute *);
extern glx_err glx_comment(xs_string string, comment *);
extern glx_err glx_processing_instruction(xs_string string, xs_string string1, processing_instruction *);

/**********************/
/* Accessors on items */
/**********************/

extern glx_err glx_string(item i, char **p);
extern glx_err glx_item_kind(item i, char **p);
extern glx_err glx_get_node(item i, node *);
extern glx_err glx_get_atomicvalue(item i, atomicvalue *); 
extern glx_err glx_get_element(item i, element *);
extern glx_err glx_get_attribute(item i, attribute *);

/**********************/
/* Accessors on nodes */
/**********************/

extern glx_err glx_parent(node n, nodelist *);
extern glx_err glx_children(node n, nodelist *);

/* Note: glx_base_uri returns empty list or singleton list containing xs_anyURI value */
extern glx_err glx_base_uri(node n, atomicvalue_list *);

/* Note: glx_node_name returns empty list or singleton list containing xs_string value */
extern glx_err glx_node_name(node n, atomicvalue_list *); 

extern glx_err glx_node_kind(node n, char **);
extern glx_err glx_typed_value(node n, atomicvalue_list *);
extern glx_err glx_attributes(node n, attribute_list *);

/************************/
/* Conversion functions */
/************************/

extern glx_err glx_string_of_atomicvalue(atomicvalue, char **);
extern glx_err glx_boolean_of_xs_boolean(xs_boolean, int *);
extern glx_err glx_int_of_xs_int(xs_int, int *);

/* Following two functions may raise run-time errors if integer or
   decimal cannot be cast to an int. */
extern glx_err glx_integer_of_xs_integer(xs_integer, int *);
extern glx_err glx_decimal_of_xs_decimal(xs_decimal, int *);

extern glx_err glx_float_of_xs_float(xs_float, double *);
extern glx_err glx_float_of_xs_double(xs_double, double *);

/**************************/
/* Document I/O functions */
/**************************/

extern glx_err glx_load_document(char* filename, nodelist *); 
extern glx_err glx_load_document_validate(module_context mod_ctxt, char* filename, nodelist *);

extern glx_err glx_serialize_to_string(itemlist xml, char **); 
extern glx_err glx_serialize_to_output_channel(itemlist xml); 
extern glx_err glx_serialize_to_file(char *filename, itemlist xml);

/**********************/
/* Evaluation context */
/**********************/

extern glx_err glx_default_processing_context(processing_context *);
extern glx_err glx_load_standard_library(processing_context pc, module_context *);
extern glx_err glx_import_library_module(module_context mod_ctxt, char *filename, module_context *);
extern glx_err glx_import_main_module(module_context mod_ctxt, char *filename, query_context *);
extern glx_err glx_import_main_module_from_string(module_context mod_ctxt, char *str, query_context *);

extern glx_err glx_build_external_context(itemlist ctxt_item, char **varnames, itemlist values[], int nargs, external_context *);
extern glx_err glx_add_external_context(module_context mod_ctxt, external_context ext_ctxt, module_context *);
extern glx_err glx_eval_global_variables(module_context mod_ctxt, module_context *);

/********************/
/* Query evaluation */
/********************/

extern glx_err glx_eval_statement_from_string(module_context c, char* querystring, itemlist *);
extern glx_err glx_eval_query_function(module_context c, char *funname, itemlist *params, int nargs, itemlist *);
extern glx_err glx_eval_statements(query_context q, itemlist *);

extern glx_err glx_eval_statement_with_context_item_from_document (char *filename, char *querystr, itemlist *); 
extern glx_err glx_eval_statement_with_context_item_from_xml(item ctxt_item, char *querystr, itemlist *); 
extern glx_err glx_eval_statement_from_file_with_context_item_from_xml(item ctxt_item, char *queryfile, itemlist *); 
extern glx_err glx_eval_statement_with_variables_from_xml(char **varname, itemlist values[], int nargs, char *querystr, itemlist *); 


/* extern glx_err glx_eval_statement_with_context_item_from_memory
   (char *buffer, char *querystr, itemlist *); */

/* extern glx_err glx_eval_statement_with_context_item_from_url
   (char *url, char *querystr, itemlist *); */


