summaryrefslogtreecommitdiff
path: root/src/templating/mustach-jansson.c
blob: d9b50b57e00cfc3848f7f6bc1762689a53cd23be (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
/*
 Author: José Bollo <jobol@nonadev.net>

 https://gitlab.com/jobol/mustach

 SPDX-License-Identifier: ISC
*/

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include <stdio.h>
#include <string.h>

#include "mustach.h"
#include "mustach-wrap.h"
#include "mustach-jansson.h"

struct expl {
	json_t *root;
	json_t *selection;
	int depth;
	struct {
		json_t *cont;
		json_t *obj;
		void *iter;
		int is_objiter;
		size_t index, count;
	} stack[MUSTACH_MAX_DEPTH];
};

static int start(void *closure)
{
	struct expl *e = closure;
	e->depth = 0;
	e->selection = json_null();
	e->stack[0].cont = NULL;
	e->stack[0].obj = e->root;
	e->stack[0].index = 0;
	e->stack[0].count = 1;
	return MUSTACH_OK;
}

static int compare(void *closure, const char *value)
{
	struct expl *e = closure;
	json_t *o = e->selection;
	double d;
	json_int_t i;

	switch (json_typeof(o)) {
	case JSON_REAL:
		d = json_number_value(o) - atof(value);
		return d < 0 ? -1 : d > 0 ? 1 : 0;
	case JSON_INTEGER:
		i = (json_int_t)json_integer_value(o) - (json_int_t)atoll(value);
		return i < 0 ? -1 : i > 0 ? 1 : 0;
	case JSON_STRING:
		return strcmp(json_string_value(o), value);
	case JSON_TRUE:
		return strcmp("true", value);
	case JSON_FALSE:
		return strcmp("false", value);
	case JSON_NULL:
		return strcmp("null", value);
	default:
		return 1;
	}
}

static int sel(void *closure, const char *name)
{
	struct expl *e = closure;
	json_t *o;
	int i, r;

	if (name == NULL) {
		o = e->stack[e->depth].obj;
		r = 1;
	} else {
		i = e->depth;
		while (i >= 0 && !(o = json_object_get(e->stack[i].obj, name)))
			i--;
		if (i >= 0)
			r = 1;
		else {
			o = json_null();
			r = 0;
		}
	}
	e->selection = o;
	return r;
}

static int subsel(void *closure, const char *name)
{
	struct expl *e = closure;
	json_t *o = NULL;
	int r = 0;

	if (json_is_object(e->selection)) {
		o = json_object_get(e->selection, name);
		r = o != NULL;
	}
	else if (json_is_array(e->selection)) {
		char *end;
		size_t idx = (size_t)strtol(name, &end, 10);
		if (!*end && idx < json_array_size(e->selection)) {
			o = json_array_get(e->selection, idx);
			r = 1;
		}		
	}
	if (r)
		e->selection = o;
	return r;
}

static int enter(void *closure, int objiter)
{
	struct expl *e = closure;
	json_t *o;

	if (++e->depth >= MUSTACH_MAX_DEPTH)
		return MUSTACH_ERROR_TOO_DEEP;

	o = e->selection;
	e->stack[e->depth].is_objiter = 0;
	if (objiter) {
		if (!json_is_object(o))
			goto not_entering;
		e->stack[e->depth].iter = json_object_iter(o);
		if (e->stack[e->depth].iter == NULL)
			goto not_entering;
		e->stack[e->depth].obj = json_object_iter_value(e->stack[e->depth].iter);
		e->stack[e->depth].cont = o;
		e->stack[e->depth].is_objiter = 1;
	} else if (json_is_array(o)) {
		e->stack[e->depth].count = json_array_size(o);
		if (e->stack[e->depth].count == 0)
			goto not_entering;
		e->stack[e->depth].cont = o;
		e->stack[e->depth].obj = json_array_get(o, 0);
		e->stack[e->depth].index = 0;
	} else if ((json_is_object(o) && json_object_size(o))
                || json_is_true(o)
	        || (json_is_string(o) && json_string_length(o) > 0)
	        || (json_is_integer(o) && json_integer_value(o) != 0)
	        || (json_is_real(o) && json_real_value(o) != 0)) {
		e->stack[e->depth].count = 1;
		e->stack[e->depth].cont = NULL;
		e->stack[e->depth].obj = o;
		e->stack[e->depth].index = 0;
	} else
		goto not_entering;
	return 1;

not_entering:
	e->depth--;
	return 0;
}

static int next(void *closure)
{
	struct expl *e = closure;

	if (e->depth <= 0)
		return MUSTACH_ERROR_CLOSING;

	if (e->stack[e->depth].is_objiter) {
		e->stack[e->depth].iter = json_object_iter_next(e->stack[e->depth].cont, e->stack[e->depth].iter);
		if (e->stack[e->depth].iter == NULL)
			return 0;
		e->stack[e->depth].obj = json_object_iter_value(e->stack[e->depth].iter);
		return 1;
	}

	e->stack[e->depth].index++;
	if (e->stack[e->depth].index >= e->stack[e->depth].count)
		return 0;

	e->stack[e->depth].obj = json_array_get(e->stack[e->depth].cont, e->stack[e->depth].index);
	return 1;
}

static int leave(void *closure)
{
	struct expl *e = closure;

	if (e->depth <= 0)
		return MUSTACH_ERROR_CLOSING;

	e->depth--;
	return 0;
}

static int get(void *closure, struct mustach_sbuf *sbuf, int key)
{
	struct expl *e = closure;
	const char *s;
	int d;

	if (key) {
		s = "";
		for (d = e->depth ; d >= 0 ; d--)
			if (e->stack[d].is_objiter) {
				s = json_object_iter_key(e->stack[d].iter);
				break;
			}
	}
	else if (json_is_string(e->selection))
		s = json_string_value(e->selection);
	else if (json_is_null(e->selection))
		s = "";
	else {
		s = json_dumps(e->selection, JSON_ENCODE_ANY | JSON_COMPACT);
		if (s == NULL)
			return MUSTACH_ERROR_SYSTEM;
		sbuf->freecb = free;
	}
	sbuf->value = s;
	return 1;
}

const struct mustach_wrap_itf mustach_jansson_wrap_itf = {
	.start = start,
	.stop = NULL,
	.compare = compare,
	.sel = sel,
	.subsel = subsel,
	.enter = enter,
	.next = next,
	.leave = leave,
	.get = get
};

int mustach_jansson_file(const char *template, size_t length, json_t *root, int flags, FILE *file)
{
	struct expl e;
	e.root = root;
	return mustach_wrap_file(template, length, &mustach_jansson_wrap_itf, &e, flags, file);
}

int mustach_jansson_fd(const char *template, size_t length, json_t *root, int flags, int fd)
{
	struct expl e;
	e.root = root;
	return mustach_wrap_fd(template, length, &mustach_jansson_wrap_itf, &e, flags, fd);
}

int mustach_jansson_mem(const char *template, size_t length, json_t *root, int flags, char **result, size_t *size)
{
	struct expl e;
	e.root = root;
	return mustach_wrap_mem(template, length, &mustach_jansson_wrap_itf, &e, flags, result, size);
}

int mustach_jansson_write(const char *template, size_t length, json_t *root, int flags, mustach_write_cb_t *writecb, void *closure)
{
	struct expl e;
	e.root = root;
	return mustach_wrap_write(template, length, &mustach_jansson_wrap_itf, &e, flags, writecb, closure);
}

int mustach_jansson_emit(const char *template, size_t length, json_t *root, int flags, mustach_emit_cb_t *emitcb, void *closure)
{
	struct expl e;
	e.root = root;
	return mustach_wrap_emit(template, length, &mustach_jansson_wrap_itf, &e, flags, emitcb, closure);
}