Implemented:
[ocaml-csv.git] / csvtool.ml
index 0b82a4b..1cee449 100644 (file)
@@ -1,5 +1,5 @@
 (* 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
@@ -42,6 +42,21 @@ let cmd_height ~csv ~chan () =
 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.
@@ -70,6 +85,13 @@ Commands:
   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.
@@ -144,6 +166,10 @@ let () =
         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")
   );