No empty .Rs/.Re
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / dict_ht.c
blob2fa40c2f1d1411c7b034dd62973a7ff688b7eea5
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* dict_ht 3
6 /* SUMMARY
7 /* dictionary manager interface to hash tables
8 /* SYNOPSIS
9 /* #include <dict_ht.h>
11 /* DICT *dict_ht_open(name, table, remove)
12 /* const char *name;
13 /* HTABLE *table;
14 /* void (*remove)(char *value)
15 /* DESCRIPTION
16 /* dict_ht_open() makes specified hash table accessible via the
17 /* generic dictionary operations documented in dict_open(3).
18 /* \fIremove\fR specifies an optional callback function
19 /* that is called by the hash table manager when the hash table is
20 /* removed from the dictionary manager's care. The hash table is not
21 /* destroyed when \fIremove\fR is a null pointer.
22 /* SEE ALSO
23 /* dict(3) generic dictionary manager
24 /* LICENSE
25 /* .ad
26 /* .fi
27 /* The Secure Mailer license must be distributed with this software.
28 /* AUTHOR(S)
29 /* Wietse Venema
30 /* IBM T.J. Watson Research
31 /* P.O. Box 704
32 /* Yorktown Heights, NY 10598, USA
33 /*--*/
35 /* System library. */
37 #include "sys_defs.h"
39 /* Utility library. */
41 #include "mymalloc.h"
42 #include "htable.h"
43 #include "dict.h"
44 #include "dict_ht.h"
46 /* Application-specific. */
48 typedef struct {
49 DICT dict; /* generic members */
50 HTABLE *table; /* hash table */
51 void (*remove) (char *); /* callback */
52 } DICT_HT;
54 /* dict_ht_lookup - find hash-table entry */
56 static const char *dict_ht_lookup(DICT *dict, const char *name)
58 DICT_HT *dict_ht = (DICT_HT *) dict;
60 dict_errno = 0;
62 return (htable_find(dict_ht->table, name));
65 /* dict_ht_update - add or update hash-table entry */
67 static void dict_ht_update(DICT *dict, const char *name, const char *value)
69 DICT_HT *dict_ht = (DICT_HT *) dict;
70 HTABLE_INFO *ht;
72 if ((ht = htable_locate(dict_ht->table, name)) != 0) {
73 myfree(ht->value);
74 } else {
75 ht = htable_enter(dict_ht->table, name, (char *) 0);
77 ht->value = mystrdup(value);
80 /* dict_ht_close - disassociate from hash table */
82 static void dict_ht_close(DICT *dict)
84 DICT_HT *dict_ht = (DICT_HT *) dict;
86 if (dict_ht->remove)
87 htable_free(dict_ht->table, dict_ht->remove);
88 dict_free(dict);
91 /* dict_ht_open - create association with hash table */
93 DICT *dict_ht_open(const char *name, HTABLE *table, void (*remove) (char *))
95 DICT_HT *dict_ht;
97 dict_ht = (DICT_HT *) dict_alloc(DICT_TYPE_HT, name, sizeof(*dict_ht));
98 dict_ht->dict.lookup = dict_ht_lookup;
99 dict_ht->dict.update = dict_ht_update;
100 dict_ht->dict.close = dict_ht_close;
101 dict_ht->table = table;
102 dict_ht->remove = remove;
103 return (&dict_ht->dict);