culr/lib/pipes.ml
2024-11-26 14:48:39 +11:00

90 lines
2.9 KiB
OCaml

open Angstrom.Buffered
open Parse
let print_buf buf = print_endline ("buffer has " ^ Bigstringaf.to_string buf)
(* let read_all () =
let parser = Angstrom.Unbuffered.parse culr_parse
and buf = Bigstringaf.create 4096 in
In_channel.set_binary_mode stdin true;
print_endline "doing it";
let rec loop parser =
parser |> function
(* START CONDITION *)
| Partial { committed = 0; continue } -> (
match In_channel.input_bigarray stdin buf 0 4096 with
| 0 ->
print_endline "got 0 end";
raise End_of_file
| _ ->
print_buf buf;
print_endline "got partial 0";
loop (continue buf ~off:0 ~len:4096 Incomplete) (* CONTINUE *))
| Partial { continue; _ } -> (
match In_channel.input_bigarray stdin buf 0 4096 with
| 0 ->
print_endline "got + end";
raise End_of_file
| _ ->
print_buf buf;
print_endline "got partial +";
loop (continue buf ~off:0 ~len:4096 Incomplete))
| Done (_, _) ->
print_endline "got done";
raise End_of_file
| Fail (_, _, _) -> print_endline "got fail"
in
try loop parser
with End_of_file -> (
print_endline "eof";
parser |> state_to_option |> function
| Some chunks ->
print_endline "chunks:";
List.iter (fun x -> debug_print x) chunks
| None ->
print_endline "no chunks";
()) *)
(* let read_all_3 () =
let parser = parse ~initial_buffer_size:4096 culr_parse
and buf = Bigstringaf.create 4096 in
In_channel.set_binary_mode stdin true;
print_endline "doing it";
let rec loop parser =
Bigarray.Array1.fill buf '\x00';
match In_channel.input_bigarray stdin buf 0 4096 with
| 0 -> (
print_endline "got eof";
parser
|> (function Partial cont -> cont `Eof | _ -> parser)
|> state_to_option
|> function
| Some chunks -> List.iter debug_print chunks
| None -> print_endline "nothing")
| _ -> (
print_endline "got buf:";
print_buf buf;
parser |> function
| Partial cont -> loop (cont (`Bigstring buf))
| Done (_, chunks) ->
print_endline "got done";
List.iter debug_print chunks
| Fail (_, _, _) -> print_endline "got fail")
in
loop parser *)
(* let rec loop state =
state |> function
| Partial {committed; continue} -> continue
| Done(_,_) -> ()
| Fail (_,_,_) -> () *)
let read_all () =
In_channel.set_binary_mode stdin true;
Angstrom_unix.parse_many ~buf_size:4096 culr_parse (fun c -> debug_print c) In_channel.stdin
|> function
| unparsed, Ok _ ->
print_endline ("didn't parse this much: " ^ string_of_int unparsed.len ^ "\nunparsed:\n" ^ (Bigstringaf.substring unparsed.buf ~off:unparsed.off ~len:unparsed.len) );
()
| _ -> ()