Skip to content

Commit 9eab443

Browse files
committed
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.
1 parent b2c4dde commit 9eab443

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/input/csv.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -320,42 +320,39 @@ static char **parse_line(char *buf, struct context *inc, ssize_t max_cols)
320320
GSList *list, *l;
321321
char **columns;
322322
char *column;
323-
gsize n, k;
323+
gsize seen, taken;
324324

325-
n = 0;
326-
k = 0;
325+
seen = 0;
326+
taken = 0;
327327
list = NULL;
328328

329329
remainder = buf;
330330
str = strstr(remainder, inc->delimiter->str);
331-
332331
while (str && max_cols) {
333-
if (n >= inc->first_column) {
332+
if (seen >= inc->first_column) {
334333
column = g_strndup(remainder, str - remainder);
335334
list = g_slist_prepend(list, g_strstrip(column));
336335

337336
max_cols--;
338-
k++;
337+
taken++;
339338
}
340339

341340
remainder = str + inc->delimiter->len;
342341
str = strstr(remainder, inc->delimiter->str);
343-
n++;
342+
seen++;
344343
}
345344

346-
if (buf[0] && max_cols && n >= inc->first_column) {
345+
if (buf[0] && max_cols && seen >= inc->first_column) {
347346
column = g_strdup(remainder);
348347
list = g_slist_prepend(list, g_strstrip(column));
349-
k++;
348+
taken++;
350349
}
351350

352-
if (!(columns = g_try_new(char *, k + 1)))
351+
if (!(columns = g_try_new(char *, taken + 1)))
353352
return NULL;
354-
355-
columns[k--] = NULL;
356-
353+
columns[taken--] = NULL;
357354
for (l = list; l; l = l->next)
358-
columns[k--] = l->data;
355+
columns[taken--] = l->data;
359356

360357
g_slist_free(list);
361358

0 commit comments

Comments
 (0)