(* csv.ml - comma separated values parser
*
- * $Id: csv.ml,v 1.7 2005-11-25 14:06:58 rich Exp $
+ * $Id: csv.ml,v 1.8 2006-02-15 13:25:58 rich Exp $
*)
(* The format of CSV files:
let save_out_readable chan csv =
(* Escape all the strings in the CSV file first. *)
+ (* XXX Why are we doing this? I commented it out anyway.
let csv = List.map (List.map String.escaped) csv in
-
- let csv = square csv in
+ *)
(* Find the width of each column. *)
let widths =
+ (* Don't consider rows with only a single element - typically
+ * long titles.
+ *)
+ let csv = List.filter (function [_] -> false | _ -> true) csv in
+
+ (* Square the CSV file - makes the next step simpler to implement. *)
+ let csv = square csv in
+
match csv with
| [] -> []
| r :: _ ->
| i -> f (); repeat f (i-1)
in
List.iter (
- fun row ->
- let row = List.combine widths row in
- List.iter (
- fun (width, cell) ->
- output_string chan cell;
- let n = String.length cell in
- repeat (fun () -> output_char chan ' ') (width - n + 1)
- ) row;
- output_char chan '\n'
+ function
+ | [cell] -> (* Single column. *)
+ output_string chan cell;
+ output_char chan '\n'
+ | row -> (* Other. *)
+ let row = List.combine widths row in
+ List.iter (
+ fun (width, cell) ->
+ output_string chan cell;
+ let n = String.length cell in
+ repeat (fun () -> output_char chan ' ') (width - n + 1)
+ ) row;
+ output_char chan '\n'
) csv
let print_readable = save_out_readable stdout