2 * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 2001 Internet Software Consortium.
4 * Portions Copyright (C) 2001 Nominum, Inc.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 /* $Id: alist.c,v 1.2.206.1 2004/03/06 08:15:18 marka Exp $ */
26 #include <isccc/alist.h>
27 #include <isc/assertions.h>
28 #include <isccc/result.h>
29 #include <isccc/sexpr.h>
30 #include <isccc/util.h>
32 #define CAR(s) (s)->value.as_dottedpair.car
33 #define CDR(s) (s)->value.as_dottedpair.cdr
35 #define ALIST_TAG "*alist*"
38 static char spaces[MAX_INDENT + 1] =
42 isccc_alist_create(void)
44 isccc_sexpr_t *alist, *tag;
46 tag = isccc_sexpr_fromstring(ALIST_TAG);
49 alist = isccc_sexpr_cons(tag, NULL);
51 isccc_sexpr_free(&tag);
59 isccc_alist_alistp(isccc_sexpr_t *alist)
63 if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
66 if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
68 if (strcmp(car->value.as_string, ALIST_TAG) != 0)
74 isccc_alist_emptyp(isccc_sexpr_t *alist)
76 REQUIRE(isccc_alist_alistp(alist));
78 if (CDR(alist) == NULL)
84 isccc_alist_first(isccc_sexpr_t *alist)
86 REQUIRE(isccc_alist_alistp(alist));
92 isccc_alist_assq(isccc_sexpr_t *alist, const char *key)
94 isccc_sexpr_t *car, *caar;
96 REQUIRE(isccc_alist_alistp(alist));
99 * Skip alist type tag.
103 while (alist != NULL) {
104 INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
106 INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
108 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
109 strcmp(caar->value.as_string, key) == 0)
118 isccc_alist_delete(isccc_sexpr_t *alist, const char *key)
120 isccc_sexpr_t *car, *caar, *rest, *prev;
122 REQUIRE(isccc_alist_alistp(alist));
126 while (rest != NULL) {
127 INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
129 INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
131 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
132 strcmp(caar->value.as_string, key) == 0) {
133 CDR(prev) = CDR(rest);
135 isccc_sexpr_free(&rest);
144 isccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
146 isccc_sexpr_t *kv, *k, *elt;
148 kv = isccc_alist_assq(alist, key);
153 k = isccc_sexpr_fromstring(key);
156 kv = isccc_sexpr_cons(k, value);
158 isccc_sexpr_free(&kv);
161 elt = isccc_sexpr_addtolist(&alist, kv);
163 isccc_sexpr_free(&kv);
168 * We've already got an entry for this key. Replace it.
170 isccc_sexpr_free(&CDR(kv));
178 isccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
180 isccc_sexpr_t *v, *kv;
182 v = isccc_sexpr_fromstring(str);
185 kv = isccc_alist_define(alist, key, v);
187 isccc_sexpr_free(&v);
193 isccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
195 isccc_sexpr_t *v, *kv;
197 v = isccc_sexpr_frombinary(r);
200 kv = isccc_alist_define(alist, key, v);
202 isccc_sexpr_free(&v);
208 isccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
212 kv = isccc_alist_assq(alist, key);
219 isccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
221 isccc_sexpr_t *kv, *v;
223 kv = isccc_alist_assq(alist, key);
226 if (isccc_sexpr_stringp(v)) {
228 *strp = isccc_sexpr_tostring(v);
229 return (ISC_R_SUCCESS);
231 return (ISC_R_EXISTS);
234 return (ISC_R_NOTFOUND);
238 isccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
240 isccc_sexpr_t *kv, *v;
242 kv = isccc_alist_assq(alist, key);
245 if (isccc_sexpr_binaryp(v)) {
247 *r = isccc_sexpr_tobinary(v);
248 return (ISC_R_SUCCESS);
250 return (ISC_R_EXISTS);
253 return (ISC_R_NOTFOUND);
257 isccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
259 isccc_sexpr_t *elt, *kv, *k, *v;
261 if (isccc_alist_alistp(sexpr)) {
262 fprintf(stream, "{\n");
264 for (elt = isccc_alist_first(sexpr);
268 INSIST(isccc_sexpr_listp(kv));
271 INSIST(isccc_sexpr_stringp(k));
272 fprintf(stream, "%.*s%s => ", (int)indent, spaces,
273 isccc_sexpr_tostring(k));
274 isccc_alist_prettyprint(v, indent, stream);
275 if (CDR(elt) != NULL)
276 fprintf(stream, ",");
277 fprintf(stream, "\n");
280 fprintf(stream, "%.*s}", (int)indent, spaces);
281 } else if (isccc_sexpr_listp(sexpr)) {
282 fprintf(stream, "(\n");
287 fprintf(stream, "%.*s", (int)indent, spaces);
288 isccc_alist_prettyprint(CAR(elt), indent, stream);
289 if (CDR(elt) != NULL)
290 fprintf(stream, ",");
291 fprintf(stream, "\n");
294 fprintf(stream, "%.*s)", (int)indent, spaces);
296 isccc_sexpr_print(sexpr, stream);