This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.c
212 lines (182 loc) · 4.84 KB
/
config.c
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2023 Ethan Uppal. All rights reserved.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "config.h"
#define ESC '\033'
static struct {
const char* comments;
const char* delimiters;
const char* whitespace;
char escape;
} conf = {
.comments = "\n#", .delimiters = "=", .whitespace = " \t", .escape = '\\'};
/**
* @private
*/
void conf_parse_internal(char* s, size_t length, conf_parse_handler_t handler,
void* user_data);
void conf_set_comments(const char* comments) {
conf.comments = comments;
}
void conf_set_delimiters(const char* delimiters, char escape) {
conf.delimiters = delimiters;
conf.escape = escape;
}
void conf_set_trimming(const char* whitespace) {
conf.whitespace = whitespace;
}
int conf_parse_file(FILE* f, conf_parse_handler_t handler, void* user_data) {
if (fseek(f, 0, SEEK_END) != 0) {
return 1;
}
long length = ftell(f);
if (length == -1) {
return 1;
}
rewind(f);
char* buffer = malloc(length + 1);
if (!buffer) {
return 1;
}
buffer[length] = 0;
if (fread(buffer, sizeof(char), length, f) != (size_t)length) {
return 1;
}
conf_parse_internal(buffer, (size_t)length, handler, user_data);
return 0;
}
int conf_parse_text(const char* s, conf_parse_handler_t handler,
void* user_data) {
size_t length = strlen(s);
char* s_copy = malloc(length + 1);
if (!s_copy) {
return 1;
}
memcpy(s_copy, s, length + 1);
conf_parse_internal(s_copy, length, handler, user_data);
free(s_copy);
return 0;
}
/**
* @private
*/
bool conf_should_skip_line(char c) {
return c == '\n' || (strchr(conf.comments, c) != NULL);
}
/**
* @private
*/
bool conf_should_end_var(char c) {
return strchr(conf.delimiters, c) != NULL;
}
/**
* @private
* @pre `conf.whitespace` is not `NULL`.
*/
char* conf_trim(char* str) {
// Strip leading whitespace
while (strchr(conf.whitespace, *str) != NULL) {
str++;
}
// Strip trailing whitespace
size_t length = strlen(str);
if (length > 0) {
while (strchr(conf.whitespace, str[length - 1]) != NULL) {
length--;
}
str[length] = 0;
}
return str;
}
/**
* @private
*/
void conf_clean(char* str) {
ssize_t i;
ssize_t offset = 0;
for (i = 0; str[i]; i++) {
str[i - offset] = str[i];
if (str[i] == ESC) {
offset++;
}
}
str[i - offset] = 0;
}
/**
* @private
*/
void conf_process_var(char* s, size_t var_start, size_t var_end,
size_t data_start, size_t data_end, conf_parse_handler_t handler,
void* user_data) {
char* var = s + var_start;
var[var_end - var_start] = 0;
char* data = s + data_start;
data[data_end - data_start] = 0;
conf_clean(var);
conf_clean(data);
if (conf.whitespace) {
var = conf_trim(var);
data = conf_trim(data);
}
handler(var, data, user_data);
}
void conf_parse_internal(char* s, size_t length, conf_parse_handler_t handler,
void* user_data) {
enum {
CONF_SKIP_LINE,
CONF_EXPECT_VAR,
CONF_VAR_ESCAPED,
CONF_EXPECT_DELIM,
CONF_EXPECT_DATA
} state = CONF_EXPECT_VAR;
size_t var_start = 0, var_end = 0;
size_t data_start = 0, data_end = 0;
for (size_t i = 0; i < length; i++) {
const char c = s[i];
if (conf_should_skip_line(c)) {
if (state == CONF_EXPECT_DATA) {
conf_process_var(s, var_start, var_end, data_start, data_end,
handler, user_data);
}
state = CONF_SKIP_LINE;
}
switch (state) {
case CONF_SKIP_LINE: {
if (c == '\n') {
state = CONF_EXPECT_VAR;
var_start = i + 1;
}
break;
}
case CONF_EXPECT_VAR: {
if (c == conf.escape) {
state = CONF_VAR_ESCAPED;
s[i] = ESC;
} else if (conf_should_end_var(c)) {
var_end = i;
state = CONF_EXPECT_DELIM;
}
break;
}
case CONF_VAR_ESCAPED: {
// we just found an escape character so ignore this char
state = CONF_EXPECT_VAR;
break;
}
case CONF_EXPECT_DELIM: {
// we already know we're at a delim because that's what
// conf_should_end_var checks, so we just move on to
// CONF_EXPECT_DATA
state = CONF_EXPECT_DATA;
data_start = i;
break;
}
case CONF_EXPECT_DATA: {
data_end = i + 1;
break;
}
}
}
}