Various improvements:
[SXSI/xpathcomp.git] / src / utils_stub.cpp
1 #include "utils_stub.hpp"
2
3 extern "C" {
4 #define CAML_NAME_SPACE
5
6 #include <caml/mlvalues.h>
7 #include <caml/alloc.h>
8 #include <caml/memory.h>
9 #include <caml/callback.h>
10 #include <caml/fail.h>
11 #include <caml/custom.h>
12 #include <caml/bigarray.h>
13 }
14 #include <ctype.h>
15
16 extern "C" value caml_clz(value i)
17 {
18   return Val_long( ((sizeof(unsigned long)*8) - __builtin_clzl(Long_val(i))) - 1);
19 }
20
21 extern "C" value caml_leading_bit(value i)
22 {
23   return Val_long( ( 1 << (sizeof(unsigned long)*8 - __builtin_clzl(Long_val(i)) - 1)));
24 }
25
26
27 static char * rtrim(char *str)
28 {
29   char *ptr;
30   int   len;
31
32   len = strlen(str);
33   for (ptr = str + len - 1; ptr >= str && isspace((int)*ptr ); --ptr);
34   ptr[1] = '\0';
35
36   return str;
37 }
38
39 static char * ltrim(char *str)
40 {
41   char *ptr;
42   int  len;
43
44   for (ptr = str; *ptr && isspace((int)*ptr); ++ptr);
45
46   len = strlen(ptr);
47   memmove(str, ptr, len + 1);
48  
49   return str;
50 }
51 extern "C" value caml_trim(value s)
52 {
53   CAMLparam1(s);
54   CAMLlocal1(res);
55   char * ptr;
56   char * str = String_val(s);
57   ptr = rtrim(str);
58   str = ltrim(ptr);
59   res = caml_copy_string(str);
60   CAMLreturn(res);
61 }
62
63 xml_tree::tag_t*& TAGLIST(value x)
64 {
65   return Obj_val<xml_tree::tag_t*>(x);
66 }
67
68
69
70 static void finalize_tag_list(value x)
71 {
72   xml_tree::tag_t * t = TAGLIST(x);
73   delete [] t;
74 }
75
76 extern "C" value caml_tag_list_alloc(value length)
77 {
78   CAMLparam1(length);
79   CAMLlocal1(tlist);
80   tlist = sxsi_alloc_custom<xml_tree::tag_t*>();
81   TAGLIST(tlist) = new xml_tree::tag_t[Int_val(length)];
82   CAMLreturn (tlist);
83 }
84
85 NoAlloc extern "C" value caml_tag_list_set(value tl, value i, value v)
86 {
87   TAGLIST(tl)[Int_val(i)] = Int_val(v);
88   return Val_unit;
89 }