Preliminary support for the grammar.
[SXSI/xpathcomp.git] / src / OCamlDriver.cpp
index 8eb3a22..cd7fed4 100644 (file)
 
 #include "XMLTree.h"
 #include "XMLTreeBuilder.h"
+#include "Grammar.h"
 #include "Utils.h"
 #include "common_stub.hpp"
 
-extern "C" {
-#include <stdio.h>
-}
-
 #define CAMLRAISEMSG(msg) (sxsi_raise_msg((char*) (msg)))
 
-#define XMLTREE(x) (Obj_val<XMLTree>(x))
+#define XMLTREE(x) (Obj_val<XMLTree*>(x))
+
+#define HSET(x) (Obj_val<TagIdSet*>(x))
 
-#define HSET(x) (Obj_val<TagIdSet>(x))
+#define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder*>(x))
 
-#define XMLTREEBUILDER(x) (Obj_val<XMLTreeBuilder>(x))
+#define GRAMMAR(x) (Obj_val<Grammar*>(x))
 
 
 #define TREENODEVAL(i) ((treeNode) (Int_val(i)))
@@ -44,6 +43,7 @@ extern "C" {
 extern "C" {
 #include <sys/time.h>
 #include <sys/resource.h>
+#include <stdio.h>
 }
 
 
@@ -56,8 +56,9 @@ extern "C" value caml_xml_tree_builder_create(value unit)
 {
   CAMLparam1(unit);
   CAMLlocal1(result);
-  result = sxsi_alloc_custom<XMLTreeBuilder>();
-  Obj_val<XMLTreeBuilder>(result) = new XMLTreeBuilder();
+  result = sxsi_alloc_custom<XMLTreeBuilder*>();
+  Obj_val<XMLTreeBuilder*>(result) = new XMLTreeBuilder();
+
   CAMLreturn(result);
 }
 
@@ -102,8 +103,8 @@ extern "C" value caml_xml_tree_builder_close_document(value vbuilder)
   XMLTree * tree = XMLTREEBUILDER(vbuilder)->CloseDocument();
   if (tree == NULL)
     CAMLRAISEMSG("CloseDocument");
-  result = sxsi_alloc_custom<XMLTree>();
-  Obj_val<XMLTree>(result) = tree;
+  result = sxsi_alloc_custom<XMLTree*>();
+  Obj_val<XMLTree*>(result) = tree;
   CAMLreturn (result);
 }
 
@@ -158,9 +159,10 @@ extern "C"  value caml_xml_tree_load(value fd, value name, value load_tc,value s
   CAMLlocal1(result);
   XMLTree * tree;
   try {
+
     tree = XMLTree::Load(Int_val(fd),Bool_val(load_tc),Int_val(sf), String_val(name));
-    result = sxsi_alloc_custom<XMLTree>();
-    Obj_val<XMLTree>(result) = tree;
+    result = sxsi_alloc_custom<XMLTree*>();
+    Obj_val<XMLTree*>(result) = tree;
     CAMLreturn(result);
   }
   catch (const std::exception& e){ CAMLRAISEMSG(e.what()); }
@@ -280,6 +282,10 @@ NoAlloc extern "C"  value caml_xml_tree_next_element(value tree, value node){
   return (Val_int(XMLTREE(tree)->NextElement(TREENODEVAL(node))));
 }
 
+NoAlloc extern "C"  value caml_xml_tree_next_node_before(value tree, value node, value ctx){
+  return (Val_int(XMLTREE(tree)->NextNodeBefore(TREENODEVAL(node), TREENODEVAL(ctx))));
+}
+
 NoAlloc extern "C"  value caml_xml_tree_prev_sibling(value tree, value node){
   return (Val_int(XMLTREE(tree)->PrevSibling(TREENODEVAL(node))));
 }
@@ -416,8 +422,8 @@ NoAlloc extern "C"  value caml_unordered_set_length(value hset){
 extern "C"  value caml_unordered_set_alloc(value unit){
   CAMLparam1(unit);
   CAMLlocal1(hset);
-  hset = sxsi_alloc_custom<TagIdSet>();
-  Obj_val<TagIdSet>(hset) = new TagIdSet();
+  hset = sxsi_alloc_custom<TagIdSet*>();
+  Obj_val<TagIdSet*>(hset) = new TagIdSet();
   CAMLreturn (hset);
 }
 
@@ -888,3 +894,25 @@ BV_QUERY(suffix, Suffix)
 BV_QUERY(equals, Equals)
 BV_QUERY(contains, Contains)
 BV_QUERY(lessthan, LessThan)
+
+
+
+//////////////////////////////////////////// Grammar stuff
+
+extern "C"  value caml_grammar_load(value file, value load_bp)
+{
+  CAMLparam2(file, load_bp);
+  CAMLlocal1(result);
+  Grammar *grammar;
+  int f1 = Int_val(file);
+  int f2 = dup(f1);
+  FILE * fd = fdopen(f2, "r");
+  if (fd == NULL)
+    CAMLRAISEMSG("Error opening grammar file");
+  grammar = Grammar::load(fd, Bool_val(load_bp));
+  fclose(fd);
+  result = sxsi_alloc_custom<Grammar*>();
+  Obj_val<Grammar*>(result) = grammar;
+  CAMLreturn(result);
+}
+