mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
bugfix in CCIO.read_all and CCIO.read_chunks
This commit is contained in:
parent
78ff35154b
commit
059528257a
1 changed files with 21 additions and 15 deletions
36
core/CCIO.ml
36
core/CCIO.ml
|
|
@ -82,20 +82,16 @@ let with_in ?(mode=0o644) ?(flags=[]) filename f =
|
||||||
close_in ic;
|
close_in ic;
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
let read_chunks ?(size=256) ic =
|
let read_chunks ?(size=1024) ic =
|
||||||
let buf = Buffer.create size in
|
let buf = Bytes.create size in
|
||||||
let eof = ref false in
|
let eof = ref false in
|
||||||
let next() =
|
let next() =
|
||||||
if !eof then None
|
if !eof then None
|
||||||
else try
|
else
|
||||||
Buffer.add_channel buf ic size;
|
let n = input ic buf 0 size in
|
||||||
let s = Buffer.contents buf in
|
if n = 0
|
||||||
Buffer.clear buf;
|
then None
|
||||||
Some s
|
else Some (Bytes.sub_string buf 0 n)
|
||||||
with End_of_file ->
|
|
||||||
let s = Buffer.contents buf in
|
|
||||||
eof := true;
|
|
||||||
if s="" then None else Some s
|
|
||||||
in
|
in
|
||||||
next
|
next
|
||||||
|
|
||||||
|
|
@ -121,14 +117,24 @@ let read_lines_l ic =
|
||||||
List.rev !l
|
List.rev !l
|
||||||
|
|
||||||
let read_all ic =
|
let read_all ic =
|
||||||
let buf = Buffer.create 256 in
|
let buf = ref (Bytes.create 256) in
|
||||||
|
let len = ref 0 in
|
||||||
try
|
try
|
||||||
while true do
|
while true do
|
||||||
Buffer.add_channel buf ic 1024
|
(* resize *)
|
||||||
|
if !len = Bytes.length !buf then (
|
||||||
|
let buf' = Bytes.create (2* !len) in
|
||||||
|
Bytes.blit !buf 0 buf' 0 !len;
|
||||||
|
buf := buf'
|
||||||
|
);
|
||||||
|
assert (Bytes.length !buf > !len);
|
||||||
|
let n = input ic !buf !len (Bytes.length !buf - !len) in
|
||||||
|
len := !len + n;
|
||||||
|
if n = 0 then raise Exit; (* exhausted *)
|
||||||
done;
|
done;
|
||||||
assert false (* never reached*)
|
assert false (* never reached*)
|
||||||
with End_of_file ->
|
with Exit ->
|
||||||
Buffer.contents buf
|
Bytes.sub_string !buf 0 !len
|
||||||
|
|
||||||
let with_out ?(mode=0o644) ?(flags=[]) filename f =
|
let with_out ?(mode=0o644) ?(flags=[]) filename f =
|
||||||
let oc = open_out_gen flags mode filename in
|
let oc = open_out_gen flags mode filename in
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue