mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
Bij.TrBencode with more functions;
Bencode.parse_resume function to resume parsing of buffer
This commit is contained in:
parent
e1064845a7
commit
5e0052af14
4 changed files with 50 additions and 4 deletions
|
|
@ -305,6 +305,8 @@ let parse dec s i len =
|
||||||
(* state machine *)
|
(* state machine *)
|
||||||
parse_rec dec
|
parse_rec dec
|
||||||
|
|
||||||
|
let parse_resume d = parse_rec d
|
||||||
|
|
||||||
let reset dec =
|
let reset dec =
|
||||||
dec.l <- 0;
|
dec.l <- 0;
|
||||||
dec.c <- 0;
|
dec.c <- 0;
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,10 @@ val parse : decoder -> string -> int -> int -> parse_result
|
||||||
It can return an error, a value or just [ParsePartial] if
|
It can return an error, a value or just [ParsePartial] if
|
||||||
more input is needed *)
|
more input is needed *)
|
||||||
|
|
||||||
|
val parse_resume : decoder -> parse_result
|
||||||
|
(** Resume where the previous call to {!parse} stopped (may have
|
||||||
|
returned a value while some input is not processed) *)
|
||||||
|
|
||||||
val reset : decoder -> unit
|
val reset : decoder -> unit
|
||||||
(** Reset the decoder to its pristine state, ready to parse something
|
(** Reset the decoder to its pristine state, ready to parse something
|
||||||
different. Before that, {! rest} and {! rest_size} can be used
|
different. Before that, {! rest} and {! rest_size} can be used
|
||||||
|
|
|
||||||
41
bij.ml
41
bij.ml
|
|
@ -201,8 +201,43 @@ module TrBencode = struct
|
||||||
let b = B.of_string s in
|
let b = B.of_string s in
|
||||||
decode ~bij b
|
decode ~bij b
|
||||||
|
|
||||||
(* TODO *)
|
let read ~bij ic =
|
||||||
let read ~bij ic = failwith "read: not implemented"
|
let d = B.mk_decoder () in
|
||||||
|
let buf = String.create 256 in
|
||||||
|
let rec read_chunk() =
|
||||||
|
let n = input ic buf 0 (String.length buf) in
|
||||||
|
if n = 0
|
||||||
|
then raise (DecodingError "unexpected EOF")
|
||||||
|
else match B.parse d buf 0 n with
|
||||||
|
| B.ParsePartial -> read_chunk()
|
||||||
|
| B.ParseError s -> raise (DecodingError s)
|
||||||
|
| B.ParseOk b -> decode ~bij b
|
||||||
|
in
|
||||||
|
read_chunk()
|
||||||
|
|
||||||
let write ~bij x oc = failwith "write: not implemented"
|
let read_stream ~bij ic =
|
||||||
|
let d = B.mk_decoder () in
|
||||||
|
let buf = String.create 256 in
|
||||||
|
let rec try_parse n = match B.parse d buf 0 n with
|
||||||
|
| B.ParsePartial -> read_chunk()
|
||||||
|
| B.ParseError s -> raise (DecodingError s)
|
||||||
|
| B.ParseOk b -> Some (decode ~bij b)
|
||||||
|
and read_chunk() =
|
||||||
|
let n = input ic buf 0 (String.length buf) in
|
||||||
|
if n = 0
|
||||||
|
then match B.parse_resume d with
|
||||||
|
| B.ParsePartial -> None
|
||||||
|
| B.ParseError s -> raise (DecodingError s)
|
||||||
|
| B.ParseOk b -> Some (decode ~bij b)
|
||||||
|
else try_parse n
|
||||||
|
in
|
||||||
|
Stream.from (fun _ -> read_chunk())
|
||||||
|
|
||||||
|
let write ~bij oc x =
|
||||||
|
let b = encode ~bij x in
|
||||||
|
B.to_chan oc b;
|
||||||
|
flush oc
|
||||||
|
|
||||||
|
let write_stream ~bij oc str =
|
||||||
|
Stream.iter (fun x -> write ~bij oc x) str
|
||||||
end
|
end
|
||||||
|
|
|
||||||
7
bij.mli
7
bij.mli
|
|
@ -89,7 +89,12 @@ module TrBencode : sig
|
||||||
val of_string : bij:'a t -> string -> 'a
|
val of_string : bij:'a t -> string -> 'a
|
||||||
|
|
||||||
val read : bij:'a t -> in_channel -> 'a
|
val read : bij:'a t -> in_channel -> 'a
|
||||||
|
(** Read a single value from the channel *)
|
||||||
|
|
||||||
val write : bij:'a t -> 'a t -> out_channel -> unit
|
val read_stream : bij:'a t -> in_channel -> 'a Stream.t
|
||||||
|
|
||||||
|
val write : bij:'a t -> out_channel -> 'a -> unit
|
||||||
|
|
||||||
|
val write_stream : bij:'a t -> out_channel -> 'a Stream.t -> unit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue