csv(3m)

NAME
csv - parse comma separated values

SYNOPSIS
#include <mba/csv.h>

int csv_row_parse(const tchar *src,
            size_t sn,
            tchar *buf,
            size_t bn,
            tchar *row[],
            int rn,
            int sep,
            int flags)
int csv_row_fread(FILE *in, char *buf, size_t bn, char *row[], int numcols, int sep, int flags)

DESCRIPTION
The csv(3m) module parses the popular comma separated values (CSV) format exported by applications like spreadsheets. This parser considers quoted elements, quoted quotes, and preserves carriage returns and newlines within elements.

Please note that escaping a quote requires that the whole element to be quoted as well. So a quoted string element would actually look like "foo" where the outer quotes quote the element and the remaining quote pairs are each an escape followed by the literal quote. This is consistent with how Excel and gnumeric behave and deviating from this behavior will generate an error.

row_parse
    Parse a line of text at src for no more than sn bytes and place pointers to zero terminiated strings allocated from no more than bn bytes of the memory at buf into the array row for at most rn data elements. The sep parameter must specify a separator to use and should not be a quote, carriage return, or newline. Comma ',', tab '\t', colon ':', and pipe '|' are common separators.

    The flags parameter can be zero or any combination of CSV_TRIM and CSV_QUOTES. If CSV_TRIM is specified, strings will be trimmed of leading and trailing whitespace. If the CSV_QUOTES flag is spcecified, quotes will be interpreted. Both flags should usually be specified although it is not uncommon to encounter tab delimited files where quotes should not be interpreted.

    The csv_row_parse function is actually a macro for either csv_row_parse_str or csv_row_parse_wcs. The csv_row_parse_wcs function has the same prototype but accepts wchar_t parameters whereas csv_row_parse_str accepts unsigned char parameters.

row_fread
    Read a line of text from the stream in, process the line with csv_row_parse and place pointers to zero terminiated strings allocated from no more than bn bytes of the memory at buf into the array row for at most rn data elements.

RETURNS

row_parse
    The csv_row_parse function returns the number of bytes of src parsed or -1 if an error occured in which case errno will be set appropriately.

row_fread
    The csv_row_fread function returns the number of bytes read from the stream in or -1 if an error occured in which case errno will be set appropriately.