9df48bcdd35df301c8b97ef88479b3f8b5f348d5 braney Wed Oct 24 13:42:53 2018 -0700 add a routine to chop on commas that allows the inclusion of escaped commas diff --git src/lib/common.c src/lib/common.c index fea1e5b..17debeb 100644 --- src/lib/common.c +++ src/lib/common.c @@ -790,30 +790,66 @@ e = strchr(s, delimiter); if (e == NULL) el = slNameNew(s); else { el = slNameNewN(s, e-s); e += 1; } slAddHead(&list, el); s = e; } slReverse(&list); return list; } +struct slName *slNameListFromCommaEscaped(char *s) +/* Return list of slNames gotten from parsing comma delimited string. + * The final comma is optional. a,b,c and a,b,c, are equivalent + * for comma-delimited lists. To escape commas, put two in a row, + * which eliminates the possibility for null names + * (eg. a,,b,c will parse to two elements a,b and c). */ +{ +if (s == NULL) + return NULL; + +struct slName *list = NULL; +char buffer[strlen(s) + 1], *ptr = buffer; + +for (; *s != 0; s++) + { + *ptr++ = *s; + if (*s == ',') + { + if (s[1] != ',') // if next character not also a , + { + // we found the delimeter, add the string to the list + slAddHead(&list, slNameNewN(buffer, ptr - buffer - 1)); + ptr = buffer; // start a new buffer + } + else + s++; // skip the quoting comma and continue + } + } + +if (ptr > buffer) // is there something in the buffer + slAddHead(&list, slNameNewN(buffer, ptr - buffer)); // add it to the list + +slReverse(&list); +return list; +} + struct slName *slNameListFromStringArray(char *stringArray[], int arraySize) /* Return list of slNames from an array of strings of length arraySize. * If a string in the array is NULL, the array will be treated as * NULL-terminated (shorter than arraySize). */ { char *s; struct slName *list = NULL, *el; int i; if (stringArray == NULL) return NULL; for (i = 0; i < arraySize; i++) { s = stringArray[i]; if (s == NULL) break;