From 917a64c4870cb95c0969ecc56197889f6672b9d1 Mon Sep 17 00:00:00 2001 From: rich Date: Mon, 27 Oct 2008 21:57:48 +0000 Subject: [PATCH] Fix 'trim ~left' and add csvtool trim subcommand. --- Makefile.config | 4 ++-- csv.ml | 10 +++++----- csvtool.ml | 29 ++++++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Makefile.config b/Makefile.config index fa530d6..b074d8b 100644 --- a/Makefile.config +++ b/Makefile.config @@ -1,4 +1,4 @@ -# $Id: Makefile.config,v 1.4 2007-04-23 16:41:22 rich Exp $ +# $Id: Makefile.config,v 1.5 2008-10-27 21:57:48 rich Exp $ PACKAGE := ocaml-csv -VERSION := 1.1.6 +VERSION := 1.1.7 diff --git a/csv.ml b/csv.ml index 0b6e649..2237ee8 100644 --- a/csv.ml +++ b/csv.ml @@ -1,6 +1,6 @@ (* csv.ml - comma separated values parser * - * $Id: csv.ml,v 1.14 2006-11-24 15:49:24 rich Exp $ + * $Id: csv.ml,v 1.15 2008-10-27 21:57:48 rich Exp $ *) (* The format of CSV files: @@ -216,10 +216,10 @@ let trim ?(top=true) ?(left=true) ?(right=true) ?(bottom=true) csv = 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 diff --git a/csvtool.ml b/csvtool.ml index 8c54081..4e067b7 100644 --- a/csvtool.ml +++ b/csvtool.ml @@ -1,5 +1,5 @@ (* Handy tool for managing CSV files. - * $Id: csvtool.ml,v 1.10 2006-12-05 22:40:38 rich Exp $ + * $Id: csvtool.ml,v 1.11 2008-10-27 21:57:48 rich Exp $ *) open Printf @@ -385,6 +385,21 @@ let cmd_join ~input_sep ~output_sep ~chan colspec1 colspec2 files = ) csv in save_out ~separator:output_sep chan csv +let rec cmd_trim ~input_sep ~output_sep ~chan (top, left, right, bottom) files = + let csv = List.concat (List.map (load ~separator:input_sep) files) in + let csv = trim ~top ~left ~right ~bottom csv in + save_out ~separator:output_sep chan csv + +and trim_flags flags = + let set c = + try ignore (String.index flags c); true with Not_found -> false + in + let top = set 't' in + let left = set 'l' in + let right = set 'r' in + let bottom = set 'b' in + (top, left, right, bottom) + (* Process the arguments. *) let usage = "csvtool - Copyright (C) 2005-2006 Richard W.M. Jones, Merjis Ltd. @@ -475,6 +490,15 @@ Commands: Example: csvtool square input.csv > input-square.csv + trim [tlrb]+ + Trim empty cells at the top/left/right/bottom of the CSV file. + + Example: + csvtool trim t input.csv # trims empty rows at the top only + csvtool trim tb input.csv # trims empty rows at the top & bottom + csvtool trim lr input.csv # trims empty columns at left & right + csvtool trim tlrb input.csv # trims empty rows/columns all around + sub r c rows cols Take a square subset of the CSV, top left at row r, column c, which is rows deep and cols wide. 'r' and 'c' count from 1, or @@ -651,6 +675,9 @@ let () = cmd_drop ~input_sep ~output_sep ~chan rows files | "call" :: command :: files -> cmd_call ~input_sep ~output_sep ~chan command files + | "trim" :: flags :: files -> + let flags = trim_flags flags in + cmd_trim ~input_sep ~output_sep ~chan flags files | _ -> prerr_endline (Sys.executable_name ^ " --help for usage"); exit 2 -- 1.8.3.1