From: Martin Mares Date: Tue, 24 Jul 2012 14:00:44 +0000 (+0200) Subject: Use get_field() where useful X-Git-Tag: v1.0~27 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=a421fad8b4473210203c91cff0f8245f37acbde3;p=xsv.git Use get_field() where useful --- diff --git a/xsv.c b/xsv.c index 872090f..32da504 100644 --- a/xsv.c +++ b/xsv.c @@ -184,7 +184,6 @@ static void ensure_field(int pos) 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); @@ -305,16 +304,15 @@ static int is_ws(int c) static void csv_write(struct format *fmt) { - unsigned char *line = line_first(&in_line); - int n = fields_count(&out_fields); - for (int i=0; iquote >= 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; } } @@ -322,8 +320,8 @@ static void csv_write(struct format *fmt) 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) @@ -510,19 +508,20 @@ static void tmp_write(struct format *fmt) 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);