]> mj.ucw.cz Git - xsv.git/commitdiff
Use get_field() where useful
authorMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 14:00:44 +0000 (16:00 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 24 Jul 2012 14:00:44 +0000 (16:00 +0200)
xsv.c

diff --git a/xsv.c b/xsv.c
index 872090f02abdb73fa19675cd8e8902895a89fb3a..32da50480651ecd4d78968e7820a18e628020ebc 100644 (file)
--- 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; 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;
                        }
                }
@@ -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);