2 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
12 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 2001 Nominum, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
24 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
26 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 /* $Id: alist.c,v 1.8 2007/08/28 07:20:43 tbox Exp $ */
41 #include <isccc/alist.h>
42 #include <isc/assertions.h>
43 #include <isccc/result.h>
44 #include <isccc/sexpr.h>
45 #include <isccc/util.h>
47 #define CAR(s) (s)->value.as_dottedpair.car
48 #define CDR(s) (s)->value.as_dottedpair.cdr
50 #define ALIST_TAG "*alist*"
53 static char spaces[MAX_INDENT + 1] =
57 isccc_alist_create(void)
59 isccc_sexpr_t *alist, *tag;
61 tag = isccc_sexpr_fromstring(ALIST_TAG);
64 alist = isccc_sexpr_cons(tag, NULL);
66 isccc_sexpr_free(&tag);
74 isccc_alist_alistp(isccc_sexpr_t *alist)
78 if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
81 if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
83 if (strcmp(car->value.as_string, ALIST_TAG) != 0)
89 isccc_alist_emptyp(isccc_sexpr_t *alist)
91 REQUIRE(isccc_alist_alistp(alist));
93 if (CDR(alist) == NULL)
99 isccc_alist_first(isccc_sexpr_t *alist)
101 REQUIRE(isccc_alist_alistp(alist));
107 isccc_alist_assq(isccc_sexpr_t *alist, const char *key)
109 isccc_sexpr_t *car, *caar;
111 REQUIRE(isccc_alist_alistp(alist));
114 * Skip alist type tag.
118 while (alist != NULL) {
119 INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
121 INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
123 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
124 strcmp(caar->value.as_string, key) == 0)
133 isccc_alist_delete(isccc_sexpr_t *alist, const char *key)
135 isccc_sexpr_t *car, *caar, *rest, *prev;
137 REQUIRE(isccc_alist_alistp(alist));
141 while (rest != NULL) {
142 INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
144 INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
146 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
147 strcmp(caar->value.as_string, key) == 0) {
148 CDR(prev) = CDR(rest);
150 isccc_sexpr_free(&rest);
159 isccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
161 isccc_sexpr_t *kv, *k, *elt;
163 kv = isccc_alist_assq(alist, key);
168 k = isccc_sexpr_fromstring(key);
171 kv = isccc_sexpr_cons(k, value);
173 isccc_sexpr_free(&kv);
176 elt = isccc_sexpr_addtolist(&alist, kv);
178 isccc_sexpr_free(&kv);
183 * We've already got an entry for this key. Replace it.
185 isccc_sexpr_free(&CDR(kv));
193 isccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
195 isccc_sexpr_t *v, *kv;
197 v = isccc_sexpr_fromstring(str);
200 kv = isccc_alist_define(alist, key, v);
202 isccc_sexpr_free(&v);
208 isccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
210 isccc_sexpr_t *v, *kv;
212 v = isccc_sexpr_frombinary(r);
215 kv = isccc_alist_define(alist, key, v);
217 isccc_sexpr_free(&v);
223 isccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
227 kv = isccc_alist_assq(alist, key);
234 isccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
236 isccc_sexpr_t *kv, *v;
238 kv = isccc_alist_assq(alist, key);
241 if (isccc_sexpr_stringp(v)) {
243 *strp = isccc_sexpr_tostring(v);
244 return (ISC_R_SUCCESS);
246 return (ISC_R_EXISTS);
249 return (ISC_R_NOTFOUND);
253 isccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
255 isccc_sexpr_t *kv, *v;
257 kv = isccc_alist_assq(alist, key);
260 if (isccc_sexpr_binaryp(v)) {
262 *r = isccc_sexpr_tobinary(v);
263 return (ISC_R_SUCCESS);
265 return (ISC_R_EXISTS);
268 return (ISC_R_NOTFOUND);
272 isccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
274 isccc_sexpr_t *elt, *kv, *k, *v;
276 if (isccc_alist_alistp(sexpr)) {
277 fprintf(stream, "{\n");
279 for (elt = isccc_alist_first(sexpr);
283 INSIST(isccc_sexpr_listp(kv));
286 INSIST(isccc_sexpr_stringp(k));
287 fprintf(stream, "%.*s%s => ", (int)indent, spaces,
288 isccc_sexpr_tostring(k));
289 isccc_alist_prettyprint(v, indent, stream);
290 if (CDR(elt) != NULL)
291 fprintf(stream, ",");
292 fprintf(stream, "\n");
295 fprintf(stream, "%.*s}", (int)indent, spaces);
296 } else if (isccc_sexpr_listp(sexpr)) {
297 fprintf(stream, "(\n");
302 fprintf(stream, "%.*s", (int)indent, spaces);
303 isccc_alist_prettyprint(CAR(elt), indent, stream);
304 if (CDR(elt) != NULL)
305 fprintf(stream, ",");
306 fprintf(stream, "\n");
309 fprintf(stream, "%.*s)", (int)indent, spaces);
311 isccc_sexpr_print(sexpr, stream);