cd188688cc099cbaf70715e01932d41fc545fa06
[idzebra-moved-to-github.git] / index / symtab.c
1 /* $Id: symtab.c,v 1.7 2002-08-02 19:26:55 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "index.h"
30
31 struct strentry {
32     char *name;
33     void *info;
34     struct strentry *next;
35 };
36
37 #define STR_HASH 401
38
39 struct strtab {
40     struct strentry *ar[STR_HASH];
41 };
42
43 struct strtab *strtab_mk (void)
44 {
45     int i;
46     struct strtab *p = (struct strtab *) xmalloc (sizeof (*p));
47     for (i=0; i<STR_HASH; i++)
48         p->ar[i] = NULL;
49     return p;
50 }
51
52 int strtab_src (struct strtab *t, const char *name, void ***infop)
53 {
54     unsigned hash = 0;
55     int i;
56     struct strentry *e;
57
58     for (i=0; name[i]; i++)
59         hash += hash*65519 + name[i];
60     hash = hash % STR_HASH;
61     for (e = t->ar[hash]; e; e = e->next)
62         if (!strcmp(e->name, name))
63         {
64             *infop = &e->info;
65             return 1;
66         }
67     e = (struct strentry *) xmalloc (sizeof(*e));
68     e->name = (char *) xmalloc (strlen(name)+1);
69     strcpy (e->name, name);
70     e->next = t->ar[hash];
71     t->ar[hash] = e;
72     *infop = &e->info;
73     return 0;
74 }
75
76 void strtab_del (struct strtab *t,
77                  void (*func)(const char *name, void *info, void *data),
78                  void *data)
79 {
80     int i;
81     struct strentry *e, *e1;
82
83     for (i = 0; i<STR_HASH; i++)
84         for (e = t->ar[i]; e; e = e1)
85         {
86             e1 = e->next;
87             (*func)(e->name, e->info, data);
88             xfree (e->name);
89             xfree (e);
90         }
91     xfree (t);
92 }