summaryrefslogtreecommitdiff
path: root/csv.h
blob: 690df8935576f3640fc57c86b3b8da577e50c321 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef ITS1_CSV_H_INCLUDED
#define ITS1_CSV_H_INCLUDED

#include <stdio.h>	/* FILE */
#include <stddef.h>	/* size_t */

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
	CSV_ERR_OK            =  0,
	CSV_ERR_OUT_OF_MEMORY = -1,
	CSV_ERR_OUT_OF_RANGE  = -2,
	CSV_ERR_IO_READ       = -3,
	CSV_ERR_IO_WRITE      = -4
} csv_err_t;

typedef struct {
	char field_delimiter;
	char field_separator;

	void (*cb_error)(csv_err_t, void *);
	void *cb_error_arg;

	void *(*cb_allocate)(size_t, size_t, void *);
	void *(*cb_reallocate)(void *, size_t, size_t, void *);
	void (*cb_free)(void *, size_t sz, void *);
	void *cb_memory_arg;
} csv_options_t;

extern const csv_options_t csv_default_options;

typedef struct {
	char *str;
	int cap, pos;
} csv_string_t;

typedef struct {
	int *fields;
	int cap, pos;
} csv_field_t;

typedef struct {
	const csv_options_t *csv_options;
	csv_string_t csv_string;
	csv_field_t csv_field;
} csv_t;

/* initialization */
void csv_init(csv_t *csv);
void csv_init_opt(csv_t *csv, const csv_options_t * const csv_options);

/* cleanup after error */
void csv_cleanup(csv_t *csv);

/* read */
int csv_read(csv_t *csv, FILE *in);

/* field access */
int csv_nfields(csv_t *csv);
const char * csv_field(csv_t *csv, int idx);

/* write */
void csv_write(FILE *out, const csv_t * const csv);
void csv_write_ex(FILE *out, int n, const char *fields[], const csv_options_t * const csv_options);

/* error handling */
const char * csv_err_str(csv_err_t csv_err);

#ifdef __cplusplus
}
#endif

#endif