d11734eb8be118418ae02fe26799870807b43685 jcasper Mon Mar 3 07:41:09 2025 -0800 Needed a function to split up CSV without losing empty records, refs #31812 diff --git src/lib/common.c src/lib/common.c index e6e5e857abc..755d81602ad 100644 --- src/lib/common.c +++ src/lib/common.c @@ -2237,30 +2237,77 @@ break; } ++in; } if (*in == 0) break; // Tag end of word with zero if (outArray != NULL) *in = 0; in += 1; } return recordCount; } +int chopByCharRespectDoubleQuotesKeepEmpty(char *in, char sep, char *outArray[], int outSize) +/* Chop a string into sep delimited strings but honor double quotes. Keep empty entries (",,") + * in the list instead of skipping over them. */ +{ +int recordCount = 0; +char c; +boolean quoting = FALSE; +for (;;) + { + if (outArray != NULL && recordCount >= outSize) + break; + + if (*in == 0) + break; + + if (outArray != NULL) + outArray[recordCount] = in; + recordCount += 1; + quoting = FALSE; + for (;;) + { + if ((c = *in) == 0) + break; + if (quoting) + { + if (c == '"') + quoting = FALSE; + } + else + { + quoting = (c == '"'); + if (c == sep) + break; + } + ++in; + } + if (*in == 0) + break; + + // Tag end of word with zero + if (outArray != NULL) + *in = 0; + in += 1; + } +return recordCount; +} + int chopByChar(char *in, char chopper, char *outArray[], int outSize) /* Chop based on a single character. */ { int i; char c; if (*in == 0) return 0; for (i=0; (i