aboutsummaryrefslogtreecommitdiff
path: root/src/parcini.c
blob: 89a9356793e29660b4cf146d4e2ced2965535544 (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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
 * Copyright (c) 2021 Yaroslav de la Peña Smirnov
 *
 * Released under the GNU Lesser General Public License, version 2.1 only.
 * For more information see `LICENSE' or visit
 * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
 */
#define _POSIX_C_SOURCE 200809L
#include "parcini.h"

#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

struct parcini {
	FILE *stream;
	char *last_section;
	size_t last_section_n;
	char *last_line;
	size_t last_line_n;
	size_t last_line_no;
};

/*
 * Strip string "slice" of any whitespace characters to the right until the next
 * non-whitespace character.
 */
static void
rstrip(const char *start, char **end)
{
	while (start < *end && isspace(**end)) {
		**end = '\0';
		*end -= 1;
	}
}

/*
 * Ignore whitespace characters on the left, moving the pointer until we find
 * a non-whitespace character.
 */
static void
lskip(char **start)
{
	while (**start && isspace(**start)) {
		*start += 1;
	}
}

/*
 * Make a copy of the string "slice" located from pointer start to pointer end,
 * copying it over to dst, reallocating memory if needed, and terminating it
 * with a null character.
 */
static char *
slicecpy(char *start, char *end, char **dst, size_t *dstn)
{
	size_t srcn = end - start;
	if (*dstn < srcn + 1) {
		*dstn = srcn + 1;
		char *newptr = realloc(*dst, *dstn);
		if (newptr == NULL) {
			return NULL;
		}
		*dst = newptr;
	}
	for (size_t i = 0; i < srcn; i++) {
		(*dst)[i] = start[i];
	}
	(*dst)[srcn] = '\0';

	return *dst;
}

bool
parcini_value_handle(const struct parcini_value *value,
		const enum parcini_value_type expected, void *dst)
{

	if (value->type != expected) {
		return false;
	}
	if(expected == PARCINI_VALUE_STRING) {
		char **string = (char **)dst;
		*string = strdup(value->value.string);
		return true;
	}
	if(expected == PARCINI_VALUE_INTEGER) {
		long int *integer = (long int *)dst;
		*integer = value->value.integer;
		return true;
	}
	if(expected == PARCINI_VALUE_BOOLEAN) {
		bool *boolean = (bool *)dst;
		*boolean = value->value.boolean;
		return true;
	}

	return false;
}

enum parcini_result
parcini_parse_next_line(parcini_t *parser, struct parcini_line *parsed)
{
	ssize_t nread = getline(&parser->last_line, &parser->last_line_n,
			parser->stream);

	if (nread < 0) {
		if (feof(parser->stream)) {
			return PARCINI_EOF;
		}
		return PARCINI_STREAM_ERROR;
	}

	parsed->lineno = ++parser->last_line_no;
	if (nread > 0) {
		char *start = parser->last_line;
		if (*start == PARCINI_COMMENT_CHAR) {
			goto empty;
		}

		if (*start == '[') {
			char *end = strchr(start, ']');
			if (!end) {
				return PARCINI_SECTION_PARSE_ERROR;
			}
			char *cmnt = strchr(start, PARCINI_COMMENT_CHAR);
			if (cmnt && cmnt < end) {
				return PARCINI_SECTION_PARSE_ERROR;
			}
			if (!slicecpy(start + 1, end, &parser->last_section,
						&parser->last_section_n)) {
				return PARCINI_MEMORY_ERROR;
			}
			parsed->section = parser->last_section;
			parsed->key = NULL;
			parsed->value.type = PARCINI_VALUE_NONE;
			return PARCINI_SECTION;
		}

		parsed->section = parser->last_section;
		char *delim = strchr(start, '=');
		if (start == delim) {
			return PARCINI_KEY_PARSE_ERROR;
		}
		if (delim) {
			char *cmnt = strchr(start, PARCINI_COMMENT_CHAR);
			if (cmnt && cmnt < delim) {
				return PARCINI_KEY_PARSE_ERROR;
			}
			char *end = delim;
			*end = '\0';
			end -= 1;
			rstrip(start, &end);
			parsed->key = start;

			start = delim + 1;
			lskip(&start);
			end = parser->last_line + nread - 1;
			if (*start == '"') {
				delim = strrchr(start, '"');
				if (!delim) {
					return PARCINI_VALUE_PARSE_ERROR;
				}
				if (delim != end) {
					char *postq = delim + 1;
					lskip(&postq);
					if (*postq && *postq != PARCINI_COMMENT_CHAR) {
						return PARCINI_VALUE_PARSE_ERROR;
					}
				}
				*delim = '\0';
				parsed->value.type = PARCINI_VALUE_STRING;
				parsed->value.value.string = start + 1;
				return PARCINI_KEYVALUE;
			}

			if (cmnt) {
				if (start == cmnt) {
					return PARCINI_VALUE_PARSE_ERROR;
				}
				*cmnt = '\0';
				end = cmnt - 1;
			}
			rstrip(start, &end);

			if (!strcmp(start, "yes") || !strcmp(start, "true")) {
				parsed->value.type = PARCINI_VALUE_BOOLEAN;
				parsed->value.value.boolean = true;
				return PARCINI_KEYVALUE;
			}
			if (!strcmp(start, "no") || !strcmp(start, "false")) {
				parsed->value.type = PARCINI_VALUE_BOOLEAN;
				parsed->value.value.boolean = false;
				return PARCINI_KEYVALUE;
			}

			char *invalid = NULL;
			errno = ETIME;
			long int no = strtol(start, &invalid, 0);
			if ((*invalid && *invalid != '\n' && *invalid != '\r')
					|| errno == EINVAL) {
				return PARCINI_VALUE_PARSE_ERROR;
			}
			if (errno == ERANGE) {
				return PARCINI_VALUE_RANGE_ERROR;
			}
			parsed->value.type = PARCINI_VALUE_INTEGER;
			parsed->value.value.integer = no;
			return PARCINI_KEYVALUE;
		}

		lskip(&start);
		if (*start) {
			return PARCINI_KEY_PARSE_ERROR;
		}
	}

empty:
	return PARCINI_EMPTY_LINE;
}

parcini_t *
parcini_init(FILE *stream)
{
	parcini_t *parser = calloc(1, sizeof *parser);
	if (parser != NULL) {
		parser->stream = stream;
		parser->last_section = strdup("");
		parser->last_section_n = 1;
	}

	return parser;
}

parcini_t *
parcini_from_file(const char *fpath)
{
	FILE *f = NULL;
	f = fopen(fpath, "r");
	if (f == NULL) {
		return NULL;
	}
	return parcini_init(f);
}

parcini_t *
parcini_from_string(const char *str, const size_t len)
{
	FILE *stream = fmemopen((void *)str, len, "r");
	if (stream == NULL) {
		return NULL;
	}
	return parcini_init(stream);
}

void
parcini_destroy(parcini_t *parser)
{
	fclose(parser->stream);
	free(parser->last_line);
	free(parser->last_section);
	free(parser);
}