735cf928eb52e73b26027b1c9800a8922b3eb078 kent Sat Jun 3 17:07:49 2017 -0700 Adding csvEscapeAndAppend diff --git src/lib/csv.c src/lib/csv.c index 4cb5d5a..4f9914e 100644 --- src/lib/csv.c +++ src/lib/csv.c @@ -1,42 +1,50 @@ /* csv - stuff to help process comma separated values. Have to wrap quotes around * things with commas, and escape quotes with more quotes sometimes. */ #include "common.h" #include "linefile.h" #include "csv.h" -char *csvEscapeToDyString(struct dyString *dy, char *string) -/* Wrap string in quotes if it has any commas. Anything already in quotes get s double-quoted - * Returns transformated result, which will be input string if it has no commas, otherwise - * will be dy*/ +void csvEscapeAndAppend(struct dyString *dy, char *string) +/* Append escaped string to dy. Will insert comma if dy is non-empty */ { -/* If there are no commas just output it */ +if (dy->stringSize != 0) + dyStringAppendC(dy, ','); if (strchr(string, ',') == NULL) + dyStringAppend(dy, string); +else { - return string; - } -dyStringClear(dy); dyStringAppendC(dy, '"'); char c; while ((c = *string++) != 0) { if (c == '"') dyStringAppendC(dy, c); dyStringAppendC(dy, c); } dyStringAppendC(dy, '"'); + } +} + +char *csvEscapeToDyString(struct dyString *dy, char *string) +/* Wrap string in quotes if it has any commas. Put result into dy, and return it as a + * string. Anything already in quotes get double-quoted */ +{ +/* If there are no commas just output it */ +dyStringClear(dy); +csvEscapeAndAppend(dy, string); return dy->string; } void csvWriteVal(char *val, FILE *f) /* Write val, which may have some quotes or commas in it, in a way to be compatable with * csv list representation */ { /* If there are no commas just output it */ if (strchr(val, ',') == NULL) { fputs(val, f); return; } /* Strip surrounding quotes if any */