diff options
Diffstat (limited to 'lib/bindings.ml')
-rw-r--r-- | lib/bindings.ml | 60 |
1 files changed, 57 insertions, 3 deletions
diff --git a/lib/bindings.ml b/lib/bindings.ml index 6d19a76..925c6ee 100644 --- a/lib/bindings.ml +++ b/lib/bindings.ml @@ -3,7 +3,9 @@ open Foreign module CT = Config_tree -let make_config_tree name = Ctypes.Root.create (Config_tree.make name) +let to_json_str = fun s -> `String s + +let make_config_tree name = Ctypes.Root.create (CT.make name) let destroy c_ptr = Root.release c_ptr @@ -21,7 +23,7 @@ let set_add_value c_ptr path value = let ct = Root.get c_ptr in let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in try - let new_ct = CT.set ct path (Some value) Config_tree.AddValue in + let new_ct = CT.set ct path (Some value) CT.AddValue in Root.set c_ptr new_ct; 0 (* return 0 *) with CT.Duplicate_value -> 1 @@ -46,7 +48,7 @@ let delete_value c_ptr path value = let ct = Root.get c_ptr in let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in try - let new_ct = Config_tree.delete ct path (Some value) in + let new_ct = CT.delete ct path (Some value) in Root.set c_ptr new_ct; 0 (* return 0 *) with CT.No_such_value -> 1 @@ -59,6 +61,52 @@ let delete_node c_ptr path = Root.set c_ptr new_ct; 0 (* return 0 *) +let set_tag c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + try + Root.set c_ptr (CT.set_ephemeral ct path true); + 0 (* return 0 *) + with _ -> 1 + +let is_tag c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + if (CT.is_ephemeral ct path) then 1 else 0 + +let exists c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + if (Vytree.exists ct path) then 1 else 0 + +let list_nodes c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + try + let nodes = Vytree.children_of_path ct path in + let nodes_json = `List (List.map to_json_str nodes) in + Yojson.Safe.to_string nodes_json + with _ -> Yojson.Safe.to_string `Null + +let return_value c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + try + Yojson.Safe.to_string (`String (CT.get_value ct path)) + with + | CT.Node_has_no_value -> Yojson.Safe.to_string (`String "") + | _ -> Yojson.Safe.to_string `Null + +let return_values c_ptr path = + let ct = Root.get c_ptr in + let path = Pcre.split ~rex:(Pcre.regexp "\\s+") path in + let to_json_str = fun s -> `String s in + try + let values = CT.get_values ct path in + let values_json = `List (List.map to_json_str values) in + Yojson.Safe.to_string values_json + with _ -> Yojson.Safe.to_string `Null + module Stubs(I : Cstubs_inverted.INTERNAL) = struct @@ -72,5 +120,11 @@ struct let () = I.internal "set_valueless" ((ptr void) @-> string @-> returning int) set_valueless let () = I.internal "delete_value" ((ptr void) @-> string @-> string @-> returning int) delete_value let () = I.internal "delete_node" ((ptr void) @-> string @-> returning int) delete_node + let () = I.internal "set_tag" ((ptr void) @-> string @-> returning int) set_tag + let () = I.internal "is_tag" ((ptr void) @-> string @-> returning int) is_tag + let () = I.internal "exists" ((ptr void) @-> string @-> returning int) exists + let () = I.internal "list_nodes" ((ptr void) @-> string @-> returning string) list_nodes + let () = I.internal "return_value" ((ptr void) @-> string @-> returning string) return_value + let () = I.internal "return_values" ((ptr void) @-> string @-> returning string) return_values end |