aboutsummaryrefslogtreecommitdiff
path: root/lib/dict.c
blob: 83b89a76e7fdcd4f04f8d157098352d51b545154 (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
/* This file is part of Mailfromd.
   Copyright (C) 2007, 2008 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <mailutils/assoc.h>
#include <mailutils/errno.h>
#include "xalloc.h"


/* Rudimentary dictionary support */
struct dict_entry {
	char *value;
};

void
name_destroy(void *item)
{
	struct dict_entry *ent = item;
	free(ent->value);
}

void
dict_init(mu_assoc_t *dict)
{
	mu_assoc_create(dict, sizeof (struct dict_entry), 0);
	mu_assoc_set_free(*dict, name_destroy);
}

char *
dict_install(mu_assoc_t dict, const char *name, const char *value)
{
	struct dict_entry *p;
	int rc;
	
	rc = mu_assoc_ref_install (dict, name, (void **)&p);
	if (rc == MU_ERR_EXISTS) 
		free (p->value);
	p->value = xstrdup (value);
	return 0;
}

void
dict_destroy(mu_assoc_t *dict)
{
	mu_assoc_destroy(dict);
}

char *
dict_getsym(void *data, const char *str)
{
	mu_assoc_t dict = data;
	struct dict_entry *p;
	char *tmp = NULL;
	char *val = NULL;
	
	if (str[0] == '{') {
		int len = strlen(str);
		if (str[len-1] == '}') {
			tmp = malloc(len-1);
			if (!tmp)
				abort();
			memcpy(tmp, str+1, len-2);
			tmp[len-2] = 0;
			str = tmp;
		}
	}

	p = mu_assoc_ref(dict, str);
	if (p)
		val = p->value;
	free(tmp);
	return val;
}

Return to:

Send suggestions and report system problems to the System administrator.