Blob


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