aboutsummaryrefslogtreecommitdiff
path: root/config.c
blob: ba6041c0c046ee993351c99edbe85b8d33cf1ea9 (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
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "main.h"
#include "options.h"
#include "scale.h"

typedef enum {
	CE_TYPE_STR = 0,
	CE_TYPE_NUM = 4,
} config_entry_type;

#define CE_STR(val)	                                                  \
	{ #val, CE_TYPE_STRING, val }

#define CE_NUM(val)	                                                  \
	{ #val, CE_TYPE_NUM, &val }

static const struct {
	const char *name;
	config_entry_type type;
	void *val;
} config_data[] = {
	CE_NUM(show_fps),
	CE_NUM(limit_frames),
	CE_NUM(audio_buffer_size),
	CE_NUM(scale_size),
	CE_NUM(scale_filter),
};

void config_write(FILE *f)
{
	for (int i = 0; i < array_size(config_data); i++) {
		switch (config_data[i].type)
		{
		case CE_TYPE_STR:
			fprintf(f, "%s = %s\n", config_data[i].name, (char *)config_data[i].val);
			break;
		case CE_TYPE_NUM:
			fprintf(f, "%s = %u\n", config_data[i].name, *(uint32_t *)config_data[i].val);
			break;
		default:
			PA_WARN("unhandled type %d for %s\n", config_data[i].type, (char *)config_data[i].val);
			break;
		}
	}

	for (int i = 0; i < core_options.len; i++) {
		const char* k = options_get_key(i);
		if (!options_is_blocked(k))
			fprintf(f, "%s = %s\n", k, options_get_value(k));
	}
}

static void parse_str_val(char *cval, const char *src)
{
	char *tmp;
	strncpy(cval, src, 256);
	cval[256 - 1] = 0;
	tmp = strchr(cval, '\n');
	if (tmp == NULL)
		tmp = strchr(cval, '\r');
	if (tmp != NULL)
		*tmp = 0;
}

static void parse_num_val(uint32_t *cval, const char *src)
{
	char *tmp = NULL;
	uint32_t val;
	val = strtoul(src, &tmp, 10);
	if (tmp == NULL || src == tmp)
		return; // parse failed

	*cval = val;
}

static char *config_find_value(const char* cfg, const char *key) {
	char *tmp;

	tmp = strstr(cfg, key);
	if (tmp == NULL)
		return NULL;
	tmp += strlen(key);
	if (strncmp(tmp, " = ", 3) != 0)
		return NULL;
	tmp += 3;

	return tmp;
}

void config_read(const char* cfg)
{
	for (int i = 0; i < array_size(config_data); i++) {
		char *tmp = config_find_value(cfg, config_data[i].name);
		if (!tmp)
			continue;

		if (config_data[i].type == CE_TYPE_STR) {
			parse_str_val(config_data[i].val, tmp);
			continue;
		}

		parse_num_val(config_data[i].val, tmp);
	}

	for (int i = 0; i < core_options.len; i++) {
		char value[256] = {0};
		const char *key = options_get_key(i);
		if (options_is_blocked(key))
			continue;

		char *tmp = config_find_value(cfg, key);
		if (!tmp)
			continue;

		parse_str_val(value, tmp);
		options_set_value(key, value);
	}
}