new_field(pos);
}
-// FIXME: Use elsewhere
static unsigned char *get_field(fields_t *fields, int i, int *len)
{
struct field *f = fields_nth(fields, i);
static void csv_write(struct format *fmt)
{
- unsigned char *line = line_first(&in_line);
- int n = fields_count(&out_fields);
- for (int i=0; i<n; i++) {
- struct field *f = fields_nth(&out_fields, i);
+ for (int i=0; i < fields_count(&out_fields); i++) {
+ int len;
+ unsigned char *p = get_field(&out_fields, i, &len);
+
int need_quotes = 0;
if (fmt->quote >= 0) {
need_quotes = fmt->always_quote;
- for (int j=0; !need_quotes && j < f->len; j++) {
- int c = line[f->start_pos + j];
- if (c == fmt->fs || c == fmt->quote)
+ for (int j=0; !need_quotes && j < len; j++) {
+ if (p[j] == fmt->fs || p[j] == fmt->quote)
need_quotes = 1;
}
}
putchar_unlocked(fmt->fs);
if (need_quotes)
putchar_unlocked(fmt->quote);
- for (int j=0; j < f->len; j++) {
- int c = line[f->start_pos + j];
+ for (int j=0; j < len; j++) {
+ int c = p[j];
if (c == fmt->fs && !need_quotes)
warn(fmt, "Field separator found inside field and quoting is turned off.");
if (c == fmt->quote)
FILE *tf = fmt->tmp_file;
for (int i = 0; i < fields_count(&out_fields); i++) {
- struct field *f = fields_nth(&out_fields, i);
- if (f->len < 0xfe)
- putc_unlocked(f->len, tf);
+ int len;
+ unsigned char *p = get_field(&out_fields, i, &len);
+
+ if (len < 0xfe)
+ putc_unlocked(len, tf);
else {
putc_unlocked(0xfe, tf);
- putc_unlocked((f->len >> 24) & 0xff, tf);
- putc_unlocked((f->len >> 16) & 0xff, tf);
- putc_unlocked((f->len >> 8) & 0xff, tf);
- putc_unlocked(f->len & 0xff, tf);
+ putc_unlocked((len >> 24) & 0xff, tf);
+ putc_unlocked((len >> 16) & 0xff, tf);
+ putc_unlocked((len >> 8) & 0xff, tf);
+ putc_unlocked(len & 0xff, tf);
}
- unsigned char *p = line_nth(&in_line, f->start_pos);
- for (int j = 0; j < f->len; j++)
+ while (len--)
putc_unlocked(*p++, tf);
}
putc_unlocked(0xff, tf);