d3d7e6af5bb510762cfa9ee7191ed301a448b66c
[idzebra-moved-to-github.git] / index / symtab.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: symtab.c,v $
7  * Revision 1.5  1999-02-02 14:51:08  adam
8  * Updated WIN32 code specific sections. Changed header.
9  *
10  * Revision 1.4  1997/09/09 13:38:09  adam
11  * Partial port to WIN95/NT.
12  *
13  * Revision 1.3  1996/10/29 14:06:54  adam
14  * Include zebrautl.h instead of alexutil.h.
15  *
16  * Revision 1.2  1995/09/28 09:19:44  adam
17  * xfree/xmalloc used everywhere.
18  * Extract/retrieve method seems to work for text records.
19  *
20  * Revision 1.1  1995/09/06  16:11:18  adam
21  * Option: only one word key per file.
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 = 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 = xmalloc (sizeof(*e));
68     e->name = 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 }