aboutsummaryrefslogtreecommitdiffstats
path: root/dict/pdict.c
blob: 04ac84c9f78c1a420ae460c37b40a4bdd531f7c8 (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include "../stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <regex.h>
#if defined(_WINDOWS) && !defined(_CYGWIN)
#include "wincompat.h"
#endif
#include "utils.h"
#include "plist.h"
#include "ptree.h"
#include "pdict-impl.h"

typedef struct pdict_ent pdict_ent_t;
typedef int (*pdict_walk_int_func_t)(pdict_ent_t *pde, void *arg);

static int _pdict_ent_add_persistent_change_listeners(pdict_t *pd,
    pdict_ent_t *pde);
static int _pdict_ent_add_persistent_change_listener(pdict_ent_t *pde,
    pdict_persistent_listener_t *pl);
static int _pdict_ent_remove_persistent_change_listener(pdict_ent_t *pde,
    pdict_persistent_listener_t *pl);
static void _pdict_ent_notify(pdict_ent_t *pde, int reason, const char *ov);
static int _pdict_ent_add_change_listener(pdict_ent_t *pde,
    pdl_notify_func_t notify, void *arg);
static int _pdict_walk_int(pdict_t *pd, pdict_walk_int_func_t w, void *arg);
static int _pdict_ent_remove_change_listener(pdict_ent_t *pde,
    pdl_notify_func_t notify, void *a);

pdict_t *
pdict_alloc(void)
{
	pdict_t *pd;

	if (!(pd = malloc(sizeof (*pd))))
		return 0;
	memset(pd, 0, sizeof (*pd));

	return pd;
}

static int
pdecmp(const void *sv, const void *dv)
{
	return strcmp(*(char **)sv, *(char **)dv);
}

static int
pdict_ent_remove_change_listeners_cb(const void *v, const void *v0, void *a)
{
	free((void *)v); v = NULL;
	return (1);
}

static int
pdict_ent_remove_change_listeners(pdict_ent_t *pde)
{
	plist_walk(pde->pde_listeners, pdict_ent_remove_change_listeners_cb, 0);
	plist_clear(&pde->pde_listeners);
	return 1;
}

static int
pdict_ent_listeners_copy_cb(const void *v, const void *v0, void *a)
{
	pdict_listener_t *pdl = (pdict_listener_t *)v;
	pdict_ent_t *pde_ent_copy = a;

	return _pdict_ent_add_change_listener(pde_ent_copy, pdl->pdl_notify, pdl->pdl_arg);
}

static void
_pdict_ent_listeners_copy(pdict_ent_t *pde, pdict_ent_t *pde_copy)
{
	plist_walk(pde->pde_listeners, pdict_ent_listeners_copy_cb, pde_copy);
}

/*
 * Sets or resets an entry to the dictionary, returning 0 on failure.
 */
int
pdict_add(pdict_t *pd, const char *k, const char *v, const char **ovp)
{
	pdict_ent_t *n;
	pdict_ent_t n_copy;
	const char *ov;

	if (!(k = strdup(k)))
		return 0;
	if (!(v = strdup(v))) {
		free((void *)k); k = NULL;
		return 0;
	}
	memset(&n_copy, 0, sizeof(pdict_ent_t));
	if (ptree_contains((void *)&k, pd->pd_ents, pdecmp, (void *)&n)) {
		free((void *)k); k = NULL;
		ov = n->pde_val;
		n->pde_val = v;

		if (ovp)
			*ovp = ov;
		else {
			free((void *)ov); ov = NULL;
		}
		
		//We copy n so that it's safe if it gets removed during a callback
		if(n->pde_listeners)
		{
			n_copy.pde_key = strdup(n->pde_key);
			n_copy.pde_val = strdup(n->pde_val);
			_pdict_ent_listeners_copy(n, &n_copy);
			_pdict_ent_notify(&n_copy, PDR_VALUE_CHANGED, ov);
			pdict_ent_remove_change_listeners(&n_copy);
			free((char *)n_copy.pde_key);
			free((char *)n_copy.pde_val);
		}
		return 1;
	}
	if (!(n = malloc(sizeof (*n)))) {
		free((void *)k); k = NULL;
		free((void *)v); v = NULL;
		return (0);
	}
	memset(n, 0, sizeof (*n));
	n->pde_key = k;
	n->pde_val = v;

	//Add dict listeners to the dict entry object
	if (!_pdict_ent_add_persistent_change_listeners(pd, n)) {
		free((void *)k); k = NULL;
		free((void *)v); v = NULL;
		free(n); n = NULL;
		return (0);
	}
	//Add the dict entry to the dict (replacing if there is a matching key).
	if (!ptree_replace(n, &pd->pd_ents, pdecmp, NULL)) {
		pdict_ent_remove_change_listeners(n);
		free((void *)k); k = NULL;
		free((void *)v); v = NULL;
		free(n); n = NULL;
		return (0);
	}
	//notify the listeners
	if(n->pde_listeners)
	{
		n_copy.pde_key = strdup(n->pde_key);
		n_copy.pde_val = strdup(n->pde_val);
		_pdict_ent_listeners_copy(n, &n_copy);
		_pdict_ent_notify(&n_copy, PDR_ENTRY_ADDED, n_copy.pde_val);
		pdict_ent_remove_change_listeners(&n_copy);
		free((char *)n_copy.pde_key);
		free((char *)n_copy.pde_val);
	}

	if (ovp)
		*ovp = NULL;

	return 1;
}

int
pdict_ent_remove(pdict_t *pd, const char *k, char **ovp)
{
	pdict_ent_t *n;

	pu_log(PUL_VERB, 0, "Removing in key pdict_ent_remove: %s", k);
	
	if (!ptree_remove((void *)&k, &pd->pd_ents, pdecmp, (void *)&n))
	{
		//pu_log(PUL_INFO, 0, "Failed to remove key in pdict_ent_remove: %s", k);
		return 0;
	}
	
	_pdict_ent_notify(n, PDR_ENTRY_REMOVING, n->pde_val);
	
	if (ovp)
		*ovp = (char *)n->pde_val;
	else {
		free((void *)n->pde_val);
	}
	free((void *)n->pde_key);
	pdict_ent_remove_change_listeners(n);
	free(n);
	return 1;
}

static int
pdict_ent_remove_persistent_change_listener_cb(const void *pl, const void *v,
    void *a)
{
	_pdict_ent_remove_persistent_change_listener((pdict_ent_t *)a,
	    (pdict_persistent_listener_t *)pl);
	return 1;
}

static int
pdict_ent_add_persistent_change_listener_cb(const void *lid, const void *pl,
    void *a)
{
	return _pdict_ent_add_persistent_change_listener((pdict_ent_t *)a,
	    (pdict_persistent_listener_t *)pl);
}

static int
_pdict_ent_add_persistent_change_listeners(pdict_t *pd, pdict_ent_t *pde)
{
	if (!plist_walk(pd->pd_persistent_listeners, pdict_ent_add_persistent_change_listener_cb, pde)) {
		plist_walk(pd->pd_persistent_listeners, pdict_ent_remove_persistent_change_listener_cb, pde);
		pu_log(PUL_WARN, 0, "Failed to add persistent change listener in _pdict_ent_add_persistent_change_listeners.");
		return 0;
	}
	return 1;
}

static int
pdict_ent_remove_persistent_change_listener_dcb(pdict_ent_t *pde, void *pl)
{
	return _pdict_ent_remove_persistent_change_listener(pde,
	    (pdict_persistent_listener_t *)pl);
}

static int
pdict_ent_add_persistent_change_listener_dcb(pdict_ent_t *pde, void *pl)
{
	return _pdict_ent_add_persistent_change_listener(pde,
	    (pdict_persistent_listener_t *)pl);
}

static int
_pdict_ent_add_persistent_change_listener(pdict_ent_t *pde,
    pdict_persistent_listener_t *pdpl)
{
	int res;

	if ((res = regexec(&pdpl->pdpl_regex, pde->pde_key, 0, NULL, 0)) != 0)
	{
		return res == REG_NOMATCH;
	}
	if (!_pdict_ent_add_change_listener(pde, pdpl->pdpl_l.pdl_notify, pdpl->pdpl_l.pdl_arg))
	{
		pu_log(PUL_WARN, 0, "Failed to add persistent change listener in _pdict_ent_add_persistent_change_listener.");
		return 0;
	}
	if (pdpl->pdpl_new)
		pdpl->pdpl_l.pdl_notify(pde->pde_key, pde->pde_val, PDR_CURRENT_VALUE, NULL, pdpl->pdpl_l.pdl_arg);
	return 1;
}

static int
_pdict_ent_remove_persistent_change_listener(pdict_ent_t *pde,
    pdict_persistent_listener_t *pl)
{
	_pdict_ent_remove_change_listener(pde, pl->pdpl_l.pdl_notify,
	    pl->pdpl_l.pdl_arg);
	return 1;
}

int
pdict_remove_persistent_change_listener(pdict_t *pd, int id)
{
	pdict_persistent_listener_t *pdpl;

	if (!plist_remove((void *)(size_t)id, &pd->pd_persistent_listeners, (void **)&pdpl) || !pdpl)
	{
		pu_log(PUL_WARN, 0, "Failed plist_remove in pdict_remove_persistent_change_listener.");
		return 0;
	}
	if (!_pdict_walk_int(pd, pdict_ent_remove_persistent_change_listener_dcb, pdpl))
	{
		pu_log(PUL_WARN, 0, "Failed _pdict_walk_int in pdict_remove_persistent_change_listener.");
		return 0;
	}
	regfree(&pdpl->pdpl_regex);
	free(pdpl); pdpl = NULL;
	return 1;
}

int
pdict_add_persistent_change_listener(pdict_t *pd, const char *kpat,
    pdl_notify_func_t notify, void *arg)
{
	pdict_persistent_listener_t *pl;
	static int lid = 1;

	if (!(pl = malloc(sizeof (*pl))))
		return 0;
	memset(pl, 0, sizeof (*pl));
	pl->pdpl_l.pdl_notify = notify;
	pl->pdpl_l.pdl_arg = arg;
	if (regcomp(&pl->pdpl_regex, kpat, REG_EXTENDED | REG_NOSUB) != 0) {
		// XXX todo: communicate error context is not libc
		free(pl); pl = NULL;
		pu_log(PUL_WARN, 0, "Failed regcomp in pdict_add_persistent_change_listener.");
		return 0;
	}

	plist_add((void *)(size_t)lid, pl, &pd->pd_persistent_listeners);

	pl->pdpl_new = 1;
	if (!_pdict_walk_int(pd,
	    pdict_ent_add_persistent_change_listener_dcb, pl)) {
		_pdict_walk_int(pd,
		    pdict_ent_remove_persistent_change_listener_dcb, pl);
		plist_remove((void *)(size_t)lid, &pd->pd_persistent_listeners, NULL);
		regfree(&pl->pdpl_regex);
		free(pl); pl = NULL;
		pu_log(PUL_WARN, 0, "Failed _pdict_walk_int in pdict_add_persistent_change_listener.");
		return 0;
	}
	pl->pdpl_new = 0;
	return lid++;
}

/*
 * Return whether a given key is in the dictionary.  If the given
 * entry pointer is non-NULL, set it to the entry.
 */
static int
_pdict_ent_lookup(pdict_t *pd, const char *k, pdict_ent_t **e)
{
	return ptree_contains((void *)&k, pd->pd_ents, pdecmp, (void **)e);
}

int
pdict_ent_lookup(pdict_t *pd, const char *k, const char **v)
{
	pdict_ent_t *pde;

	if (_pdict_ent_lookup(pd, k, &pde)) {
		if (v)
			*v = strdup(pde->pde_val);
		return 1;
	}
	return 0;
}

static int
pdict_ent_remove_change_listener_cb(const void *k, const void *v, void *a)
{
	pdict_listener_t *l = (pdict_listener_t *)k;
	void **arg = (void **)a;

	if (l->pdl_notify == (pdl_notify_func_t)arg[0] && l->pdl_arg == arg[1]) {
		arg[2] = l;
		return 0;
	}
	return 1;
}

static int
_pdict_ent_remove_change_listener(pdict_ent_t *pde, pdl_notify_func_t notify,
    void *a)
{
	void *arg[3];

	arg[0] = (void *)notify;
	arg[1] = a;
	arg[2] = NULL;
	plist_walk(pde->pde_listeners, pdict_ent_remove_change_listener_cb, arg);
	if (arg[2]) {
		plist_remove(arg[2], &pde->pde_listeners, NULL);
		free(arg[2]); arg[2] = NULL;
		return 1;
	}
	return 0;
}

static int
_pdict_ent_add_change_listener(pdict_ent_t *pde, pdl_notify_func_t notify,
    void *arg)
{
	pdict_listener_t *l;

	if (!(l = malloc(sizeof (*l))))
		return 0;
	memset(l, 0, sizeof (*l));
	l->pdl_notify = notify;
	l->pdl_arg = arg;
	if (!plist_add(l, 0, &pde->pde_listeners)) {
		free(l); l = NULL;
		pu_log(PUL_WARN, 0, "Failed plist_add in _pdict_ent_add_change_listener.");
		return 0;
	}

	return 1;
}

int
pdict_ent_add_change_listener(pdict_t *pd, const char *k,
    pdl_notify_func_t notify, void *arg)
{
	pdict_ent_t *pde;

	if (!_pdict_ent_lookup(pd, k, &pde))
		return 0;
	return _pdict_ent_add_change_listener(pde, notify, arg);
}

typedef struct {
	pdict_ent_t *penca_pde;
	pdict_reason_t penca_reason;
	const char *penca_ov;
} pdict_ent_notify_cb_args_t;

static int
pdict_ent_notify_cb(const void *v, const void *v0, void *a)
{
	pdict_listener_t *pdl = (pdict_listener_t *)v;
	pdict_ent_notify_cb_args_t *penca = a;

	pdl->pdl_notify(penca->penca_pde->pde_key, penca->penca_pde->pde_val,
	    penca->penca_reason, penca->penca_ov, pdl->pdl_arg);
	return 1;
}

static void
_pdict_ent_notify(pdict_ent_t *pde, int reason, const char *ov)
{
	pdict_ent_notify_cb_args_t penca = { pde, reason, ov };

	plist_walk(pde->pde_listeners, pdict_ent_notify_cb, &penca);
}

static ptree_walk_res_t
pdict_walk_int_cb(const void *v, int level, void *a, void *pwra)
{
	void **args = a;

	return ((pdict_walk_int_func_t)args[0])((pdict_ent_t *)v, args[1]);
}

static ptree_walk_res_t
pdict_walk_cb(const void *v, int level, void *a, void *pwra)
{
	pdict_ent_t *pde = (pdict_ent_t *)v;
	void **args = a;

	return ((pdict_walk_func_t)args[0])(pde->pde_key, pde->pde_val,
	    args[1]);
}

static int
_pdict_walk_int(pdict_t *pd, pdict_walk_int_func_t w, void *arg)
{
	void *args[2] = { (void *)w, arg };

	return ptree_walk(pd->pd_ents, PTREE_INORDER, pdict_walk_int_cb, pdecmp, args);
}

int
pdict_walk(pdict_t *pd, pdict_walk_func_t w, void *arg)
{
	void *args[2] = { (void *)w, arg };

	return ptree_walk(pd->pd_ents, PTREE_INORDER, pdict_walk_cb, NULL, args);
}

int
pdict_ent_remove_change_listener(pdict_t *pd, const char *k,
    pdl_notify_func_t nf, void *arg)
{
	pdict_ent_t *e;
	int res;

	if (!(res = _pdict_ent_lookup(pd, k, &e)))
		return 0;
	return _pdict_ent_remove_change_listener(e, nf, arg);
}

const char *
pdict_reason_str(pdict_reason_t r)
{
	switch (r) {
	case PDR_VALUE_CHANGED:
		return "changed";
	case PDR_ENTRY_ADDED:
		return "added";
	case PDR_ENTRY_REMOVING:
		return "removing";
	case PDR_CURRENT_VALUE:
		return "current";
	default:
		return "?";
	}
}

pdict_reason_t
pdict_reason_from_str(const char *s)
{
	if (strcmp(s, "changed") == 0)
		return PDR_VALUE_CHANGED;
	if (strcmp(s, "current") == 0)
		return PDR_CURRENT_VALUE;
	if (strcmp(s, "added") == 0)
		return PDR_ENTRY_ADDED;
	if (strcmp(s, "removing") == 0)
		return PDR_ENTRY_REMOVING;
	return 0;
}