summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--csv-test.c11
-rw-r--r--csv.c20
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)
103int 103int
104main(void) 104main(void)
105{ 105{
106 const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL };
107
108 csv_write(stdout, -1, names, NULL);
109 csv_write(stdout, 0, names, NULL);
110 csv_write(stdout, 1, names, NULL);
111 csv_write(stdout, 2, names, NULL);
112 csv_write(stdout, 3, names, NULL);
113 csv_write(stdout, 4, names, NULL);
114 csv_write(stdout, 5, names, NULL);
115 return 0;
116
106 FILE *in = fopen("500000 Records.csv", "r"); 117 FILE *in = fopen("500000 Records.csv", "r");
107 if ( in == NULL ) { 118 if ( in == NULL ) {
108 fprintf(stderr, "failed to open testfile...\n"); 119 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
458} 458}
459 459
460void 460void
461csv_write(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options) 461csv_write(FILE *out, int n, const char *fields[], const csv_options_t * csv_options)
462{ 462{
463 assert(csv_options != NULL);
464 assert(fields != NULL);
465 assert(out != NULL); 463 assert(out != NULL);
464 assert(fields != NULL);
465
466 if ( csv_options == NULL ) {
467 csv_options = &csv_default_options;
468 }
466 469
467 const char SEP = csv_options->quote_symbol; 470 const char SEP = csv_options->sep_symbol;
468 471
469 /* process first field */ 472 /* process first field */
470 if ( 0 != n && fields[0] != NULL ) { 473 if ( 0 != n && *fields != NULL ) {
471 csv_write_field(out, fields[0], csv_options); 474 --n;
475 csv_write_field(out, *fields++, csv_options);
472 476
473 /* process next fields */ 477 /* process next fields */
474 for ( int i = 1; i != n && fields[i] != NULL; ++i ) { 478 for ( ; n && *fields != NULL; --n, ++fields ) {
475 putc(SEP, out); 479 putc(SEP, out);
476 csv_write_field(out, fields[i], csv_options); 480 csv_write_field(out, *fields, csv_options);
477 } 481 }
478 } 482 }
479 fprintf(out, "\r\n"); 483 fprintf(out, "\r\n");