~lattis/muon

ref: 85283983c022b571ca80d040d866215509ad0afe muon/src/functions/configuration_data.c -rw-r--r-- 4.1 KiB
85283983Stone Tickle implement very basic pc file generation 8 months ago
                                                                                
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
#include "posix.h"

#include <string.h>

#include "functions/common.h"
#include "functions/configuration_data.h"
#include "lang/interpreter.h"
#include "log.h"

static bool
ensure_not_in(struct workspace *wk, uint32_t node, uint32_t dict, uint32_t key)
{
	if (obj_dict_in(wk, dict, key)) {
		interp_error(wk, node, "duplicate key in configuration_data");
		return false;
	}

	return true;
}

static bool
func_configuration_data_set_quoted(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, { obj_string }, ARG_TYPE_NULL };
	enum kwargs {
		kw_description, // TODO
	};
	struct args_kw akw[] = {
		[kw_description] = { "description", obj_string, },
		0
	};

	if (!interp_args(wk, args_node, an, NULL, akw)) {
		return false;
	}

	uint32_t dict = get_obj(wk, rcvr)->dat.configuration_data.dict;
	if (!ensure_not_in(wk, an[0].node, dict, an[0].val)) {
		return false;
	}

	uint32_t val;
	const char *s = wk_objstr(wk, an[1].val);
	uint32_t str = wk_str_push(wk, "\"");

	for (; *s; ++s) {
		if (*s == '"') {
			wk_str_app(wk, &str, "\\");
		}

		wk_str_appn(wk, &str, s, 1);
	}

	wk_str_app(wk, &str, "\"");

	make_obj(wk, &val, obj_string)->dat.str = str;
	obj_dict_set(wk, dict, an[0].val, val);

	return true;
}

static bool
func_configuration_data_set(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, { obj_any }, ARG_TYPE_NULL };
	enum kwargs {
		kw_description, // TODO
	};
	struct args_kw akw[] = {
		[kw_description] = { "description", obj_string, },
		0
	};

	if (!interp_args(wk, args_node, an, NULL, akw)) {
		return false;
	}

	uint32_t dict = get_obj(wk, rcvr)->dat.configuration_data.dict;

	if (!ensure_not_in(wk, an[0].node, dict, an[0].val)) {
		return false;
	}

	obj_dict_set(wk, dict, an[0].val, an[1].val);

	return true;
}

static bool
configuration_data_get(struct workspace *wk, uint32_t err_node, uint32_t conf,
	uint32_t key, uint32_t def, uint32_t *obj)
{
	uint32_t dict = get_obj(wk, conf)->dat.configuration_data.dict;

	if (!obj_dict_index(wk, dict, key, obj)) {
		if (def) {
			*obj = def;
		} else {
			interp_error(wk, err_node, "key '%s' not found", wk_objstr(wk, key));
			return false;
		}
	}

	return true;
}

static bool
func_configuration_data_get(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	struct args_norm ao[] = { { obj_any }, ARG_TYPE_NULL };

	if (!interp_args(wk, args_node, an, ao, NULL)) {
		return false;
	}

	return configuration_data_get(wk, an[0].node, rcvr, an[0].val, ao[0].val, obj);
}

static bool
func_configuration_data_get_unquoted(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	struct args_norm an[] = { { obj_string }, ARG_TYPE_NULL };
	struct args_norm ao[] = { { obj_any }, ARG_TYPE_NULL };

	if (!interp_args(wk, args_node, an, ao, NULL)) {
		return false;
	}

	uint32_t res;
	if (!configuration_data_get(wk, an[0].node, rcvr, an[0].val, ao[0].val, &res)) {
		return false;
	}

	const char *s = wk_objstr(wk, res);
	uint32_t l = strlen(s);

	if (l >= 2 && s[0] == '"' && s[l - 1] == '"') {
		make_obj(wk, obj, obj_string)->dat.str = wk_str_pushn(wk, &s[1], l - 2);
	} else {
		*obj = res;
	}

	return true;
}

static enum iteration_result
obj_dict_keys_iter(struct workspace *wk, void *_ctx, uint32_t k, uint32_t _v)
{
	uint32_t *obj = _ctx;

	obj_array_push(wk, *obj, k);

	return ir_cont;
}

static bool
func_configuration_data_keys(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	if (!interp_args(wk, args_node, NULL, NULL, NULL)) {
		return false;
	}

	uint32_t dict = get_obj(wk, rcvr)->dat.configuration_data.dict;

	make_obj(wk, obj, obj_array);
	obj_dict_foreach(wk, dict, obj, obj_dict_keys_iter);
	return true;
}

const struct func_impl_name impl_tbl_configuration_data[] = {
	{ "set", func_configuration_data_set },
	{ "set_quoted", func_configuration_data_set_quoted },
	{ "get", func_configuration_data_get },
	{ "get_unquoted", func_configuration_data_get_unquoted },
	{ "keys", func_configuration_data_keys },
	{ NULL, NULL },
};