CSV API Reference
CSV API
CSV API provides stream-oriented reading of delimiter-separated value files.
The first row defines the column names (headers), and each call to
csv_next() reads one data line into the current fields vector.
Example
The following example demonstrates how to use the CSV API.
// Copyright 2024 Robert Bosch GmbH
//
// SPDX-License-Identifier: Apache-2.0
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dse/logger.h>
#include <dse/clib/csv/csv.h>
uint8_t __log_level__ = LOG_ERROR;
/* Path to the bundled sample file (relative to the working directory when
* the example binary is run from its install location). */
#define CSV_FILE "examples/csv_file/data/valueset.csv"
int main(void)
{
// Open and parse the CSV file.
CsvDesc csv = csv_open(CSV_FILE);
size_t col_count = 0;
int col_a = -1, col_b = -1, col_c = -1;
printf("Headers :");
while (1) {
const char* h = csv_header(&csv, col_count);
if (h == NULL) break;
printf(" [%zu] %s", col_count, h);
if (strcmp(h, "A") == 0) col_a = (int)col_count;
if (strcmp(h, "B") == 0) col_b = (int)col_count;
if (strcmp(h, "C") == 0) col_c = (int)col_count;
col_count++;
}
printf("\n");
printf("Columns : %zu\n\n", col_count);
if (col_a < 0 || col_b < 0 || col_c < 0) {
fprintf(stderr, "Expected signals A, B, C not found in CSV.\n");
csv_close(&csv);
return 1;
}
printf("Signal columns: A=%d B=%d C=%d\n\n", col_a, col_b, col_c);
// Stream rows and print each parsed value set.
while (1) {
int rc = csv_next(&csv);
if (rc == -ENODATA) break;
if (rc != 0) {
fprintf(stderr, "CSV parse error: %d\n", rc);
break;
}
double ts = csv_field(&csv, 0);
double signal_a = csv_field(&csv, (size_t)col_a);
double signal_b = csv_field(&csv, (size_t)col_b);
double signal_c = csv_field(&csv, (size_t)col_c);
printf("timestamp=%.4f A=%+.4f B=%+.4f C=%+.4f\n",
ts, signal_a, signal_b, signal_c);
}
// Release all resources.
csv_close(&csv);
return 0;
}
Typedefs
CsvDesc
typedef struct CsvDesc {
const char* file_name;
FILE* file;
Vector header;
Vector fields;
size_t line_maxlen;
char* line;
} CsvDesc;
Functions
csv_open
Open and parse a CSV file into a CsvDesc descriptor object.
The first row is consumed as the header row (column names); all subsequent rows are stored as data rows. Fields may be separated by commas or semicolons.
Parameters
- path (const char*)
- Path to a CSV file. If NULL, the
CSV_FILEenvironment variable is used. If the resolved path cannot be opened an emptyCsvDescis returned without error.
Returns
- CsvDesc (struct)
- CSV descriptor object. Call
csv_close()when finished.
csv_count
Return the number of fields in the current parsed row.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.
Returns
- size_t
- Number of fields in
CsvDesc.fields.
csv_header
Return the header (column name) for a column index.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.
- col (size_t)
- Zero-based column index.
Returns
- const char*
- Column name, or NULL if the index is out of range.
csv_next
Read and parse the next data line.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.
Returns
- int
- 0 on success, -ENODATA at end of file, -EINVAL on a parse error (bad token or wrong field count), -EOVERFLOW on truncated lines (line exceeds configured buffer), -ENOMEM on allocation failure.
csv_field
Return a field value from the current parsed row.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.
- col (size_t)
- Zero-based column index.
Returns
- double
- Parsed value, or 0.0 if the index is out of range.
csv_fields
Read multiple field values from the current row.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.
- col (size_t[])
- Column index list.
- val (double[])
- Output value array.
- len (size_t)
- Number of requested columns.
Returns
void
csv_close
Release all resources owned by a CSV descriptor.
Parameters
- csv (CsvDesc*)
- CSV descriptor object.