From 4bf39b20162e7d12a53a1de607a6f83b159928f5 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Mon, 22 Jun 2020 17:20:00 +0200 Subject: fix: csv_write --- csv-test.c | 11 +++++++++++ csv.c | 20 ++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/csv-test.c b/csv-test.c index e2fd308..bbc7212 100644 --- a/csv-test.c +++ b/csv-test.c @@ -103,6 +103,17 @@ test_exception_longjmp(FILE *f) int main(void) { + const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL }; + + csv_write(stdout, -1, names, NULL); + csv_write(stdout, 0, names, NULL); + csv_write(stdout, 1, names, NULL); + csv_write(stdout, 2, names, NULL); + csv_write(stdout, 3, names, NULL); + csv_write(stdout, 4, names, NULL); + csv_write(stdout, 5, names, NULL); + return 0; + FILE *in = fopen("500000 Records.csv", "r"); if ( in == NULL ) { fprintf(stderr, "failed to open testfile...\n"); diff --git a/csv.c b/csv.c index e36e3ba..736c76d 100644 --- a/csv.c +++ b/csv.c @@ -458,22 +458,26 @@ csv_write_field(FILE *out, const char *field, const csv_options_t * const csv_op } void -csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options) +csv_write(FILE *out, int n, const char *fields[], const csv_options_t * csv_options) { - assert(csv_options != NULL); - assert(fields != NULL); assert(out != NULL); + assert(fields != NULL); + + if ( csv_options == NULL ) { + csv_options = &csv_default_options; + } - const char SEP = csv_options->quote_symbol; + const char SEP = csv_options->sep_symbol; /* process first field */ - if ( 0 != n && fields[0] != NULL ) { - csv_write_field(out, fields[0], csv_options); + if ( 0 != n && *fields != NULL ) { + --n; + csv_write_field(out, *fields++, csv_options); /* process next fields */ - for ( int i = 1; i != n && fields[i] != NULL; ++i ) { + for ( ; n && *fields != NULL; --n, ++fields ) { putc(SEP, out); - csv_write_field(out, fields[i], csv_options); + csv_write_field(out, *fields, csv_options); } } fprintf(out, "\r\n"); -- cgit v1.3