summaryrefslogtreecommitdiff
path: root/ocamltest/variables.ml
blob: 706dc63d3430f5985dc2cd574e371d4976ee9793 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*             Sebastien Hinderer, projet Gallium, INRIA Paris            *)
(*                                                                        *)
(*   Copyright 2016 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(* Definition of environment variables *)

type value = string

type exporter = value -> string * string

type t = {
  variable_name : string;
  variable_description : string;
  variable_exporter : exporter
}

let compare v1 v2 = String.compare v1.variable_name v2.variable_name

exception Empty_variable_name

exception Variable_already_registered of string

exception No_such_variable of string

let default_exporter varname value = (varname, value)

let make (name, description) =
  if name="" then raise Empty_variable_name else {
    variable_name = name;
    variable_description = description;
    variable_exporter = default_exporter name
  }

let make_with_exporter exporter (name, description) =
  if name="" then raise Empty_variable_name else {
    variable_name = name;
    variable_description = description;
    variable_exporter = exporter
  }

let name_of_variable v = v.variable_name

let description_of_variable v = v.variable_description

let (variables : (string, t) Hashtbl.t) = Hashtbl.create 10

let register_variable variable =
  if Hashtbl.mem variables variable.variable_name
  then raise (Variable_already_registered variable.variable_name)
  else Hashtbl.add variables variable.variable_name variable

let find_variable variable_name =
  try Some (Hashtbl.find variables variable_name)
  with Not_found -> None

let string_of_binding variable value =
  let (varname, value) = variable.variable_exporter value in
  Printf.sprintf "%s=%s" varname value

let get_registered_variables () =
  let f _variable_name variable variable_list = variable::variable_list in
  List.sort compare (Hashtbl.fold f variables [])