1 #include "xml-tree.hpp"
2 #include "utils_stub.hpp"
7 static xml_tree*& XMLTREE(value v)
9 return Obj_val<xml_tree*>(v);
12 static xml_tree::node_t TREENODE(value i)
14 return static_cast<xml_tree::node_t>(Int_val(i));
17 static xml_tree::tag_t TAG(value i)
19 return static_cast<xml_tree::tag_t>(Int_val(i));
22 ML_BINDING value caml_xml_tree_save(value tree, value fd, value prefix)
24 CAMLparam3(tree, fd, prefix);
25 XMLTREE(tree)->save(Int_val(fd), String_val(prefix));
26 CAMLreturn (Val_unit);
30 caml_xml_tree_load(value fd, value prefix, value load_tc, value sf)
32 CAMLparam4(fd, prefix, load_tc, sf);
37 tree = xml_tree::load(Int_val(fd),
42 result = sxsi_alloc_custom<xml_tree*>();
43 XMLTREE(result) = tree;
46 catch (const std::exception& e){ sxsi_raise_msg(e.what()); }
47 catch (std::string msg){ sxsi_raise_msg(msg.c_str()); }
48 catch (char const * msg){ sxsi_raise_msg(msg); };
53 NoAlloc ML_BINDING value caml_xml_tree_root(value tree)
55 return (Val_int(XMLTREE(tree)->root()));
58 NoAlloc ML_BINDING value caml_xml_tree_size(value tree)
60 return (Val_int(XMLTREE(tree)->size()));
63 NoAlloc ML_BINDING value caml_xml_tree_num_tags(value tree)
65 return (Val_int(XMLTREE(tree)->num_tags()));
68 NoAlloc ML_BINDING value caml_xml_tree_subtree_size(value tree, value node)
70 return (Val_int(XMLTREE(tree)->subtree_size(TREENODE(node))));
73 NoAlloc ML_BINDING value
74 caml_xml_tree_subtree_tags(value tree, value node, value tag)
76 return (Val_int(XMLTREE(tree)->subtree_tags(TREENODE(node),
80 NoAlloc ML_BINDING value caml_xml_tree_subtree_elements(value tree,
83 return (Val_int(XMLTREE(tree)->subtree_elements(TREENODE(node))));
86 NoAlloc ML_BINDING value caml_xml_tree_is_leaf(value tree, value node){
87 return (Val_bool(XMLTREE(tree)->is_leaf(TREENODE(node))));
90 NoAlloc ML_BINDING value
91 caml_xml_tree_is_ancestor(value tree, value node1, value node2)
93 return (Val_bool(XMLTREE(tree)->is_ancestor(TREENODE(node1),
97 NoAlloc ML_BINDING value
98 caml_xml_tree_is_child(value tree, value node1, value node2)
100 return (Val_bool(XMLTREE(tree)->is_child(TREENODE(node1),
104 NoAlloc ML_BINDING value caml_xml_tree_is_first_child(value tree, value node)
106 return (Val_bool(XMLTREE(tree)->is_first_child(TREENODE(node))));
109 NoAlloc ML_BINDING value
110 caml_xml_tree_is_right_descendant(value tree, value x, value y)
112 return (Val_bool(XMLTREE(tree)->is_right_descendant(TREENODE(x),
116 NoAlloc ML_BINDING value caml_xml_tree_num_children(value tree, value node)
118 return (Val_int(XMLTREE(tree)->num_children(TREENODE(node))));
121 NoAlloc ML_BINDING value caml_xml_tree_child_pos(value tree, value node)
123 return (Val_int(XMLTREE(tree)->child_pos(TREENODE(node))));
126 NoAlloc ML_BINDING value caml_xml_tree_depth(value tree, value node)
128 return (Val_int(XMLTREE(tree)->depth(TREENODE(node))));
131 NoAlloc ML_BINDING value caml_xml_tree_preorder(value tree, value node)
133 return (Val_int(XMLTREE(tree)->preorder(TREENODE(node))));
136 NoAlloc ML_BINDING value caml_xml_tree_postorder(value tree, value node)
138 return (Val_int(XMLTREE(tree)->postorder(TREENODE(node))));
141 NoAlloc ML_BINDING value caml_xml_tree_tag(value tree, value node)
143 return (Val_int(XMLTREE(tree)->tag(TREENODE(node))));
146 NoAlloc ML_BINDING value caml_xml_tree_parent(value tree, value node)
148 return (Val_int(XMLTREE(tree)->parent(TREENODE(node))));
151 NoAlloc ML_BINDING value caml_xml_tree_child(value tree, value node, value idx)
153 return (Val_int(XMLTREE(tree)->child(TREENODE(node), Int_val(idx))));
156 NoAlloc ML_BINDING value caml_xml_tree_first_child(value tree, value node)
158 return (Val_int(XMLTREE(tree)->first_child(TREENODE(node))));
161 NoAlloc ML_BINDING value caml_xml_tree_first_element(value tree, value node)
163 return (Val_int(XMLTREE(tree)->first_element(TREENODE(node))));
166 NoAlloc ML_BINDING value caml_xml_tree_last_child(value tree, value node)
168 return (Val_int(XMLTREE(tree)->last_child(TREENODE(node))));
171 NoAlloc ML_BINDING value caml_xml_tree_next_sibling(value tree, value node)
173 return (Val_int(XMLTREE(tree)->next_sibling(TREENODE(node))));
176 NoAlloc ML_BINDING value caml_xml_tree_next_element(value tree, value node)
178 return (Val_int(XMLTREE(tree)->next_element(TREENODE(node))));
181 NoAlloc ML_BINDING value caml_xml_tree_prev_sibling(value tree, value node)
183 return (Val_int(XMLTREE(tree)->prev_sibling(TREENODE(node))));
186 NoAlloc ML_BINDING value
187 caml_xml_tree_tagged_child(value tree, value node, value tag)
189 return (Val_int(XMLTREE(tree)->tagged_child(TREENODE(node),
193 NoAlloc ML_BINDING value
194 caml_xml_tree_select_child(value tree, value node, value tags)
196 return (Val_int(XMLTREE(tree)->select_child(TREENODE(node), TAGLIST(tags))));
199 NoAlloc ML_BINDING value
200 caml_xml_tree_tagged_sibling(value tree, value node, value tag)
202 return (Val_int(XMLTREE(tree)->tagged_sibling(TREENODE(node),
206 NoAlloc ML_BINDING value
207 caml_xml_tree_select_sibling(value tree, value node, value tags)
209 return (Val_int(XMLTREE(tree)->select_sibling(TREENODE(node),
213 NoAlloc ML_BINDING value
214 caml_xml_tree_tagged_descendant(value tree, value node, value tag)
216 return (Val_int(XMLTREE(tree)->tagged_descendant(TREENODE(node),
220 NoAlloc ML_BINDING value
221 caml_xml_tree_tagged_next(value tree, value node, value tag)
223 return (Val_int(XMLTREE(tree)->tagged_next(TREENODE(node),
227 NoAlloc ML_BINDING value
228 caml_xml_tree_select_descendant(value tree, value node, value tags)
230 return (Val_int(XMLTREE(tree)->select_descendant(TREENODE(node),
234 NoAlloc ML_BINDING value caml_xml_tree_tagged_following_before(value tree,
239 return (Val_int(XMLTREE(tree)->tagged_following_before(TREENODE(node),
241 TREENODE(closing))));
244 NoAlloc ML_BINDING value caml_xml_tree_select_following_before(value tree,
249 return (Val_int(XMLTREE(tree)->select_following_before(TREENODE(node),
251 TREENODE(closing))));
256 ML_BINDING value caml_xml_tree_get_text_collection(value tree)
260 text = sxsi_alloc_custom<TextCollection*>();
261 Obj_val<TextCollection*>(text) = XMLTREE(tree)->get_text_collection();
265 NoAlloc ML_BINDING value caml_xml_tree_closing(value tree, value node)
267 return (Val_int(XMLTREE(tree)->closing(TREENODE(node))));
270 NoAlloc ML_BINDING value caml_xml_tree_nullt(value unit){
271 return (Val_int(xml_tree::NIL));
275 ML_BINDING value caml_xml_tree_print(value tree, value node, value fd)
277 CAMLparam3(tree, node, fd);
278 XMLTREE(tree)->print(TREENODE(node), Int_val(fd));
279 CAMLreturn(Val_unit);
283 ML_BINDING value caml_xml_tree_get_tag_name(value tree, value tag)
285 CAMLparam2(tree, tag);
287 const char* s = XMLTREE(tree)->get_tag_name_by_ref(TAG(tag));
288 res = caml_copy_string(s);
292 NoAlloc ML_BINDING value caml_xml_tree_flush(value tree, value fd)
294 XMLTREE(tree)->flush(Int_val(fd));
298 ML_BINDING value caml_xml_tree_register_tag(value tree, value str)
300 CAMLparam2(tree, str);
302 res = Val_int(XMLTREE(tree)->register_tag(String_val(str)));
307 /** Full reporting into a bit vector
309 static void pr_vector(std::vector<int32_t> v)
311 for(auto i = v.begin(); i!= v.end(); ++i)
312 fprintf(stderr, "%i ", *i);
313 fprintf(stderr, "\n");
315 static std::vector<int32_t> sort_results(std::vector<int32_t> v, xml_tree *t)
317 std::vector<int32_t> res;
318 std::sort(v.begin(), v.end());
320 for(auto i = v.begin(); i != v.end(); ++i){
323 if (i == v.end()) return res;
326 res.push_back(t->parent_node(prev));
331 #define BV_QUERY(pref, Pref) \
332 ML_BINDING value caml_text_collection_## pref ##_bv(value tree, value str, value dobvv){ \
333 CAMLparam3(tree, str, dobvv); \
334 CAMLlocal3(res, res_bv, res_array); \
336 uchar * cstr = (uchar *) strdup(String_val(str)); \
337 std::vector<int32_t> uresults = XMLTREE(tree)->Pref(cstr); \
338 std::vector<int32_t> results = sort_results(uresults, XMLTREE(tree)); \
339 bool dobv = Bool_val(dobvv); \
340 res_bv = caml_alloc_string(dobv ? ((XMLTREE(tree)->size() / 4) + 2) : 0); \
341 unsigned long slen = caml_string_length(res_bv); \
343 memset(&(Byte(res_bv,0)), 0, slen); \
344 res_array = caml_alloc_shr(results.size(), 0); \
345 for (unsigned int i = 0; i < results.size(); ++i) { \
348 Byte(res_bv, j >> 3) |= (1 << (j & 7)); \
350 caml_initialize(&Field(res_array, i), Val_int(j)); \
352 fprintf(stderr, "Raw results: %zu, Sorted results %zu\n", uresults.size(), results.size()); \
354 res = caml_alloc(2, 0); \
355 Store_field(res, 0, res_bv); \
356 Store_field(res, 1, res_array); \
361 BV_QUERY(prefix, prefix)
362 BV_QUERY(suffix, suffix)
363 BV_QUERY(equals, equals)
364 BV_QUERY(contains, contains)
365 BV_QUERY(lessthan, less_than)