]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
CA-44731: Add buffer_of_file and bigbuffer_of_file to read files into Buffer.t and...
authorDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:08:05 +0000 (12:08 +0100)
committerDavid Scott <dave.scott@eu.citrix.com>
Tue, 12 Oct 2010 11:08:05 +0000 (12:08 +0100)
Signed-off-by: David Scott <dave.scott@eu.citrix.com>
stdext/unixext.ml
stdext/unixext.mli

index 4cb0d02e8b32d54565c359a05916dab3f3e1e4a6..55b6a1cfeb61bb608a6a6a6fef926017bae98b1d 100644 (file)
@@ -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
index 814ede0eba39d90b361701092b3ffe3dae3fc05d..47e68d0dcc18b8c63e786de9fc93cfb2aa53eb25 100644 (file)
@@ -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