summaryrefslogtreecommitdiff
path: root/src/util.ml
blob: c4bbd96c0602e4ab2fb4c698342ced117763e620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(** The unavoidable module for functions that don't fit anywhere else *)

(** Find a child node in xml-lite *)
let find_xml_child name xml =
    let find_aux e =
        match e with
        | Xml.Element (name', _, _) when name' = name -> true
        | _ -> false
    in
    match xml with
    | Xml.Element (_, _, children) -> Vylist.find find_aux children
    | Xml.PCData _ -> None

(** Convert a list of strings to a string of unquoted, space separated words *)
let string_of_list ss =
    let rec aux xs acc =
        match xs with
        | [] -> acc
        | x :: xs' -> aux xs' (Printf.sprintf "%s %s" acc x)
    in
    match ss with
    | [] -> ""
    | x :: xs -> Printf.sprintf "%s%s" x (aux xs "")

(** Convert a list of strings to JSON *)
let json_of_list ss =
    let ss = List.map (fun x -> `String x) ss in
    Yojson.Safe.to_string (`List ss)

(** Convert a relative path to an absolute path based on the current working directory *)
let absolute_path relative_path =
    FilePath.make_absolute (Sys.getcwd ()) relative_path

(** Makes a hex dump of a byte string *)
let hexdump b =
    let dump = ref "" in
    Bytes.iter (fun c -> dump := Char.code c |> Printf.sprintf "%s %02x" !dump) b;
    !dump