2 8d1b399b 2021-07-22 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 8d1b399b 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
5 8d1b399b 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
6 8d1b399b 2021-07-22 op * copyright notice and this permission notice appear in all copies.
8 8d1b399b 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 8d1b399b 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 8d1b399b 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 8d1b399b 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 8d1b399b 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 8d1b399b 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 8d1b399b 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 8d1b399b 2021-07-22 op #include "compat.h"
19 8d1b399b 2021-07-22 op #include <stdlib.h>
20 f89beeaa 2021-07-22 op #include <string.h>
22 8d1b399b 2021-07-22 op #include "kamid.h"
23 8d1b399b 2021-07-22 op #include "utils.h"
25 8d1b399b 2021-07-22 op static void *hash_alloc(size_t, void *);
26 8d1b399b 2021-07-22 op static void *hash_calloc(size_t, size_t, void *);
27 8d1b399b 2021-07-22 op static void hash_free(void *, void *);
29 8d1b399b 2021-07-22 op static int table_static_open(struct table *);
30 8d1b399b 2021-07-22 op static int table_static_add(struct table *, const char *, const char *);
31 8d1b399b 2021-07-22 op static int table_static_lookup(struct table *, const char *, char **);
32 8d1b399b 2021-07-22 op static void table_static_close(struct table *);
34 8d1b399b 2021-07-22 op struct table_backend table_static = {
36 8d1b399b 2021-07-22 op table_static_open,
37 8d1b399b 2021-07-22 op table_static_add,
38 8d1b399b 2021-07-22 op table_static_lookup,
39 8d1b399b 2021-07-22 op table_static_close,
48 8d1b399b 2021-07-22 op hash_alloc(size_t len, void *d)
50 8d1b399b 2021-07-22 op return xmalloc(len);
54 8d1b399b 2021-07-22 op hash_calloc(size_t nmemb, size_t size, void *d)
56 8d1b399b 2021-07-22 op return xcalloc(nmemb, size);
60 8d1b399b 2021-07-22 op hash_free(void *ptr, void *d)
66 8d1b399b 2021-07-22 op table_static_open(struct table *t)
68 8d1b399b 2021-07-22 op struct ohash_info info = {
69 8d1b399b 2021-07-22 op .key_offset = offsetof(struct kp, key),
70 8d1b399b 2021-07-22 op .calloc = hash_calloc,
71 8d1b399b 2021-07-22 op .free = hash_free,
72 8d1b399b 2021-07-22 op .alloc = hash_alloc,
75 8d1b399b 2021-07-22 op t->t_handle = xmalloc(sizeof(struct ohash));
76 8d1b399b 2021-07-22 op ohash_init(t->t_handle, 5, &info);
81 8d1b399b 2021-07-22 op table_static_add(struct table *t, const char *key, const char *val)
83 8d1b399b 2021-07-22 op struct kp *kp;
84 8d1b399b 2021-07-22 op unsigned int slot;
86 72d0ec48 2021-12-15 op if (key == NULL)
89 72d0ec48 2021-12-15 op kp = xcalloc(1, sizeof(*kp) + strlen(key) + 1);
90 72d0ec48 2021-12-15 op strcpy(kp->key, key);
91 8d1b399b 2021-07-22 op if (val != NULL)
92 8d1b399b 2021-07-22 op kp->val = xstrdup(val);
94 8d1b399b 2021-07-22 op slot = ohash_qlookup(t->t_handle, kp->key);
95 8d1b399b 2021-07-22 op ohash_insert(t->t_handle, slot, kp);
101 8d1b399b 2021-07-22 op table_static_lookup(struct table *t, const char *key, char **ret_val)
103 8d1b399b 2021-07-22 op struct kp *kp;
104 8d1b399b 2021-07-22 op unsigned int slot;
106 8d1b399b 2021-07-22 op slot = ohash_qlookup(t->t_handle, key);
107 8d1b399b 2021-07-22 op if ((kp = ohash_find(t->t_handle, slot)) == NULL)
110 8d1b399b 2021-07-22 op *ret_val = xstrdup(kp->val);
115 8d1b399b 2021-07-22 op table_static_close(struct table *t)
117 8d1b399b 2021-07-22 op struct kp *kp;
118 8d1b399b 2021-07-22 op unsigned int i;
120 8d1b399b 2021-07-22 op for (kp = ohash_first(t->t_handle, &i);
122 8d1b399b 2021-07-22 op kp = ohash_next(t->t_handle, &i)) {
123 8d1b399b 2021-07-22 op ohash_remove(t->t_handle, i);
124 8d1b399b 2021-07-22 op free(kp->key);
125 8d1b399b 2021-07-22 op free(kp->val);
129 8d1b399b 2021-07-22 op free(t->t_handle);