(* Handy tool for managing CSV files.
- * $Id: csvtool.ml,v 1.1 2005-05-24 13:52:50 rich Exp $
+ * $Id: csvtool.ml,v 1.2 2005-11-25 14:06:58 rich Exp $
*)
open Printf
let cmd_readable ~csv ~chan () =
save_out_readable chan csv
+let cmd_square ~separator ~csv ~chan () =
+ let csv = square csv in
+ save_out ~separator chan csv
+
+let cmd_sub ~separator ~csv ~chan args =
+ let r, c, rows, cols =
+ match args with
+ | [ r; c; rows; cols ] ->
+ int_of_string r, int_of_string c,
+ int_of_string rows, int_of_string cols
+ | _ ->
+ failwith "unknown arguments to 'sub' command" in
+ let csv = sub r c rows cols csv in
+ save_out ~separator chan csv
+
(* Process the arguments. *)
let usage =
"csvtool - Copyright (C) 2005 Richard W.M. Jones, Merjis Ltd.
readable
Print the input CSV in a readable format.
+ square
+ Make the CSV square, so all rows have the same length.
+
+ sub r c rows cols
+ Take a square subset of the CSV, top left at row r, column c (counting
+ from 0), which is rows deep and cols wide.
+
Input and output files:
csvtool normally processes its input from stdin and writes its output
to stdout. Use the -i and -o options to override this behaviour.
cmd_height ~csv:input ~chan ()
| "readable" ->
cmd_readable ~csv:input ~chan ()
+ | "square" ->
+ cmd_square ~separator:output_sep ~csv:input ~chan ()
+ | "sub" ->
+ cmd_sub ~separator:output_sep ~csv:input ~chan args
| _ -> prerr_endline (Sys.executable_name ^ " --help for usage")
);