summaryrefslogtreecommitdiff
path: root/src/util.ml
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.ml')
-rw-r--r--src/util.ml57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/util.ml b/src/util.ml
new file mode 100644
index 0000000..93f2c93
--- /dev/null
+++ b/src/util.ml
@@ -0,0 +1,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
+
+