summaryrefslogtreecommitdiff
path: root/src/util.ml
blob: 93f2c93f2f7fda26c943766919de0284ea2f160e (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


(* Programmed by Jedidiah Barber *)
(* Licensed under the Sunset License v1.0 *)


exception Not_an_integer of string



let ( @@ ) f g x = f (g x)

let ( |> ) f x = f x



let call_with_open_input_file ~filename ~func =
  let ic = open_in filename in
  let res =
    try func ic
    with exn -> close_in ic; raise exn in
  close_in ic;
  res



let call_with_open_output_file ~filename ~func =
  let oc = open_out filename in
  let res =
    try func oc
    with exn -> close_out oc; raise exn in
  close_out oc;
  res



let read_integer_list args =
  let convert x =
    try Z.of_string x
    with Invalid_argument _ ->
      raise (Not_an_integer x) in
  let blank_is_zero x =
    if x = "" then "0" else x in
  (Array.map convert) @@
  Array.of_list @@
  (List.map (blank_is_zero @@ String.trim)) @@
  (String.split_on_char ',') |> args



let read_sequence_file filename =
  let reader handle =
    let line = input_line handle in
    read_integer_list line
  in call_with_open_input_file ~filename:filename ~func:reader