1 /*
2 * Copyright (c) 1989 Jan-Simon Pendry
3 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry at Imperial College, London.
9 *
10 * %sccs.include.redist.c%
11 *
12 * @(#)fsi_dict.c 8.1 (Berkeley) 06/06/93
13 *
14 * $Id: fsi_dict.c,v 5.2.2.1 1992/02/09 15:09:43 jsp beta $
15 *
16 */
17
18 /*
19 * Dictionary support
20 */
21
22 #include "../fsinfo/fsinfo.h"
23
dict_hash(k)24 static int dict_hash(k)
25 char *k;
26 {
27 unsigned int h;
28
29 for (h = 0; *k; h += *k++)
30 ;
31 return h % DICTHASH;
32 }
33
new_dict()34 dict *new_dict()
35 {
36 dict *dp = ALLOC(dict);
37 return dp;
38 }
39
dict_add_data(de,v)40 static void dict_add_data(de, v)
41 dict_ent *de;
42 char *v;
43 {
44 dict_data *dd = ALLOC(dict_data);
45 dd->dd_data = v;
46 ins_que(&dd->dd_q, de->de_q.q_back);
47 de->de_count++;
48 }
49
new_dict_ent(k)50 static dict_ent *new_dict_ent(k)
51 char *k;
52 {
53 dict_ent *de = ALLOC(dict_ent);
54 de->de_key = k;
55 init_que(&de->de_q);
56 return de;
57 }
58
dict_locate(dp,k)59 dict_ent *dict_locate(dp, k)
60 dict *dp;
61 char *k;
62 {
63 dict_ent *de = dp->de[dict_hash(k)];
64 while (de && !STREQ(de->de_key, k))
65 de = de->de_next;
66
67 return de;
68 }
69
dict_add(dp,k,v)70 void dict_add(dp, k, v)
71 dict *dp;
72 char *k, *v;
73 {
74 dict_ent *de = dict_locate(dp, k);
75 if (!de) {
76 dict_ent **dep = &dp->de[dict_hash(k)];
77 de = new_dict_ent(k);
78 de->de_next = *dep;
79 *dep = de;
80 }
81 dict_add_data(de, v);
82 }
83
dict_iter(dp,fn)84 int dict_iter(dp, fn)
85 dict *dp;
86 int (*fn)();
87 {
88 int i;
89 int errors = 0;
90
91 for (i = 0; i < DICTHASH; i++) {
92 dict_ent *de = dp->de[i];
93 while (de) {
94 errors += (*fn)(&de->de_q);
95 de = de->de_next;
96 }
97 }
98 return errors;
99 }
100