Skip to content

Commit

Permalink
Fix parse_csv_file/2 to add header/1 option to skip first line
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed May 1, 2023
1 parent d4dcb7a commit b04d20a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ Where options can be:

trim(Boolean) # default false, trims leading and trailing whitespace
numbers(Boolean) # default false, converts integers and floats
header(Boolean) # default false, skip first (header) line
strings(Boolean) # default depends on type of input (atom ot string)
assert(Boolean) # default false, assertz to database instead (assumed for files, needs a functor)
functor(Atom) # default output is a list, create a structure (mandatory for files and with assert)
Expand Down
10 changes: 9 additions & 1 deletion src/predicates.c
Original file line number Diff line number Diff line change
Expand Up @@ -7489,7 +7489,8 @@ static bool fn_parse_csv_file_2(query *q)
{
GET_FIRST_ARG(p1,atom);
GET_NEXT_ARG(p3,list_or_nil);
bool trim = false, numbers = false, use_strings = is_string(p1), do_assert = true;
bool trim = false, numbers = false, use_strings = is_string(p1);
bool header = false, do_assert = true;
const char *functor = NULL;
int sep = ',', quote = '"';
LIST_HANDLER(p3);
Expand All @@ -7505,6 +7506,8 @@ static bool fn_parse_csv_file_2(query *q)
trim = true;
else if (!strcmp("numbers", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s))
numbers = true;
else if (!strcmp("header", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s))
header = true;
else if (!strcmp("strings", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s))
use_strings = true;
else if (!strcmp("strings", C_STR(q, h)) && is_atom(c) && (c->val_off == g_false_s))
Expand Down Expand Up @@ -7544,6 +7547,11 @@ static bool fn_parse_csv_file_2(query *q)

line[len] = '\0';

if (header) {
header = false;
continue;
}

if (!do_parse_csv_line(q, sep, quote, trim, numbers, use_strings, functor, line, NULL, 0))
fprintf(stderr, "Error: line %u\n", line_nbr+1);

Expand Down

0 comments on commit b04d20a

Please sign in to comment.