1 /* This file is part of the Zebra server.
2 Copyright (C) 1994-2011 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
25 #include <zebra_strmap.h>
27 #include <yaz/xmalloc.h>
33 struct strmap_entry *next;
41 struct strmap_entry **entries;
42 struct strmap_entry *free_entries;
45 zebra_strmap_t zebra_strmap_create(void)
48 NMEM nmem_ent = nmem_create();
49 zebra_strmap_t st = nmem_malloc(nmem_ent, sizeof(*st));
50 st->nmem_ent = nmem_ent;
51 st->nmem_str = nmem_create();
55 st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
56 for (i = 0; i < st->hsize; i++)
61 void zebra_strmap_destroy(zebra_strmap_t st)
65 nmem_destroy(st->nmem_str);
66 nmem_destroy(st->nmem_ent);
70 static struct strmap_entry **hash(zebra_strmap_t st, const char *name)
74 for (i = 0; name[i]; i++)
75 hash += hash*65519 + name[i];
76 hash = hash % st->hsize;
77 return st->entries + hash;
80 void zebra_strmap_add(zebra_strmap_t st, const char *name,
81 void *data_buf, size_t data_len)
83 struct strmap_entry **e = hash(st, name);
84 struct strmap_entry *ne = st->free_entries;
87 st->free_entries = ne->next;
89 ne = nmem_malloc(st->nmem_ent, sizeof(*ne));
92 ne->name = nmem_strdup(st->nmem_str, name);
93 ne->data_buf = nmem_malloc(st->nmem_str, data_len);
94 memcpy(ne->data_buf, data_buf, data_len);
95 ne->data_len = data_len;
99 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
102 struct strmap_entry *e = *hash(st, name);
104 for (; e ; e = e->next)
105 if (!strcmp(name, e->name))
110 *data_len = e->data_len;
118 int zebra_strmap_remove(zebra_strmap_t st, const char *name)
120 struct strmap_entry **e = hash(st, name);
121 for (; *e ; e = &(*e)->next)
122 if (!strcmp(name, (*e)->name))
124 struct strmap_entry *tmp = *e;
127 tmp->next = st->free_entries;
128 st->free_entries = tmp;
136 int zebra_strmap_get_size(zebra_strmap_t st)
141 struct zebra_strmap_it_s {
143 struct strmap_entry *ent;
148 zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st)
150 zebra_strmap_it it = (zebra_strmap_it) xmalloc(sizeof(*it));
157 void zebra_strmap_it_destroy(zebra_strmap_it it)
162 const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf,
165 struct strmap_entry *ent = 0;
166 while (!it->ent && it->hno < it->st->hsize)
168 it->ent = it->st->entries[it->hno];
179 *data_buf = ent->data_buf;
181 *data_len = ent->data_len;
190 * c-file-style: "Stroustrup"
191 * indent-tabs-mode: nil
193 * vim: shiftwidth=4 tabstop=8 expandtab