From: Gerhard Sittig Date: Sun, 13 Oct 2019 14:39:51 +0000 (+0200) Subject: input/csv: unobfuscate text line to column splitting X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=9eab4435f09234dfe7fbdcbab6a4a35298ed5393;p=libsigrok.git input/csv: unobfuscate text line to column splitting The parse_line() routine is rather complex, optionally accepts an upper limit for the number of columns, but unconditionally assumes a first one and drops preceeding fields. The rather generic n and k identifiers are not helpful. Use the 'seen' and 'taken' names instead which better reflect what's actually happening. Remove empty lines which used to tear apart groups of instructions which are strictly related. This organization neither was helpful during maintenance. --- diff --git a/src/input/csv.c b/src/input/csv.c index b1645e73..ed054e33 100644 --- a/src/input/csv.c +++ b/src/input/csv.c @@ -320,42 +320,39 @@ static char **parse_line(char *buf, struct context *inc, ssize_t max_cols) GSList *list, *l; char **columns; char *column; - gsize n, k; + gsize seen, taken; - n = 0; - k = 0; + seen = 0; + taken = 0; list = NULL; remainder = buf; str = strstr(remainder, inc->delimiter->str); - while (str && max_cols) { - if (n >= inc->first_column) { + if (seen >= inc->first_column) { column = g_strndup(remainder, str - remainder); list = g_slist_prepend(list, g_strstrip(column)); max_cols--; - k++; + taken++; } remainder = str + inc->delimiter->len; str = strstr(remainder, inc->delimiter->str); - n++; + seen++; } - if (buf[0] && max_cols && n >= inc->first_column) { + if (buf[0] && max_cols && seen >= inc->first_column) { column = g_strdup(remainder); list = g_slist_prepend(list, g_strstrip(column)); - k++; + taken++; } - if (!(columns = g_try_new(char *, k + 1))) + if (!(columns = g_try_new(char *, taken + 1))) return NULL; - - columns[k--] = NULL; - + columns[taken--] = NULL; for (l = list; l; l = l->next) - columns[k--] = l->data; + columns[taken--] = l->data; g_slist_free(list);