Source

ocaml_monad_io / iO_Lwt.ml

Diff from to

File iO_Lwt.ml

 value error = Lwt.fail;
 
 value catch = Lwt.catch;
+
+
+(*************************)
+
+
+value wrap_exn _place = error
+;
+
+
+value wrap1 place f = fun a ->
+  catch (fun () -> f a)
+  (wrap_exn place)
+;
+
+value wrap2 place f = fun a b ->
+  catch (fun () -> f a b)
+  (wrap_exn place)
+;
+
+value wrap4 place f = fun a b c d ->
+  catch (fun () -> f a b c d)
+  (wrap_exn place)
+;
+
+value read_into = wrap4 "read_into" Lwt_io.read_into;
+
+value error = Lwt.fail;
+
+type output_channel = Lwt_io.output_channel;
+value stdout = Lwt_io.stdout;
+value write = wrap2 "write" Lwt_io.write;
+
+type input_channel = Lwt_io.input_channel;
+
+value open_in = wrap1 "open_in" (
+  fun fn ->
+    Lwt_io.open_file
+      ~mode:Lwt_io.input
+      ~flags:[Unix.O_RDONLY]
+     fn
+  )
+;
+
+value close_in x = wrap1 "close_in" Lwt_io.close x;
+
+value runIO x : [= `Ok of 'a | `Error of exn ] =
+  try `Ok (Lwt_main.run x)
+  with [e -> `Error e]
+;
+
+value with_file_in_bin filename func =
+  Lwt_io.with_file ~mode:Lwt_io.input filename func
+;
+
+value with_file_out_bin filename func =
+  Lwt_io.with_file ~mode:Lwt_io.output filename func
+;