Skip to content

Commit

Permalink
Add 'done' argument to fread_csv_line
Browse files Browse the repository at this point in the history
  • Loading branch information
semitrivial committed Dec 9, 2016
1 parent 5c0fa6e commit 1785877
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ Both the array and the strings in the array are `malloc`'d.

## Documentation (fread_csv_line.c)

char *fread_csv_line(FILE *fp, int max_line_size, int *err)
char *fread_csv_line(FILE *fp, int max_line_size, int *done, int *err)

Given a file pointer, extract a line of CSV from it.

Other arguments:
int max_line_size: a maximum line size. Lines longer than this will
cause fread_csv_line to return NULL.
int *done: Pointer to an int which will be set to 1 if the end of fp is reached.
int *err: Pointer to an int where error codes will be
written. The two error codes, defined in `csv.h`, are:
CSV_ERR_LONGLINE and CSV_ERR_NO_MEMORY.
Expand All @@ -60,10 +61,11 @@ sense) in unpredictable ways, and will break if other things tamper with the fil
position in between calls to fread_csv_line.

Warning: `fread_csv_line` will not work correctly if called on different files in
parallel.
parallel. (Hopefully sometime in the future we'll add a proper `init` system to
deal with this).

Hopefully sometime in the future we'll add a proper `init` system to deal with the
latter warning.
Warning: Calling fread_csv_line on fp after a previous call exhausted the file
(indicated by *done), is undefined behavior.

## TODO

Expand Down
16 changes: 11 additions & 5 deletions fread_csv_line.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ while(0)
*
* Other arguments:
* size_t max_line_size: Maximum line size, in bytes.
* int *done: Pointer to an int that will be set to 1 when file is exhausted.
* int *err: Pointer to an int where error code will be written.
*
* Warning: Calling this function on an exhausted file (as indicated by the
* 'done' flag) is undefined behavior.
*
* See csv.h for definitions of error codes.
*/
char *fread_csv_line(FILE *fp, int max_line_size, int *err) {
char *fread_csv_line(FILE *fp, int max_line_size, int *done, int *err) {
static FILE *bookmark;
static char read_buf[READ_BLOCK_SIZE], *read_ptr, *read_end;
static int fread_len, prev_max_line_size = -1;
Expand Down Expand Up @@ -70,8 +74,7 @@ char *fread_csv_line(FILE *fp, int max_line_size, int *err) {
QUICK_GETC(ch, fp);

if ( !ch || (ch == '\n' && !fQuote)) {
*bptr = '\0';
return strdup(buf);
break;
}

if ( bptr >= limit ) {
Expand All @@ -87,8 +90,7 @@ char *fread_csv_line(FILE *fp, int max_line_size, int *err) {

if ( ch != '\"' ) {
if ( !ch || ch == '\n' ) {
*bptr = '\0';
return strdup(buf);
break;
}
fQuote = 0;
}
Expand All @@ -98,4 +100,8 @@ char *fread_csv_line(FILE *fp, int max_line_size, int *err) {
fQuote = 1;
}
}

*done = !ch;
*bptr = '\0';
return strdup(buf);
}

0 comments on commit 1785877

Please sign in to comment.