]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CA-44731: Add file_blocks_fold which folds over the data in a file, block at a time.
authorDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:07:50 +0000 (12:07 +0100)
committerDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:07:50 +0000 (12:07 +0100)
Signed-off-by: David Scott <dave.scott@eu.citrix.com>
stdext/unixext.ml
stdext/unixext.mli

index 1e162cdda51a274be6cf69ecc0339c855791bac3..4cb0d02e8b32d54565c359a05916dab3f3e1e4a6 100644 (file)
@@ -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 =
index a07aff67df8f1bbf59a3bce6ff369af3ba825e7d..814ede0eba39d90b361701092b3ffe3dae3fc05d 100644 (file)
@@ -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