Skip to content

Commit

Permalink
input/csv: unobfuscate text line to column splitting
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gsigh committed Dec 21, 2019
1 parent b2c4dde commit 9eab443
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/input/csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 9eab443

Please sign in to comment.