(* csv.ml - comma separated values parser
*
- * $Id: csv.ml,v 1.10 2006-04-21 12:44:42 rich Exp $
+ * $Id: csv.ml,v 1.13 2006-11-24 09:43:15 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
let c = compare_row [] y in
if c <> 0 then c else compare [] ys
+(* Concatenate - arrange left to right. *)
+let rec concat = function
+ | [] -> []
+ | [csv] -> csv
+ | left_csv :: csvs ->
+ (* Concatenate the remaining CSV files. *)
+ let right_csv = concat csvs in
+
+ (* Set the height of the left and right CSVs to the same. *)
+ let nr_rows = max (lines left_csv) (lines right_csv) in
+ let left_csv = set_rows nr_rows left_csv in
+ let right_csv = set_rows nr_rows right_csv in
+
+ (* Square off the left CSV. *)
+ let left_csv = square left_csv in
+
+ (* Prepend the right CSV rows with the left CSV rows. *)
+ List.map (
+ fun (left_row, right_row) -> List.append left_row right_row
+ ) (List.combine left_csv right_csv)
+
let to_array csv =
Array.of_list (List.map Array.of_list csv)
try List.combine r1 r2
with
Invalid_argument "List.combine" ->
- failwith "Csv.save_out_readable: internal error: length r1 = %d, length r2 = %d" (List.length r1) (List.length r2) in
+ failwith (sprintf "Csv.save_out_readable: internal error: length r1 = %d, length r2 = %d" (List.length r1) (List.length r2)) in
List.map (fun ((a : int), (b : int)) -> max a b) rp
in
List.fold_left max2rows lengths_row1 lengths_rest in