|
rcbench
Servo and ESC test bench: panel, IOmcu and shared C
|
The CSV reader's byte layer: the sources behind the read/rewind function pointers, the row splitter that handles quoting and line endings, and the delimiter sniffer. More...
#include <ctype.h>#include <limits.h>#include <stdio.h>#include <string.h>#include "log_csv.h"#include "log_csv_int.h"Macros | |
| #define | SNIFF_LINES 25 |
| Most non-blank lines the delimiter sniffer looks at. | |
Functions | |
| static size_t | mem_read (void *ctx, char *buf, size_t max) |
| Read from a memory source. | |
| static bool | mem_rewind (void *ctx) |
| Rewind a memory source to its start. | |
| static bool | mem_size (void *ctx, size_t *bytes) |
| Length of a memory source. | |
| static bool | mem_seek (void *ctx, size_t offset) |
| Move a memory source's read position. | |
| void | log_source_memory (log_source_t *src, log_mem_ctx_t *ctx, const char *text, size_t len) |
| A source over a block of memory. | |
| static size_t | file_read (void *ctx, char *buf, size_t max) |
| Read from a stdio file. | |
| static bool | file_error (void *ctx) |
| Whether the last read of a stdio file failed. | |
| static bool | file_rewind (void *ctx) |
| Rewind a stdio file to its start. | |
| static bool | file_size (void *ctx, size_t *bytes) |
| fseek and ftell take a long: 2GiB less 1 byte on the panel. | |
| static bool | file_seek (void *ctx, size_t offset) |
| Move a stdio file's read position. | |
| void | log_source_stdio (log_source_t *src, void *file) |
A source over an open stdio stream. file is a FILE*. | |
| void | log_reader_init (log_reader_t *r, log_source_t *src) |
| Set up a row splitter. | |
| static int | reader_fill (log_reader_t *r) |
| Refill the read-ahead buffer when it is empty. | |
| static int | reader_getc (log_reader_t *r) |
| Read one byte, skipping a leading byte order mark. | |
| static int | reader_peek (log_reader_t *r) |
| Look at the next byte without taking it. | |
| int | log_reader_row (log_reader_t *r, char delimiter, char *store, size_t store_size, char **fields, int max_fields, int *total_fields) |
| Read one row. | |
| const char * | log_csv_delimiter_label (char delimiter) |
| Label of a delimiter. | |
| static int | count_fields (const char *s, size_t len, char delimiter) |
| Count the fields a delimiter makes in one line. | |
| char | log_csv_sniff (const char *sample, size_t len) |
| Guess the delimiter from a text sample: the most consistent, widest split. | |
The CSV reader's byte layer: the sources behind the read/rewind function pointers, the row splitter that handles quoting and line endings, and the delimiter sniffer.
A source is a string in memory (tests, the sniff sample) or a stdio file (the card); the row splitter reads either through log_source_t and never holds more than one buffer of it. The splitter and the sniffer take no allocation; the allocator and the analysis built on them are in log_csv.c.
SPDX-License-Identifier: MIT
|
static |
Count the fields a delimiter makes in one line.
Delimiters inside a quoted field are not counted.
| s | The line. |
| len | Length of the line, bytes. |
| delimiter | The candidate delimiter. |
|
static |
Whether the last read of a stdio file failed.
| ctx | The FILE *. |
|
static |
Read from a stdio file.
| ctx | The FILE *. |
| buf | Receives the bytes. |
| max | Capacity of buf, bytes. |
|
static |
Rewind a stdio file to its start.
| ctx | The FILE *. |
|
static |
Move a stdio file's read position.
| ctx | The FILE *. |
| offset | New position, bytes from the start. |
offset exceeds LONG_MAX.
|
static |
fseek and ftell take a long: 2GiB less 1 byte on the panel.
A file longer than that answers false, and the analysis reads it on instead.
| ctx | The FILE *. |
| bytes | Receives the length, bytes. |
| const char * log_csv_delimiter_label | ( | char | delimiter | ) |
Label of a delimiter.
| delimiter | The delimiter. |
| char log_csv_sniff | ( | const char * | sample, |
| size_t | len ) |
Guess the delimiter from a text sample: the most consistent, widest split.
| sample | The sample text. |
| len | Length of sample, bytes. |
| void log_reader_init | ( | log_reader_t * | r, |
| log_source_t * | src ) |
Set up a row splitter.
| r | The splitter. |
| src | The input. |
| int log_reader_row | ( | log_reader_t * | r, |
| char | delimiter, | ||
| char * | store, | ||
| size_t | store_size, | ||
| char ** | fields, | ||
| int | max_fields, | ||
| int * | total_fields ) |
Read one row.
A row ends at its line feed (0x0A). Input that ends inside a row – a write cut off by a power loss or a full card – leaves a row nothing proves complete: a number cut after two of its four digits still parses, and the field count can still match the header. That row is not returned: the call answers -1 as at the end of input and sets r->cut.
| r | The splitter. |
| delimiter | The field delimiter. |
| store | scratch for the field text; fields point into it |
| store_size | Size of store, bytes. |
| fields | receives one pointer per field, up to max_fields |
| max_fields | Capacity of fields, in pointers. |
| total_fields | receives the true field count, which may exceed max_fields; that is how a ragged row is spotted |
| void log_source_memory | ( | log_source_t * | src, |
| log_mem_ctx_t * | ctx, | ||
| const char * | text, | ||
| size_t | len ) |
A source over a block of memory.
| src | Receives the source. |
| ctx | Holds the read position; set to the start of text. |
| text | The bytes read. |
| len | Length of text, bytes. |
| void log_source_stdio | ( | log_source_t * | src, |
| void * | file ) |
A source over an open stdio stream. file is a FILE*.
| src | Receives the source. |
| file | An open stream, a FILE *. |
|
static |
Read from a memory source.
| ctx | The log_mem_ctx_t. |
| buf | Receives the bytes. |
| max | Capacity of buf, bytes. |
|
static |
|
static |
Move a memory source's read position.
| ctx | The log_mem_ctx_t. |
| offset | New position, bytes from the start. |
offset is past the end.
|
static |
Length of a memory source.
| ctx | The log_mem_ctx_t. |
| bytes | Receives the length, bytes. |
|
static |
Refill the read-ahead buffer when it is empty.
Sets failed when the source reports an error at its end.
| r | The row splitter. |
|
static |
Read one byte, skipping a leading byte order mark.
| r | The row splitter. |
|
static |
Look at the next byte without taking it.
| r | The row splitter. |