xtk_csv.h
Reads CSV text into a table of rows and fields.
This module turns comma-, semicolon- or tab-separated text into a table in memory, and can write that table out again as a Markdown (GFM) pipe table. It does not draw anything and needs no X display; it knows nothing about what the data means.
The parser reads the text one character at a time, so it handles the cases that simple line splitting gets wrong:
- A field in double quotes can hold the delimiter, a line break, or a double quote written twice (
""). - A field counts as quoted only when its first character is
"</tt>. A <tt>"anywhere else is an ordinary character. - Spaces are never trimmed: a field keeps its bytes exactly as written.
- A UTF-8 byte order mark at the start is skipped.
- Lines can end in
\n,\r\nor\r. - A line with nothing on it is skipped. A line such as
,,,is kept as a row of empty fields. - Rows do not have to have the same number of fields.
xtk_csv_parse()keeps each row as it is;xtk_csv_table_to_markdown()pads or cuts rows to the width of the first row.
Use. Pick the delimiter (or let xtk_csv_sniff_delimiter() guess it), call xtk_csv_parse(), read rows[r].fields[f], and free the table with xtk_csv_table_destroy(). The table owns all of its rows and field strings.
Example.
#include <stdio.h>
#include "xtk_csv.h"
const char *text = "name;size\n\"a;b\";12\n";
XtkCsvTable *table = xtk_csv_parse(text, xtk_csv_sniff_delimiter(text));
for (int r = 0; r < table->row_count; r++)
for (int f = 0; f < table->rows[r].field_count; f++)
printf("%d,%d: %s\n", r, f, table->rows[r].fields[f]);
xtk_csv_table_destroy(table);
*
Limitations.
- Allocation failures are not checked.
- A quoted field that is never closed takes the rest of the text.
#include "xtk_csv.h"
Types: XtkCsvDelimiter, XtkCsvRow, XtkCsvTable
Functions: xtk_csv_sniff_delimiter, xtk_csv_parse, xtk_csv_table_destroy, xtk_csv_table_to_markdown
Types
XtkCsvDelimiter
The character that separates fields.
| Name | Value | Description |
|---|---|---|
XTK_CSV_DELIM_COMMA |
',' |
Comma, the usual CSV delimiter. |
XTK_CSV_DELIM_SEMICOLON |
';' |
Semicolon, common where decimals are written with a comma. |
XTK_CSV_DELIM_TAB |
'\t' |
Tab, for tab-separated values. |
XtkCsvRow
One row of the table: its fields as strings.
| Field | Type | Description |
|---|---|---|
fields |
char ** |
The fields, in order. Each is a NUL-terminated string owned by the table. |
field_count |
int |
Number of fields in this row. |
field_capacity |
int |
Number of slots allocated in fields (internal; do not change). |
XtkCsvTable
A parsed CSV text: its rows in order.
| Field | Type | Description |
|---|---|---|
rows |
XtkCsvRow * |
The rows, in the order of the text. Owned by the table. |
row_count |
int |
Number of rows. |
row_capacity |
int |
Number of slots allocated in rows (internal; do not change). |
Functions
xtk_csv_sniff_delimiter
XtkCsvDelimiter xtk_csv_sniff_delimiter(const char *text);
Guesses the delimiter of CSV text from its first line.
Counts commas, semicolons and tabs outside double quotes on the first line. A byte order mark at the start is skipped.
| Parameter | Description |
|---|---|
text |
NUL-terminated CSV text, or NULL. |
Returns: The delimiter that occurs most often. XTK_CSV_DELIM_COMMA when text is NULL, when none occurs, or when the highest counts are equal.
xtk_csv_parse
XtkCsvTable *xtk_csv_parse(const char *text, XtkCsvDelimiter delimiter);
Parses CSV text into a new table.
A leading UTF-8 byte order mark is skipped. Rows keep the number of fields they have in the text; they are not padded or cut to the same width.
| Parameter | Description |
|---|---|
text |
NUL-terminated CSV text, or NULL. |
delimiter |
The character that separates fields (see xtk_csv_sniff_delimiter()). |
Returns: A new table, which the caller frees with xtk_csv_table_destroy(). It is not NULL: empty text or NULL gives a table with row_count 0.
xtk_csv_table_destroy
void xtk_csv_table_destroy(XtkCsvTable *table);
Frees a table with all of its rows and fields.
| Parameter | Description |
|---|---|
table |
The table to free. NULL is allowed and does nothing. |
xtk_csv_table_to_markdown
char *xtk_csv_table_to_markdown(const XtkCsvTable *table);
Writes a table as a Markdown (GFM) pipe table.
Row 0 becomes the header row, followed by a | --- | separator line. Each | in a field is written as \|, and each line break in a field becomes one space, because a table cell must stay on one line. Rows with fewer fields than row 0 get empty cells; fields past the width of row 0 are left out. Every line, the last one too, ends in \n.
| Parameter | Description |
|---|---|
table |
The table to write. |
Returns: A new NUL-terminated string that the caller frees with free(), or NULL when table is NULL, has no rows, or its first row has no fields.
xtk API
Widgets
- button
- checkbox
- radiobutton
- label
- entry
- textview
- edit_history
- spinbutton
- slider
- progressbar
- combobox
- scrollbar
- swatch
- panel
- statusbar
- tooltip
- toolbar
- icongrid
- iconview
- pathbar
- treeview
- datagrid
- graph
- scheme_wheel
- zoom
- tray
Containers and menus
Dialogs
Rich text
Drawing and images
Color
Configuration and themes
Input and the X server
Safety
Data formats