aboutsummaryrefslogtreecommitdiff
path: root/src/csv_parser.h
blob: 2e263a2e13f2f7e63033d13f5751cdd14f59e862 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
 * Copyright (C) 2013 Tadas Vilkeliskis <vilkeliskis.t@gmail.com>
 *
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifndef CSV_PARSER_INCLUDE_GUARD_E95A7C4D_BE4A_49D1_8607_900F36DD25B8
#define CSV_PARSER_INCLUDE_GUARD_E95A7C4D_BE4A_49D1_8607_900F36DD25B8

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <sys/types.h>


typedef enum csv_parser_state_t {
    csvps_line_start = 0,
    csvps_field_start,
    csvps_field_value,
    csvps_field_end,
    csvps_line_end_begin,
    csvps_line_end
} csv_parser_state_t;


typedef struct csv_parser_t {
    csv_parser_state_t state;
    /* csv row */
    int row;
    /* csv column */
    int col;
    uint32_t nread;
    /* user data */
    void *data;
} csv_parser_t;


/**
 * Field callback interface.
 *
 * Field callback can be called multiple times for the same row and column.
 * It's up to the user to construct full field value.
 *
 * Return 0 value on success, anything else on error.
 */
typedef int (*csv_parser_field_calback_t)(csv_parser_t *parser,
                                          const char *data,
                                          size_t length,
                                          int row,
                                          int col);


typedef struct csv_parser_settings_t {
    char delimiter;
    csv_parser_field_calback_t field_cb;
} csv_parser_settings_t;


void csv_parser_init(csv_parser_t *parser);

/**
 * Executes CSV parser on the given data. Returns number
 * of bytes read.
 */
size_t csv_parser_execute(csv_parser_t *parser,
                          const csv_parser_settings_t *settings,
                          const char *data,
                          size_t data_len);


#ifdef __cplusplus
}
#endif
#endif /* end of include guard */