int quiet;
int (*read_line)(struct format *fmt);
void (*write_line)(struct format *fmt);
- int needs_two_passes;
+ int needs_stats;
// CSV backend:
int always_quote;
// Temporary file backend:
FILE *tmp_file;
- intarray_t column_widths;
// Table backend:
int table_sep;
return chars;
}
+/*** Field statistics ***/
+
+static intarray_t column_widths;
+
+static void update_stats(void)
+{
+ for (int i = 0; i < fields_count(&in_fields); i++) {
+ struct field *f = fields_nth(&in_fields, i);
+ intarray_t *w = &column_widths;
+
+ while (i >= intarray_count(w))
+ *intarray_push(w) = 0;
+ int fw = field_chars(f);
+ if (*intarray_nth(w, i) < fw)
+ *intarray_nth(w, i) = fw;
+ }
+}
+
/*** CSV/TSV back-end */
static int csv_read(struct format *fmt)
printf("%*s", fmt->table_sep, "");
struct field *f = fields_nth(&in_fields, i);
int fw = field_chars(f);
- int cw = *intarray_nth(&in_format->column_widths, i);
+ int cw = *intarray_nth(&column_widths, i);
if (fw > cw) {
warn(fmt, "Internal error: Wrongly calculated column width (%d > %d)", fw, cw);
cw = fw;
unsigned char *p = line_nth(&in_line, f->start_pos);
for (int j = 0; j < f->len; j++)
putc_unlocked(*p++, tf);
-
- intarray_t *w = &fmt->column_widths;
- while (i >= intarray_count(w))
- *intarray_push(w) = 0;
- int fw = field_chars(f);
- if (*intarray_nth(w, i) < fw)
- *intarray_nth(w, i) = fw;
}
putc_unlocked(0xff, tf);
fields_reset(&out_fields);
select_fields();
+ if (out_format->needs_stats)
+ update_stats();
out_format->write_line(out_format);
if (ferror_unlocked(stdout))
die("I/O error when writing standard input");
out_format->read_line = tmp_read;
out_format->write_line = tmp_write;
out_format->tmp_file = tmpfile();
+ out_format->needs_stats = final_format->needs_stats;
one_pass();
// Pass 2: Set up reader of intermediate format
in_format = out_format;
rewind(in_format->tmp_file);
out_format = final_format;
+ out_format->needs_stats = 0;
one_pass();
fclose(in_format->tmp_file);
}
break;
case FORM_TABLE:
f->write_line = table_write;
- f->needs_two_passes = 1;
+ f->needs_stats = 1;
f->table_sep = 2;
break;
}
}
finish_parse_selectors();
- if (out_format->needs_two_passes)
+ if (out_format->needs_stats)
two_pass();
else
one_pass();