Module table
Reading CSV Files.
This reads tab- or comma-delimited data, optionally treating the first row
as a set of headers (TableCsv implies both commas and header row).
The rows are then available as an array of string arrays.
If TableColumns is also specified, it will additionally create columns,
which will be converted into float arrays if they appear to be numbers.
Given a simple CSV file like this:
Name,Age Bonzo,12 Alice,16 Frodo,46 Bilbo,144
then the most straightforward way to read it would be:
Table *t = table_new_from_file("test.csv", TableCsv | TableAll | TableColumns); if (t->error) { // note how you handle errors: file not found, conversion failed fprintf(stderr,"%s\n",t->error); return 1; } // printing out first row together with column names char **R = t->rows[0]; for (char **P = t->col_names; *P; ++P,++R) printf("'%s' (%s),",*P,*R); printf("\n"); // getting the second column with default conversion (float) float *ages = (float*)t->cols[1];
With table_convert_cols you can customize column conversion, and even provide your own conversion functions. These look like this:
const char *int_convert(const char *str, void *res) { \*((int*)res) = strtol(str,&endptr,10); return *endptr ? endptr : NULL; }
That is, they set the value and return an error if conversion is impossible.
See test-table.c for an example with custom conversions, and
test-sqlite3-table.c for a case where the table is built up
using table_add_row (which by design matches the required signature
for sqlite3_exec.)
Functions
| table_new (opts) | create a new empty table. |
| table_convert_cols (T, ...) | custom conversion for table columns. |
| table_generate_columns (T) | Explicitly create columns. |
| table_read_all (T) | Read all of a table into rows. |
| table_add_row (d, ncols, row, columns) | explicitly add new rows to a table. |
| table_finish_rows (T) | explicitly finish off a table created with table_add_row. |
| table_new_from_stream (in, opts) | Create a table from an opened file stream. |
| table_new_from_file (fname, opts) | Create a table from a file path. |
Functions
- table_new (opts)
-
create a new empty table.
optsare a set of flags:TableTab,TableComma,TableColumnNames,TableCsv(implying last two),TableColumns(create columns as well).Parameters:
- opts int
Returns:
-
Table *
- table_convert_cols (T, ...)
-
custom conversion for table columns.
Normally, table attempts to convert numerical columns
to arrays of
float. With this you can force the conversion tointarrays or leave them as strings, etcEach entry is a zero-based column index, and a type, one of
TableString,TableFloat,TableIntandTableCustom. If it’s a custom conversion, then followTableCustomwith aTableConvFun. The argument list ends with -1, which is an invalid column index.Parameters:
- T Table *
- ...
- table_generate_columns (T)
-
Explicitly create columns.
Only does this if the flags have
TableColumnsset. This is implicitly called by table_read_all and table_finish_rows.Parameters:
- T Table *
Returns:
-
bool
- table_read_all (T)
-
Read all of a table into rows.
If flag has
TableColumnsset, create columns as well.Parameters:
- T Table *
Returns:
-
bool
- table_add_row (d, ncols, row, columns)
-
explicitly add new rows to a table.
Parameters:
- d void *
- ncols int
- row char * *
- columns char * *
Returns:
-
int
- table_finish_rows (T)
-
explicitly finish off a table created with table_add_row.
Parameters:
- T Table *
Returns:
-
bool
- table_new_from_stream (in, opts)
-
Create a table from an opened file stream.
optshave same meaning as for table_new. If unsuccesful, the table’serrorfield will be non-NULL.Parameters:
- in FILE *
- opts int
Returns:
-
Table *
- table_new_from_file (fname, opts)
-
Create a table from a file path.
optshave same meaning as for table_new. If unsuccesful, the table’serrorfield will be non-NULL.Parameters:
- fname const char *
- opts int
Returns:
-
Table *