1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2009 Index Data
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
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
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
22 #include <zebra_strmap.h>
24 #include <yaz/xmalloc.h>
30 struct strmap_entry *next;
38 struct strmap_entry **entries;
39 struct strmap_entry *free_entries;
42 zebra_strmap_t zebra_strmap_create(void)
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();
52 st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
53 for (i = 0; i < st->hsize; i++)
58 void zebra_strmap_destroy(zebra_strmap_t st)
62 nmem_destroy(st->nmem_str);
63 nmem_destroy(st->nmem_ent);
67 static struct strmap_entry **hash(zebra_strmap_t st, const char *name)
71 for (i = 0; name[i]; i++)
72 hash += hash*65519 + name[i];
73 hash = hash % st->hsize;
74 return st->entries + hash;
77 void zebra_strmap_add(zebra_strmap_t st, const char *name,
78 void *data_buf, size_t data_len)
80 struct strmap_entry **e = hash(st, name);
81 struct strmap_entry *ne = st->free_entries;
84 st->free_entries = ne->next;
86 ne = nmem_malloc(st->nmem_ent, sizeof(*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;
96 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
99 struct strmap_entry *e = *hash(st, name);
101 for (; e ; e = e->next)
102 if (!strcmp(name, e->name))
107 *data_len = e->data_len;
115 int zebra_strmap_remove(zebra_strmap_t st, const char *name)
117 struct strmap_entry **e = hash(st, name);
118 for (; *e ; e = &(*e)->next)
119 if (!strcmp(name, (*e)->name))
121 struct strmap_entry *tmp = *e;
124 tmp->next = st->free_entries;
125 st->free_entries = tmp;
133 int zebra_strmap_get_size(zebra_strmap_t st)
138 struct zebra_strmap_it_s {
140 struct strmap_entry *ent;
145 zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st)
147 zebra_strmap_it it = (zebra_strmap_it) xmalloc(sizeof(*it));
154 void zebra_strmap_it_destroy(zebra_strmap_it it)
159 const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf,
162 struct strmap_entry *ent = 0;
163 while (!it->ent && it->hno < it->st->hsize)
165 it->ent = it->st->entries[it->hno];
176 *data_buf = ent->data_buf;
178 *data_len = ent->data_len;
187 * c-file-style: "Stroustrup"
188 * indent-tabs-mode: nil
190 * vim: shiftwidth=4 tabstop=8 expandtab