package hxd

  1. Overview
  2. Docs
Hexdump in OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

hxd-0.4.0.tbz
sha256=10030b7226a17504471809ad5afc0c200721264c5c6dd3d595d248040a718058
sha512=7cc6911729d6b030d10b25a6222649bf1275dc18efcaefd170138addf744c790f786ff2772aaae65071b112a00ed6bd51d0c3667d49a185f0399739b5e8cb693

doc/src/hxd.lwt/hxd_lwt.ml.html

Source file hxd_lwt.ml

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
open Hxd
module Lwt_scheduler = Make (struct type +'a t = 'a Lwt.t end)

type error = |

let ( <.> ) f g x = f (g x)
let ok x = Ok x

let lwt_bind x f =
  let open Lwt.Infix in
  Lwt_scheduler.(inj (prj x >>= (prj <.> f)))

let lwt = {bind= lwt_bind; return= (fun x -> Lwt_scheduler.inj (Lwt.return x))}

let lseek =
  let lseek _ pos mode =
    let res =
      match pos, mode with 0, `SET -> Lwt.return_ok 0 | _, _ -> assert false
    in
    Lwt_scheduler.inj res in
  {lseek}

type input = unit -> (string * int * int) option Lwt.t
type output = (string * int * int) option -> unit Lwt.t

let recv ic buffer ~off ~len =
  let open Lwt.Infix in
  let res =
    ic.contents () >>= function
    | None -> Lwt.return_ok 0
    | Some (res, off', len') ->
      let len'' = (min : int -> int -> int) len len' in
      Bytes.blit_string res off' buffer off len''
      ; (if len'' < len' then
           (* XXX(dinosaure): deferred inputs. *)
           let consumed = ref false in
           ic.contents <-
             (fun () ->
               if !consumed then ic.contents ()
               else (
                 consumed := true
                 ; Lwt.return (Some (res, off' + len'', len' - len'')))))
      ; Lwt.return_ok len'' in
  Lwt_scheduler.inj res

let send oc buffer ~off ~len =
  let open Lwt.Infix in
  let res = oc (Some (buffer, off, len)) >|= fun () -> ok len in
  Lwt_scheduler.inj res

let generate configuration ic oc ppf =
  let ic = {contents= ic} in
  let res = generate configuration lwt recv send ic oc lseek (`Absolute 0) ppf in
  let open Lwt.Infix in
  Lwt_scheduler.prj res >>= function Ok () -> oc None | Error (_ : error) -> .