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