csvpp 1.2.0
CSV parsing / writing libraries
Loading...
Searching...
No Matches
csv.h
Go to the documentation of this file.
1
3
4// Copyright 2020 Matthew Chandler
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
25
26#ifndef CSV_H
27#define CSV_H
28
29#ifdef __cplusplus
30extern "C"
31{
32#endif
33
34#include <stdarg.h>
35#include <stdbool.h>
36#include <stdio.h>
37
38#include "version.h"
39
41
53
55
59char * CSV_strdup(const char * src);
60
65
67typedef struct CSV_row CSV_row;
68
70
73CSV_row * CSV_row_init(void);
74
76
79void CSV_row_free(CSV_row * rec);
80
82
86void CSV_row_append(CSV_row * rec, char * field);
87
89
92size_t CSV_row_size(const CSV_row * rec);
93
95
99const char * CSV_row_get(const CSV_row * rec, size_t i);
100
102
111CSV_status CSV_row_read_v(CSV_row * rec, ...);
112
114
118const char * const * CSV_row_arr(const CSV_row * rec);
119
134
136typedef struct CSV_reader CSV_reader;
137
139
144CSV_reader * CSV_reader_init_from_filename(const char * filename);
145
147
153CSV_reader * CSV_reader_init_from_file(FILE * file);
154
156
162CSV_reader * CSV_reader_init_from_str(const char * input);
163
165
168void CSV_reader_free(CSV_reader * reader);
169
171
174void CSV_reader_set_delimiter(CSV_reader * reader, const char delimiter);
175
177
180void CSV_reader_set_quote(CSV_reader * reader, const char quote);
181
183
186void CSV_reader_set_lenient(CSV_reader * reader, const bool lenient);
187
189
195char * CSV_reader_read_field(CSV_reader * reader);
196
198
206CSV_status CSV_reader_read_v(CSV_reader * reader, ...);
207
209
214CSV_row * CSV_reader_read_row(CSV_reader * reader);
215
217
238CSV_status CSV_reader_read_row_ptr(CSV_reader * reader, char *** fields, size_t * num_fields);
239
241
244bool CSV_reader_eof(const CSV_reader * reader);
245
247
250bool CSV_reader_end_of_row(const CSV_reader * reader);
251
253
257const char * CSV_reader_get_error_msg(const CSV_reader * reader);
258
260
263CSV_status CSV_reader_get_error(const CSV_reader * reader);
264
272
274typedef struct CSV_writer CSV_writer;
275
277
283CSV_writer * CSV_writer_init_from_filename(const char * filename);
284
286
290CSV_writer * CSV_writer_init_from_file(FILE * file);
291
293
297CSV_writer * CSV_writer_init_to_str(void);
298
300
303void CSV_writer_free(CSV_writer * writer);
304
306
309void CSV_writer_set_delimiter(CSV_writer * writer, const char delimiter);
310
312
315void CSV_writer_set_quote(CSV_writer * writer, const char quote);
316
318
322CSV_status CSV_writer_end_row(CSV_writer * writer);
323
325
331CSV_status CSV_writer_write_field(CSV_writer * writer, const char * field);
332
334
337CSV_status CSV_writer_write_fields(CSV_writer * writer, const CSV_row * fields);
338
340
346CSV_status CSV_writer_write_fields_ptr(CSV_writer * writer, const char *const * fields, const size_t num_fields);
347
349
354CSV_status CSV_writer_write_fields_v(CSV_writer * writer, ...);
355
357
360CSV_status CSV_writer_write_row(CSV_writer * writer, const CSV_row * fields);
361
363
369CSV_status CSV_writer_write_row_ptr(CSV_writer * writer, const char *const * fields, const size_t num_fields);
370
372
377CSV_status CSV_writer_write_row_v(CSV_writer * writer, ...);
378
380
385const char * CSV_writer_get_str(CSV_writer * writer);
386
387#ifdef __cplusplus
388}
389#endif
390
391#endif // CSV_H
CSV_status CSV_reader_read_v(CSV_reader *reader,...)
Reads fields into variadic arguments.
bool CSV_reader_eof(const CSV_reader *reader)
Check for end of input.
CSV_reader * CSV_reader_init_from_filename(const char *filename)
Create a new CSV_reader object parsing from a file.
void CSV_reader_set_delimiter(CSV_reader *reader, const char delimiter)
Change delimiter character.
const char * CSV_reader_get_error_msg(const CSV_reader *reader)
Get message for last error.
CSV_row * CSV_reader_read_row(CSV_reader *reader)
Read the current row from the CSV file and advance to the next.
void CSV_reader_free(CSV_reader *reader)
Free a CSV_reader object.
void CSV_reader_set_quote(CSV_reader *reader, const char quote)
Change the quote character.
CSV_reader * CSV_reader_init_from_file(FILE *file)
Create a new CSV_reader object parsing from a FILE *.
char * CSV_reader_read_field(CSV_reader *reader)
Read a single field.
CSV_status CSV_reader_get_error(const CSV_reader *reader)
Get error code for last error.
CSV_status CSV_reader_read_row_ptr(CSV_reader *reader, char ***fields, size_t *num_fields)
Read a row into an array of strings.
void CSV_reader_set_lenient(CSV_reader *reader, const bool lenient)
Enable / disable lenient parsing.
bool CSV_reader_end_of_row(const CSV_reader *reader)
Check for end of current row.
CSV_reader * CSV_reader_init_from_str(const char *input)
Create a new CSV_reader object parsing from an in-memory string.
size_t CSV_row_size(const CSV_row *rec)
Get CSV_Record array size.
CSV_row * CSV_row_init(void)
Create a new CSV_row.
const char *const * CSV_row_arr(const CSV_row *rec)
CSV_row array access.
void CSV_row_free(CSV_row *rec)
Free a CSV_row.
const char * CSV_row_get(const CSV_row *rec, size_t i)
CSV_row array element access.
CSV_status CSV_row_read_v(CSV_row *rec,...)
Reads fields into variadic arguments.
void CSV_row_append(CSV_row *rec, char *field)
Append a new field.
CSV_status CSV_writer_write_row_ptr(CSV_writer *writer, const char *const *fields, const size_t num_fields)
Write an array of strings as a row.
CSV_status CSV_writer_end_row(CSV_writer *writer)
End the current row.
CSV_status CSV_writer_write_row_v(CSV_writer *writer,...)
Write a row from the given variadic parameters.
CSV_status CSV_writer_write_row(CSV_writer *writer, const CSV_row *fields)
Write a CSV_row.
CSV_status CSV_writer_write_fields_ptr(CSV_writer *writer, const char *const *fields, const size_t num_fields)
Write an array of strings without ending the current row.
CSV_writer * CSV_writer_init_from_filename(const char *filename)
Create a new CSV_writer object writing to a file.
void CSV_writer_free(CSV_writer *writer)
Free a CSV_writer object.
CSV_status CSV_writer_write_fields_v(CSV_writer *writer,...)
Write from the given variadic parameters without ending the current row.
CSV_status CSV_writer_write_fields(CSV_writer *writer, const CSV_row *fields)
Write a CSV_row without ending the current row.
CSV_writer * CSV_writer_init_from_file(FILE *file)
Create a new CSV_writer object writing to a FILE *.
CSV_status CSV_writer_write_field(CSV_writer *writer, const char *field)
Write a single field.
void CSV_writer_set_quote(CSV_writer *writer, const char quote)
Change the quote character.
const char * CSV_writer_get_str(CSV_writer *writer)
Get the string output.
void CSV_writer_set_delimiter(CSV_writer *writer, const char delimiter)
Change delimiter character.
CSV_writer * CSV_writer_init_to_str(void)
Create a new CSV_writer object writing to a string.
char * CSV_strdup(const char *src)
strdup implementation, in case it's not implemented in string.h
CSV_status
Status codes.
Definition csv.h:45
@ CSV_INTERNAL_ERROR
Illegal reader / writer state reached.
Definition csv.h:51
@ CSV_EOF
Reached end of file.
Definition csv.h:47
@ CSV_OK
No errors, ready to read / write another field.
Definition csv.h:46
@ CSV_TOO_MANY_FIELDS_WARNING
More fields exist in one row than will fit in given storage. Non fatal.
Definition csv.h:50
@ CSV_PARSE_ERROR
Parsing error. See CSV_reader_get_error_msg for details.
Definition csv.h:48
@ CSV_IO_ERROR
IO error.
Definition csv.h:49