From 222a2de0fc0d7d9ca048fedaf0df194a066e2a83 Mon Sep 17 00:00:00 2001 From: David Scott Date: Tue, 12 Oct 2010 12:07:50 +0100 Subject: [PATCH] CA-44731: Add file_blocks_fold which folds over the data in a file, block at a time. Signed-off-by: David Scott --- stdext/unixext.ml | 14 ++++++++++++++ stdext/unixext.mli | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/stdext/unixext.ml b/stdext/unixext.ml index 1e162cd..4cb0d02 100644 --- a/stdext/unixext.ml +++ b/stdext/unixext.ml @@ -112,6 +112,20 @@ let with_file file mode perms f = Unix.close fd; r +(** [file_blocks_fold block_size f start file_path] folds [f] over blocks (strings) + from the file [file_path] with initial value [start] *) +let file_blocks_fold block_size f start file_path = + with_file file_path [ Unix.O_RDONLY ] 0 + (fun fd -> + let block = String.create block_size in + let rec fold acc = + let n = Unix.read fd block 0 block_size in + (* Consider making the interface explicitly use Substrings *) + let s = if n = block_size then block else String.sub block 0 n in + if n = 0 then acc else fold (f acc s) in + fold start + ) + let with_directory dir f = let dh = Unix.opendir dir in let r = diff --git a/stdext/unixext.mli b/stdext/unixext.mli index a07aff6..814ede0 100644 --- a/stdext/unixext.mli +++ b/stdext/unixext.mli @@ -30,6 +30,10 @@ val file_lines_fold : ('a -> string -> 'a) -> 'a -> string -> 'a (** Applies function [f] to every line in the file at [file_path]. *) val file_lines_iter : (string -> unit) -> string -> unit +(** [file_blocks_fold block_size f start file_path] folds [f] over blocks (strings) + from the file [file_path] with initial value [start] *) +val file_blocks_fold: int -> ('a -> string -> 'a) -> 'a -> string -> 'a + (** Alias for function [file_lines_iter]. *) val readfile_line : (string -> 'a) -> string -> unit val read_whole_file : int -> int -> Unix.file_descr -> string -- 2.39.5