0ad56b6d2c2d647e88502684023bd68d65263269
[idzebra-moved-to-github.git] / util / strmap.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stddef.h>
21 #include <string.h>
22 #include <zebra_strmap.h>
23 #include <yaz/nmem.h>
24 #include <yaz/xmalloc.h>
25
26 struct strmap_entry {
27     char *name;
28     size_t data_len;
29     void *data_buf;
30     struct strmap_entry *next;
31 };
32     
33 struct zebra_strmap {
34     NMEM nmem_str;
35     NMEM nmem_ent;
36     int hsize;
37     int size;
38     struct strmap_entry **entries;
39     struct strmap_entry *free_entries;
40 };
41
42 zebra_strmap_t zebra_strmap_create(void)
43 {
44     int i;
45     NMEM nmem_ent = nmem_create();
46     zebra_strmap_t st = nmem_malloc(nmem_ent, sizeof(*st));
47     st->nmem_ent = nmem_ent;
48     st->nmem_str = nmem_create();
49     st->hsize = 1001;
50     st->size = 0;
51     st->free_entries = 0;
52     st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
53     for (i = 0; i < st->hsize; i++)
54         st->entries[i] = 0;
55     return st;
56 }
57
58 void zebra_strmap_destroy(zebra_strmap_t st)
59 {
60     if (st)
61     {
62         nmem_destroy(st->nmem_str);
63         nmem_destroy(st->nmem_ent);
64     }
65 }
66
67 static struct strmap_entry **hash(zebra_strmap_t st, const char *name)
68 {
69     unsigned hash = 0;
70     int i;
71     for (i = 0; name[i]; i++)
72         hash += hash*65519 + name[i];
73     hash = hash % st->hsize;
74     return st->entries + hash;
75 }
76
77 void zebra_strmap_add(zebra_strmap_t st, const char *name,
78                       void *data_buf, size_t data_len)
79 {
80     struct strmap_entry **e = hash(st, name);
81     struct strmap_entry *ne = st->free_entries;
82
83     if (ne)
84         st->free_entries = ne->next;
85     else
86         ne = nmem_malloc(st->nmem_ent, sizeof(*ne));
87     ne->next = *e;
88     *e = ne;
89     ne->name = nmem_strdup(st->nmem_str, name);
90     ne->data_buf = nmem_malloc(st->nmem_str, data_len);
91     memcpy(ne->data_buf, data_buf, data_len);
92     ne->data_len = data_len;
93     (st->size)++;
94 }
95
96 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
97                           size_t *data_len)
98 {
99     struct strmap_entry *e = *hash(st, name);
100     int i = 0;
101     for (; e ; e = e->next)
102         if (!strcmp(name, e->name))
103         {
104             if (i == no)
105             {
106                 if (data_len)
107                     *data_len = e->data_len;
108                 return e->data_buf;
109             }
110             i++;
111         }
112     return 0;
113 }
114
115 int zebra_strmap_remove(zebra_strmap_t st, const char *name)
116 {
117     struct strmap_entry **e = hash(st, name);
118     for (; *e ; e = &(*e)->next)
119         if (!strcmp(name, (*e)->name))
120         {
121             struct strmap_entry *tmp = *e;
122             *e = (*e)->next;
123
124             tmp->next = st->free_entries;
125             st->free_entries = tmp;
126
127             --(st->size);
128             return 1;
129         }
130     return 0;
131 }
132
133 int zebra_strmap_get_size(zebra_strmap_t st)
134 {
135     return st->size;
136 }
137
138 struct zebra_strmap_it_s {
139     int hno;
140     struct strmap_entry *ent;
141     zebra_strmap_t st;
142     
143 };
144
145 zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st)
146 {
147     zebra_strmap_it it = (zebra_strmap_it) xmalloc(sizeof(*it));
148     it->hno = 0;
149     it->ent = 0;
150     it->st = st;
151     return it;
152 }
153
154 void zebra_strmap_it_destroy(zebra_strmap_it it)
155 {
156     xfree(it);
157 }
158
159 const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf,
160                                  size_t *data_len)
161 {
162     struct strmap_entry *ent = 0;
163     while (!it->ent && it->hno < it->st->hsize)
164     {
165         it->ent = it->st->entries[it->hno];
166         it->hno++;
167     }
168     if (it->ent)
169     {
170         ent = it->ent;
171         it->ent = ent->next;
172     }
173     if (ent)
174     {
175         if (data_buf)
176             *data_buf = ent->data_buf;
177         if (data_len)
178             *data_len = ent->data_len;
179         return ent->name;
180     }
181     return 0;
182 }
183                  
184 /*
185  * Local variables:
186  * c-basic-offset: 4
187  * indent-tabs-mode: nil
188  * End:
189  * vim: shiftwidth=4 tabstop=8 expandtab
190  */
191