-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsv_parser.h
More file actions
73 lines (61 loc) · 1.77 KB
/
Copy pathcsv_parser.h
File metadata and controls
73 lines (61 loc) · 1.77 KB
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
#ifndef CSV_PARSER_H
#define CSV_PARSER_H
#include "csv_config.h"
#include "arena.h"
#include <stddef.h>
#include <stdio.h>
#include <stdbool.h>
#define MAX_LINE_LENGTH 4096
typedef enum {
FIELD_START,
UNQUOTED_FIELD,
QUOTED_FIELD,
QUOTE_IN_QUOTED_FIELD,
FIELD_END
} ParseState;
typedef enum {
CSV_PARSER_OK = 0,
CSV_PARSER_ERROR_NULL_POINTER,
CSV_PARSER_ERROR_MEMORY_ALLOCATION,
CSV_PARSER_ERROR_BUFFER_OVERFLOW,
CSV_PARSER_ERROR_INVALID_INPUT,
CSV_PARSER_ERROR_MALFORMED_CSV
} CSVParserResult;
typedef struct {
char **fields;
size_t count;
size_t capacity;
} FieldArray;
typedef struct {
char *line;
size_t pos;
size_t len;
ParseState state;
bool in_quotes;
char delimiter;
char enclosure;
char escape;
int line_number;
Arena *arena;
} ParseContext;
typedef struct {
FieldArray fields;
bool success;
const char *error;
int error_line;
int error_column;
} CSVParseResult;
typedef struct {
CSVConfig *config;
Arena *arena;
ParseContext parse_ctx;
} CSVParser;
char* read_full_record(FILE *file, Arena *arena);
int parse_csv_line(const char *line, char **fields, int max_fields, Arena *arena, const CSVConfig *config);
int parse_headers(const char *line, char **fields, int max_fields, Arena *arena, const CSVConfig *config);
CSVParserResult csv_parser_count_fields_in_line(const char *line, const ParseContext *ctx, int *field_count);
CSVParserResult csv_parser_split_line_generic(const char *line, FieldArray *fields, const ParseContext *ctx);
CSVParser* csv_parser_init(Arena *arena, CSVConfig *config);
void csv_parser_free(CSVParser *parser);
CSVParseResult csv_parse_line_inplace(const char *line, Arena *arena, const CSVConfig *config, int line_number);
#endif