+/*** Table back-end ***/
+
+static void table_write(void)
+{
+ for (int i = 0; i < fields_count(&in_fields); i++) {
+ if (i)
+ printf("%*s", out_format->table_sep, "");
+ struct field *f = fields_nth(&in_fields, i);
+ int w = *intarray_nth(&in_format->column_widths, i);
+ if (f->len > w) {
+ warn(out_format, "Internal error: Wrongly calculated column width (%d > %d)", f->len, w);
+ w = f->len;
+ }
+ int j = 0;
+ unsigned char *p = line_nth(&in_line, f->start_pos);
+ while (j < f->len) {
+ putchar(*p++);
+ j++;
+ }
+ while (j < w) {
+ putchar(' ');
+ j++;
+ }
+ }
+ putchar('\n');
+}
+
+/*** Temporary file back-end ***/
+
+static int tmp_read(void)
+{
+ FILE *tf = in_format->tmp_file;
+
+ for (;;) {
+ int c = fgetc(tf);
+ if (c < 0)
+ return 0;
+ if (c == 0xff)
+ return 1;
+ if (c == 0xfe) {
+ c = fgetc(tf);
+ c = (c << 8) | fgetc(tf);
+ c = (c << 8) | fgetc(tf);
+ c = (c << 8) | fgetc(tf);
+ }
+ new_field(line_count(&in_line));
+ in_field->len = c;
+ while (c--) {
+ int x = fgetc(tf);
+ if (x < 0) {
+ warn(in_format, "Truncated temporary file");
+ return 0;
+ }
+ *line_push(&in_line) = x;
+ }
+ }
+}
+
+static void tmp_write(void)
+{
+ FILE *tf = out_format->tmp_file;
+
+ for (int i = 0; i < fields_count(&in_fields); i++) {
+ struct field *f = fields_nth(&in_fields, i);
+ if (f->len < 0xfe)
+ fputc(f->len, tf);
+ else {
+ fputc(0xfe, tf);
+ fputc((f->len >> 24) & 0xff, tf);
+ fputc((f->len >> 16) & 0xff, tf);
+ fputc((f->len >> 8) & 0xff, tf);
+ fputc(f->len & 0xff, tf);
+ }
+
+ unsigned char *p = line_nth(&in_line, f->start_pos);
+ for (int j = 0; j < f->len; j++)
+ fputc(*p++, tf);
+
+ intarray_t *w = &out_format->column_widths;
+ while (i >= intarray_count(w))
+ *intarray_push(w) = 0;
+ if (*intarray_nth(w, i) < f->len)
+ *intarray_nth(w, i) = f->len;
+ }
+ fputc(0xff, tf);
+}
+