Skip to content

Commit

Permalink
Fix parse_csv_file/2 to add comments/1 & comment/1 options
Browse files Browse the repository at this point in the history
  • Loading branch information
infradig committed May 2, 2023
1 parent b04d20a commit ea43f5b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ 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
comments(Boolean) # default false, skip lines beginning with comment character
comment(Char) # default '#', set the comment character
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
16 changes: 11 additions & 5 deletions src/predicates.c
Original file line number Diff line number Diff line change
Expand Up @@ -7490,9 +7490,9 @@ 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);
bool header = false, do_assert = true;
bool header = false, do_assert = true, comments = false;
const char *functor = NULL;
int sep = ',', quote = '"';
int sep = ',', quote = '"', comment = '#';
LIST_HANDLER(p3);

while (is_list(p3)) {
Expand All @@ -7506,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("comments", C_STR(q, h)) && is_atom(c) && (c->val_off == g_true_s))
comments = 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))
Expand All @@ -7514,6 +7516,8 @@ static bool fn_parse_csv_file_2(query *q)
use_strings = false;
else if (!strcmp("functor", C_STR(q, h)) && is_atom(c))
functor = C_STR(q, c);
else if (!strcmp("comment", C_STR(q, h)) && is_atom(c) && (C_STRLEN_UTF8(c) == 1))
comment = peek_char_utf8(C_STR(q, c));
else if (!strcmp("sep", C_STR(q, h)) && is_atom(c) && (C_STRLEN_UTF8(c) == 1))
sep = peek_char_utf8(C_STR(q, c));
else if (!strcmp("quote", C_STR(q, h)) && is_atom(c) && (C_STRLEN_UTF8(c) == 1))
Expand All @@ -7538,6 +7542,7 @@ static bool fn_parse_csv_file_2(query *q)

while ((len = getline(&q->p->save_line, &q->p->n_line, q->p->fp)) != -1) {
char *line = q->p->save_line;
line_nbr++;

if (line[len-1] == '\n')
len--;
Expand All @@ -7552,13 +7557,14 @@ static bool fn_parse_csv_file_2(query *q)
continue;
}

if (comments && (line[0] == comment))
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);
fprintf(stderr, "Error: line %u\n", line_nbr);

*f = save_f;
q->st.hp = save_hp;

line_nbr++;
}

free(q->p->save_line);
Expand Down

0 comments on commit ea43f5b

Please sign in to comment.