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