]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
Adds functions "file_lines_{fold,iter}" to the Unixext module. These functions implem...
authorJonathan Knowles <jonathan.knowles@eu.citrix.com>
Fri, 20 Nov 2009 14:22:16 +0000 (14:22 +0000)
committerJonathan Knowles <jonathan.knowles@eu.citrix.com>
Fri, 20 Nov 2009 14:22:16 +0000 (14:22 +0000)
Signed-off-by: Jonathan Knowles <jonathan.knowles@eu.citrix.com>
stdext/unixext.ml
stdext/unixext.mli

index ae0df7434332aace666c4bd966f4529991cdeb1e..e1a3734e7437bbef3b7056710856cf8c37d28f8e 100644 (file)
@@ -85,18 +85,31 @@ let daemonize () =
                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 =
index d35d51bc709848aee3e352513cbbf51ecd95cb28..8008bde556d8ddd5e1fc3e0e9d8cda69be485380 100644 (file)
@@ -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 pidfile_read : string -> int option
 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