aboutsummaryrefslogtreecommitdiff
path: root/src/config.c
blob: 7c1d1732a70655f013226e75885e02a2d5a3faea (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include "config.h"

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

#include "fs.h"
#include "log.h"
#include "parcini.h"

typedef enum kv_handler_result (*ini_keyvalue_handler_fn)(struct parcini_line *, void *dst);

enum config_key_result {
	CONFIG_KEY_NONE,
	CONFIG_KEY_OK,
	CONFIG_KEY_BADKEY,
	CONFIG_KEY_BADVALUE,
};

enum kv_handler_result {
	KV_HANDLER_OK,
	KV_HANDLER_NOMATCH,
	KV_HANDLER_BADVALUE,
};

static int
site_config_images_keyvalue_handler(struct parcini_line *parsed,
                                    struct image_config *iconfig)
{
	int res = CONFIG_KEY_BADKEY;
	if (!strcmp(parsed->key, "strip")) {
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_BOOLEAN,
		                           &iconfig->strip)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
	}
	if (!strcmp(parsed->key, "quality")) {
		long int temp;
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER,
		                           &temp)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
		if (res == CONFIG_KEY_OK) {
			if (temp > 100 || temp < 0) {
				res = CONFIG_KEY_BADVALUE;
			} else {
				iconfig->quality = (uint8_t)temp;
			}
		}
	}
	if (!strcmp(parsed->key, "max_width")) {
		long int temp;
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER,
		                           &temp)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
		if (res == CONFIG_KEY_OK) {
			if (temp < 1) {
				res = CONFIG_KEY_BADVALUE;
			} else {
				iconfig->max_width = (size_t)temp;
			}
		}
	}
	if (!strcmp(parsed->key, "max_height")) {
		long int temp;
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER,
		                           &temp)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
		if (res == CONFIG_KEY_OK) {
			if (temp < 1) {
				res = CONFIG_KEY_BADVALUE;
			} else {
				iconfig->max_height = (size_t)temp;
			}
		}
	}
	if (!strcmp(parsed->key, "smart_resize")) {
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_BOOLEAN,
		                           &iconfig->smart_resize)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
	}
	if (!strcmp(parsed->key, "blur")) {
		long int temp;
		res = parcini_value_handle(&parsed->value, PARCINI_VALUE_INTEGER,
		                           &temp)
		        ? CONFIG_KEY_OK
		        : CONFIG_KEY_BADVALUE;
		if (res == CONFIG_KEY_OK) {
			if (temp < -100 || temp > 100) {
				res = CONFIG_KEY_BADVALUE;
			} else {
				iconfig->blur = (double)temp / 100;
			}
		}
	}

	return res;
}

#define MATCHSK(s, k, p) !strcmp(s, p->section) && !strcmp(k, p->key)

static enum kv_handler_result
site_config_keyvalue_handler(struct parcini_line *parsed, void *dst)
{
	struct site_config *config = dst;
	enum config_key_result subconf = CONFIG_KEY_NONE;
	if (!strcmp(parsed->section, "images")) {
		subconf = site_config_images_keyvalue_handler(parsed, &config->images);
	} else if (!strcmp(parsed->section, "thumbnails")) {
		subconf = site_config_images_keyvalue_handler(parsed,
		                                              &config->thumbnails);
	}
	switch (subconf) {
	case CONFIG_KEY_OK:
		return KV_HANDLER_OK;
	case CONFIG_KEY_BADKEY:
		goto out;
	case CONFIG_KEY_BADVALUE:
		return KV_HANDLER_BADVALUE;
	default:
		break;
	}
	if (MATCHSK("", "title", parsed)) {
		free(config->title);
		return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
		                            &config->title)
		         ? KV_HANDLER_OK
		         : KV_HANDLER_BADVALUE;
	}
	if (MATCHSK("", "base_url", parsed)) {
		free(config->base_url);
		return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
		                            &config->base_url)
		         ? KV_HANDLER_OK
		         : KV_HANDLER_BADVALUE;
	}

out:
	return KV_HANDLER_NOMATCH;
}

static enum kv_handler_result
album_config_keyvalue_handler(struct parcini_line *parsed, void *dst)
{
	struct album_config *config = dst;
	if (MATCHSK("", "title", parsed)) {
		free(config->title);
		return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
		                            &config->title)
		         ? KV_HANDLER_OK
		         : KV_HANDLER_BADVALUE;
	}
	if (MATCHSK("", "desc", parsed)) {
		free(config->desc);
		return parcini_value_handle(&parsed->value, PARCINI_VALUE_STRING,
		                            &config->desc)
		         ? KV_HANDLER_OK
		         : KV_HANDLER_BADVALUE;
	}

	return KV_HANDLER_NOMATCH;
}

static bool
ini_handler(const char *fpath, ini_keyvalue_handler_fn kvhandler,
            void *dst)
{
	struct parcini_line parsed;
	enum parcini_result res;
	parcini_t *parser = parcini_from_file(fpath);
	if (parser == NULL) {
		log_printl_errno(LOG_FATAL, "Couldn't open %s", fpath);
		return false;
	}

	bool ok = true;
	enum kv_handler_result hres;
	while ((res = parcini_parse_next_line(parser, &parsed)) != PARCINI_EOF
	       && ok) {
		switch (res) {
		case PARCINI_KEYVALUE:
			if ((hres = kvhandler(&parsed, dst)) == KV_HANDLER_BADVALUE) {
				log_printl(LOG_ERROR, "Error parsing %s:%ld, bad value for key"
				                      "%s",
				           fpath, parsed.lineno, parsed.key);
				ok = false;
			} else if (hres == KV_HANDLER_NOMATCH) {
				log_printl(LOG_ERROR,
				           "Warning: key '%s' in section '%s' is not a valid "
				           "section-key combination for %s, ignoring.",
				           parsed.key, parsed.section, fpath);
			}
			continue;

		case PARCINI_EMPTY_LINE:
		case PARCINI_SECTION:
			continue;

		case PARCINI_STREAM_ERROR:
		case PARCINI_MEMORY_ERROR:
			log_printl_errno(LOG_FATAL, "Error reading %s", fpath);
			ok = false;
			break;

		default:
			log_printl(LOG_FATAL, "Error parsing %s:%ld", fpath, parsed.lineno);
			ok = false;
			break;
		}
	}

	parcini_destroy(parser);
	return ok;
}

bool
site_config_read_ini(const char *wdir, struct site_config *config)
{
	if (wdir == NULL) {
		return ini_handler(SITE_CONF, site_config_keyvalue_handler,
		                   config);
	}
	char *fpath = joinpath(wdir, SITE_CONF);
	bool ok = ini_handler(fpath, site_config_keyvalue_handler, config);
	free(fpath);
	return ok;
}

bool
album_config_read_ini(const char *fpath, struct album_config *config)
{
	bool ok = ini_handler(fpath, album_config_keyvalue_handler, config);
	return ok;
}

struct site_config *
site_config_init(void)
{
	struct site_config *config = calloc(1, sizeof *config);
	if (config != NULL) {
		config->images = (struct image_config){
			.strip = true,
			.quality = 80,
			.max_width = 3000,
			.max_height = 2000,
			.smart_resize = true,
			.blur = 0,
		};
		config->thumbnails = (struct image_config){
			.strip = true,
			.quality = 60,
			.max_width = 400,
			.max_height = 270,
			.smart_resize = true,
			.blur = 0.25,
		};
	}

	return config;
}

struct album_config *
album_config_init(void)
{
	struct album_config *config = calloc(1, sizeof *config);
	return config;
}

void
site_config_destroy(struct site_config *config)
{
	free(config->title);
	free(config->base_url);
	free(config);
}

void
album_config_destroy(struct album_config *config)
{
	free(config->title);
	free(config->desc);
	free(config);
}