]> mj.ucw.cz Git - xsv.git/commitdiff
Let column selection update the header
authorMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 15:11:44 +0000 (17:11 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 15:11:44 +0000 (17:11 +0200)
xsv.c

diff --git a/xsv.c b/xsv.c
index f980634d214036ef5e20199ea4e178bd4c235680..971ae58f3ba971ca566130e35d8be0572971c3e8 100644 (file)
--- a/xsv.c
+++ b/xsv.c
@@ -24,6 +24,9 @@
 #define UNUSED
 #endif
 
+static void select_fields(void);
+static void select_all_fields(void);
+
 /*** General functions ***/
 
 static void NONRET die(char *msg, ...)
@@ -611,20 +614,22 @@ static void write_header(void)
                return;
        }
 
+       int want_select_fields = 0;
        if (out_format->set_field_names) {
                struct field_names *fn = xmalloc_zero(sizeof(*fn));
                out_format->field_names = fn;
                add_field_names(fn, out_format->set_field_names);
-       } else if (in_format->field_names)
+       } else if (in_format->field_names) {
                out_format->field_names = in_format->field_names;
-       else
+               want_select_fields = 1;
+       } else
                die("Output header requested, but no field names specified");
 
        line_reset(&in_line);
-       fields_reset(&out_fields);
+       fields_reset(&in_fields);
        struct field_names *fn = out_format->field_names;
        for (int i = 0; i < stringarray_count(&fn->names); i++) {
-               struct field *f = fields_push(&out_fields);
+               struct field *f = fields_push(&in_fields);
                f->start_pos = line_count(&in_line);
                f->len = 0;
                char *s = *stringarray_nth(&fn->names, i);
@@ -634,6 +639,11 @@ static void write_header(void)
                }
        }
 
+       if (want_select_fields)
+               select_fields();
+       else
+               select_all_fields();
+
        // This is tricky: when we are formatting a table, field names are normally
        // calculated in pass 1, but the header is written in pass 2, so we have to
        // update column statistics, because field name can be too wide to fit.