From: David Scott Date: Tue, 12 Oct 2010 11:08:05 +0000 (+0100) Subject: CA-44731: Add buffer_of_file and bigbuffer_of_file to read files into Buffer.t and... X-Git-Url: http://xenbits.xensource.com/gitweb?a=commitdiff_plain;h=ed05b7362ec24fe3af74624ca82a4f267f34c445;p=xcp%2Fxen-api-libs.git CA-44731: Add buffer_of_file and bigbuffer_of_file to read files into Buffer.t and Bigbuffer.t. Signed-off-by: David Scott --- diff --git a/stdext/unixext.ml b/stdext/unixext.ml index 4cb0d02..55b6a1c 100644 --- a/stdext/unixext.ml +++ b/stdext/unixext.ml @@ -135,6 +135,12 @@ let with_directory dir f = Unix.closedir dh; r +let buffer_of_file file_path = + file_blocks_fold 1024 (fun b s -> Buffer.add_string b s; b) (Buffer.create 1024) file_path + +let bigbuffer_of_file file_path = + file_blocks_fold 1024 (fun b s -> Bigbuffer.append_string b s; b) (Bigbuffer.make ()) file_path + (** Read whole file from specified fd *) let read_whole_file size_hint block_size fd = let filebuf = Buffer.create size_hint in diff --git a/stdext/unixext.mli b/stdext/unixext.mli index 814ede0..47e68d0 100644 --- a/stdext/unixext.mli +++ b/stdext/unixext.mli @@ -36,6 +36,13 @@ val file_blocks_fold: int -> ('a -> string -> 'a) -> 'a -> string -> 'a (** Alias for function [file_lines_iter]. *) val readfile_line : (string -> 'a) -> string -> unit + +(** [buffer_of_file file] returns a Buffer.t containing the contents of [file] *) +val buffer_of_file : string -> Buffer.t + +(** [bigbuffer_of_file file] returns a Bigbuffer.t containing the contents of [file] *) +val bigbuffer_of_file : string -> Bigbuffer.t + val read_whole_file : int -> int -> Unix.file_descr -> string val read_whole_file_to_string : string -> string val atomic_write_to_file : string -> Unix.file_perm -> (Unix.file_descr -> 'a) -> 'a