]> mj.ucw.cz Git - xsv.git/commitdiff
Added --ws mode
authorMartin Mares <mj@ucw.cz>
Mon, 23 Jul 2012 18:19:22 +0000 (20:19 +0200)
committerMartin Mares <mj@ucw.cz>
Mon, 23 Jul 2012 18:19:22 +0000 (20:19 +0200)
xsv.c

diff --git a/xsv.c b/xsv.c
index fa7964da7727328dfc087dd1c080bdcb4944d4b3..38f1fdeac88088859fae44f2835f080f971a4fbe 100644 (file)
--- a/xsv.c
+++ b/xsv.c
@@ -160,6 +160,31 @@ static void csv_write(void)
        putchar('\n');
 }
 
+static int ws_read(void)
+{
+       int ws = 0;
+       for (;;) {
+               int c = getchar();
+               if (c < 0)
+                       return !!fields_count(&in_fields);
+               if (c == '\r')
+                       continue;
+               if (c == '\n')
+                       return 1;
+               if (c == ' ' || c == '\t' || c == '\f') {
+                       ensure_field();
+                       if (!ws)
+                               new_field();
+                       ws++;
+               } else {
+                       ensure_field();
+                       *line_push(&in_line) = c;
+                       in_field->len++;
+                       ws = 0;
+               }
+       }
+}
+
 /*** Field selection ***/
 
 struct selector {
@@ -276,6 +301,10 @@ static void set_format(int format_id)
                        f->write_line = csv_write;
                        break;
                case FORM_WS:
+                       f->fs = ' ';
+                       f->quote = -1;
+                       f->read_line = ws_read;
+                       f->write_line = csv_write;
                        break;
        }