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