solidc
Robust collection of general-purpose cross-platform C libraries and data structures designed for rapid and safe development in C
Loading...
Searching...
No Matches
csvparser.h
Go to the documentation of this file.
1
6#ifndef __CSV_PARSER_H__
7#define __CSV_PARSER_H__
8
9#include <stdbool.h>
10#include <stddef.h>
11
12// C++ compatibility
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17#ifndef _GNU_SOURCE
18#define _GNU_SOURCE 1
19#endif
20
21#ifndef CSV_ARENA_BLOCK_SIZE
22#define CSV_ARENA_BLOCK_SIZE (1 << 20)
23#endif
24
25#ifndef MAX_FIELD_SIZE
26// Maximum size of the csv line.
27#define MAX_FIELD_SIZE 1024
28#endif
29
30// static assert to ensure MAX_FIELD_SIZE is reasonable for stack allocation
31#if MAX_FIELD_SIZE > 4096
32#error "MAX_FIELD_SIZE is too large for stack allocation. Please reduce it to 4096 or less."
33#endif
34
48typedef struct CsvReader CsvReader;
49
53typedef struct {
54 char** fields;
55 size_t count;
56} Row;
57
58// Async callback for processed rows.
59typedef void (*CsvRowCallback)(size_t row_index, Row* row);
60
72CsvReader* csv_reader_new(const char* filename, size_t arena_memory);
73
89
102void csv_reader_parse_async(CsvReader* reader, CsvRowCallback callback, size_t alloc_max);
103
113size_t csv_reader_numrows(const CsvReader* reader);
114
123void csv_reader_free(CsvReader* reader);
124
125struct CsvReaderConfig {
126 char delim;
127 char quote;
128 char comment;
129 bool has_header;
130 bool skip_header;
131};
132
133struct CsvWriterConfig {
134 char delim; // Delimiter character
135 char quote; // Quote character
136 char newline; // Newline character
137 bool quote_all; // Quote all fields
138 bool flush; // Flush the stream after writing each row
139 bool append; // Append to the file if it exists, otherwise create a new file
140 bool first_row; // Whether the current row is the first row written to the
141 // file
142};
143
144typedef struct CsvReaderConfig CsvReaderConfig;
145typedef struct CsvWriterConfig CsvWriterConfig;
146
147void csv_reader_setconfig(CsvReader* reader, CsvReaderConfig config);
148CsvReaderConfig csv_reader_getconfig(CsvReader* reader);
149
164typedef struct CsvWriter CsvWriter;
165
166#define CsvWriterConfigure(writer, ...) \
167 csvwriter_setconfig(writer, (CsvWriterConfig){.delim = ',', \
168 .quote = '"', \
169 .newline = '\n', \
170 .quote_all = false, \
171 .flush = false, \
172 .first_row = false, \
173 __VA_ARGS__})
174
175// Create a new CSV writer associated with a filename.
176CsvWriter* csvwriter_new(const char* filename);
177
178// Set the configuration for the CSV writer.
179bool csvwriter_write_row(CsvWriter* writer, const char** fields, size_t numfields);
180
181// Free memory used by the CsvWriter and close the file stream.
182void csvwriter_free(CsvWriter* writer);
183
184#ifdef __cplusplus
185}
186#endif
187
188#endif /* __CSV_PARSER_H__ */
size_t csv_reader_numrows(const CsvReader *reader)
Get the number of rows in the CSV data.
Definition csvparser.c:294
void csv_reader_free(CsvReader *reader)
Free memory used by the CsvReader and CsvRow structures.
Definition csvparser.c:296
struct CsvReader CsvReader
Opaque structure representing a CSV parser. Create a new CSV parser with csv_reader_new and free it w...
Definition csvparser.h:48
Row ** csv_reader_parse(CsvReader *reader)
Parse the CSV data and retrieve all the rows at once.
Definition csvparser.c:138
CsvReader * csv_reader_new(const char *filename, size_t arena_memory)
Create a new CSV reader associated with a filename.
Definition csvparser.c:49
void csv_reader_parse_async(CsvReader *reader, CsvRowCallback callback, size_t alloc_max)
Parse the CSV data and pass each processed row back in a callback. Return true from the callback to s...
Definition csvparser.c:218
struct CsvWriter CsvWriter
Definition csvparser.h:164
Structure representing a CSV row.
Definition csvparser.h:53
size_t count
Number of fields in each row.
Definition csvparser.h:55
char ** fields
Array of fields in each row.
Definition csvparser.h:54