Include zebrautl.h instead of alexutil.h.
[idzebra-moved-to-github.git] / index / symtab.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: symtab.c,v $
7  * Revision 1.3  1996-10-29 14:06:54  adam
8  * Include zebrautl.h instead of alexutil.h.
9  *
10  * Revision 1.2  1995/09/28 09:19:44  adam
11  * xfree/xmalloc used everywhere.
12  * Extract/retrieve method seems to work for text records.
13  *
14  * Revision 1.1  1995/09/06  16:11:18  adam
15  * Option: only one word key per file.
16  *
17  */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <assert.h>
21
22 #include "index.h"
23
24 struct strentry {
25     char *name;
26     void *info;
27     struct strentry *next;
28 };
29
30 #define STR_HASH 401
31
32 struct strtab {
33     struct strentry *ar[STR_HASH];
34 };
35
36 struct strtab *strtab_mk (void)
37 {
38     int i;
39     struct strtab *p = xmalloc (sizeof (*p));
40     for (i=0; i<STR_HASH; i++)
41         p->ar[i] = NULL;
42     return p;
43 }
44
45 int strtab_src (struct strtab *t, const char *name, void ***infop)
46 {
47     unsigned hash = 0;
48     int i;
49     struct strentry *e;
50
51     for (i=0; name[i]; i++)
52         hash += hash*65519 + name[i];
53     hash = hash % STR_HASH;
54     for (e = t->ar[hash]; e; e = e->next)
55         if (!strcmp(e->name, name))
56         {
57             *infop = &e->info;
58             return 1;
59         }
60     e = xmalloc (sizeof(*e));
61     e->name = xmalloc (strlen(name)+1);
62     strcpy (e->name, name);
63     e->next = t->ar[hash];
64     t->ar[hash] = e;
65     *infop = &e->info;
66     return 0;
67 }
68
69 void strtab_del (struct strtab *t,
70                  void (*func)(const char *name, void *info, void *data),
71                  void *data)
72 {
73     int i;
74     struct strentry *e, *e1;
75
76     for (i = 0; i<STR_HASH; i++)
77         for (e = t->ar[i]; e; e = e1)
78         {
79             e1 = e->next;
80             (*func)(e->name, e->info, data);
81             xfree (e->name);
82             xfree (e);
83         }
84     xfree (t);
85 }