(* csv.ml - comma separated values parser
*
- * $Id: csv.ml,v 1.12 2006-10-18 14:56:12 rich Exp $
+ * $Id: csv.ml,v 1.15 2008-10-27 21:57:48 rich Exp $
*)
(* The format of CSV files:
open Printf
+(* Uncomment the next line to enable Extlib's List function. These
+ * avoid stack overflows on really huge CSV files.
+ *)
+(*open ExtList*)
+
type t = string list list
exception Bad_CSV_file of string
List.rev !csv
let load ?separator filename =
- let chan = open_in filename in
+ let chan, close =
+ match filename with
+ | "-" -> stdin, false
+ | filename -> open_in filename, true in
let csv = load_in ?separator chan in
- close_in chan;
- csv
+ if close then close_in chan;
+ csv
let trim ?(top=true) ?(left=true) ?(right=true) ?(bottom=true) csv =
let rec empty_row = function
let remove_left_col =
List.map (function [] -> [] | x :: xs -> xs) in
let rec loop csv =
- if empty_left_col csv then (
- let csv = remove_left_col csv in
- loop csv
- ) else csv
+ if empty_left_col csv then
+ remove_left_col csv
+ else
+ csv
in
let csv = if left then loop csv else csv in