2007.
[idzebra-moved-to-github.git] / index / symtab.c
1 /* $Id: symtab.c,v 1.13 2007-01-15 15:10:17 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <yaz/xmalloc.h>
28
29 struct strentry {
30     char *name;
31     void *info;
32     struct strentry *next;
33 };
34
35 #define STR_HASH 401
36
37 struct strtab {
38     struct strentry *ar[STR_HASH];
39 };
40
41 struct strtab *strtab_mk (void)
42 {
43     int i;
44     struct strtab *p = (struct strtab *) xmalloc (sizeof (*p));
45     for (i=0; i<STR_HASH; i++)
46         p->ar[i] = NULL;
47     return p;
48 }
49
50 int strtab_src (struct strtab *t, const char *name, void ***infop)
51 {
52     unsigned hash = 0;
53     int i;
54     struct strentry *e;
55
56     for (i=0; name[i]; i++)
57         hash += hash*65519 + name[i];
58     hash = hash % STR_HASH;
59     for (e = t->ar[hash]; e; e = e->next)
60         if (!strcmp(e->name, name))
61         {
62             *infop = &e->info;
63             return 1;
64         }
65     e = (struct strentry *) xmalloc (sizeof(*e));
66     e->name = (char *) xmalloc (strlen(name)+1);
67     strcpy (e->name, name);
68     e->next = t->ar[hash];
69     t->ar[hash] = e;
70     *infop = &e->info;
71     return 0;
72 }
73
74 void strtab_del (struct strtab *t,
75                  void (*func)(const char *name, void *info, void *data),
76                  void *data)
77 {
78     int i;
79     struct strentry *e, *e1;
80
81     for (i = 0; i<STR_HASH; i++)
82         for (e = t->ar[i]; e; e = e1)
83         {
84             e1 = e->next;
85             (*func)(e->name, e->info, data);
86             xfree (e->name);
87             xfree (e);
88         }
89     xfree (t);
90 }
91 /*
92  * Local variables:
93  * c-basic-offset: 4
94  * indent-tabs-mode: nil
95  * End:
96  * vim: shiftwidth=4 tabstop=8 expandtab
97  */
98