[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-API] [PATCH] Adds functions "file_lines_{fold, iter}" to the Unixext module. These functions implement the standard "fold" and "iter" functions for all the lines in a file
# HG changeset patch # User Jonathan Knowles <jonathan.knowles@xxxxxxxxxxxxx> # Date 1258726936 0 # Node ID dd25a8923a9a5bc082d848e57b0e3eee34ad5e30 # Parent 4d0f0db47314e509928e05d578e9c2eb58d89e88 Adds functions "file_lines_{fold,iter}" to the Unixext module. These functions implement the standard "fold" and "iter" functions for all the lines in a file. Signed-off-by: Jonathan Knowles <jonathan.knowles@xxxxxxxxxxxxx> diff -r 4d0f0db47314 -r dd25a8923a9a stdext/unixext.ml --- a/stdext/unixext.ml Tue Nov 17 10:55:17 2009 +0000 +++ b/stdext/unixext.ml Fri Nov 20 14:22:16 2009 +0000 @@ -85,18 +85,31 @@ end | _ -> exit 0 -(** Run a function over every line in a file *) -let readfile_line fn fname = - let fin = open_in fname in +let file_lines_fold f start file_path = + let input = open_in file_path in + let rec fold accumulator = + let line = + try Some (input_line input) + with End_of_file -> None in + match line with + | Some line -> fold (f accumulator line) + | None -> accumulator in + finally + (fun () -> fold start) + (fun () -> close_in input) + +let file_lines_iter f file_path = + let input = open_in file_path in try while true do - let line = input_line fin in - fn line - done; - close_in fin; + let line = input_line input in + f line + done with - | End_of_file -> close_in fin - | exn -> close_in fin; raise exn + | End_of_file -> close_in input + | exn -> close_in input; raise exn + +let readfile_line = file_lines_iter (** open a file, and make sure the close is always done *) let with_file file mode perms f = diff -r 4d0f0db47314 -r dd25a8923a9a stdext/unixext.mli --- a/stdext/unixext.mli Tue Nov 17 10:55:17 2009 +0000 +++ b/stdext/unixext.mli Fri Nov 20 14:22:16 2009 +0000 @@ -11,6 +11,8 @@ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. *) +(** A collection of extensions to the [Unix] module. *) + external _exit : int -> unit = "unix_exit" val unlink_safe : string -> unit val mkdir_safe : string -> Unix.file_perm -> unit @@ -20,6 +22,15 @@ val daemonize : unit -> unit val with_file : string -> Unix.open_flag list -> Unix.file_perm -> (Unix.file_descr -> 'a) -> 'a val with_directory : string -> (Unix.dir_handle -> 'a) -> 'a + +(** Folds function [f] over every line in the file at [file_path] using the +starting value [start]. *) +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 + +(** Alias for function [file_lines_iter]. *) val readfile_line : (string -> 'a) -> string -> unit val read_whole_file : int -> int -> Unix.file_descr -> string val read_whole_file_to_string : string -> string Attachment:
xen-api-libs.hg.patch _______________________________________________ xen-api mailing list xen-api@xxxxxxxxxxxxxxxxxxx http://lists.xensource.com/mailman/listinfo/xen-api
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |