(** {2 Text File Operations} Functions to manage files that are assumed to consist of a series of lines terminated by newlines. Terminating newline is not included, on the assumption that whether one is missing at the EOF is not significant. *) (** Return a stream of lines *) let to_stream path = let chan = open_in path in Stream.from (fun _ -> try Some (input_line chan) with End_of_file -> close_in chan; None | exn -> close_in chan; raise exn) (** Return a list of a lines *) let to_list path = Stream2.to_list (to_stream path) (** Write the lines to a file *) let of_list lines path = let chan = open_out path in List.iter (fun line -> output_string chan (line ^ "\n")) lines; close_out chan (** Modify a file "in place" by loading it into memory, applying f to each line, and writing the result to the same path. *) let map f path = let lines = to_list path in let chan = open_out path in List.iter (fun line -> output_string chan ((f line) ^ "\n")) lines; close_out chan let iter f path = Stream.iter f (Linestream.of_file path)