2 * Portions Copyright (C) 2004, 2005 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.3.18.2 2005/04/29 00:17:11 marka Exp $ */
28 #include <isccc/alist.h>
29 #include <isc/assertions.h>
30 #include <isccc/result.h>
31 #include <isccc/sexpr.h>
32 #include <isccc/util.h>
34 #define CAR(s) (s)->value.as_dottedpair.car
35 #define CDR(s) (s)->value.as_dottedpair.cdr
37 #define ALIST_TAG "*alist*"
40 static char spaces[MAX_INDENT + 1] =
44 isccc_alist_create(void)
46 isccc_sexpr_t *alist, *tag;
48 tag = isccc_sexpr_fromstring(ALIST_TAG);
51 alist = isccc_sexpr_cons(tag, NULL);
53 isccc_sexpr_free(&tag);
61 isccc_alist_alistp(isccc_sexpr_t *alist)
65 if (alist == NULL || alist->type != ISCCC_SEXPRTYPE_DOTTEDPAIR)
68 if (car == NULL || car->type != ISCCC_SEXPRTYPE_STRING)
70 if (strcmp(car->value.as_string, ALIST_TAG) != 0)
76 isccc_alist_emptyp(isccc_sexpr_t *alist)
78 REQUIRE(isccc_alist_alistp(alist));
80 if (CDR(alist) == NULL)
86 isccc_alist_first(isccc_sexpr_t *alist)
88 REQUIRE(isccc_alist_alistp(alist));
94 isccc_alist_assq(isccc_sexpr_t *alist, const char *key)
96 isccc_sexpr_t *car, *caar;
98 REQUIRE(isccc_alist_alistp(alist));
101 * Skip alist type tag.
105 while (alist != NULL) {
106 INSIST(alist->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
108 INSIST(car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
110 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
111 strcmp(caar->value.as_string, key) == 0)
120 isccc_alist_delete(isccc_sexpr_t *alist, const char *key)
122 isccc_sexpr_t *car, *caar, *rest, *prev;
124 REQUIRE(isccc_alist_alistp(alist));
128 while (rest != NULL) {
129 INSIST(rest->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
131 INSIST(car != NULL && car->type == ISCCC_SEXPRTYPE_DOTTEDPAIR);
133 if (caar->type == ISCCC_SEXPRTYPE_STRING &&
134 strcmp(caar->value.as_string, key) == 0) {
135 CDR(prev) = CDR(rest);
137 isccc_sexpr_free(&rest);
146 isccc_alist_define(isccc_sexpr_t *alist, const char *key, isccc_sexpr_t *value)
148 isccc_sexpr_t *kv, *k, *elt;
150 kv = isccc_alist_assq(alist, key);
155 k = isccc_sexpr_fromstring(key);
158 kv = isccc_sexpr_cons(k, value);
160 isccc_sexpr_free(&kv);
163 elt = isccc_sexpr_addtolist(&alist, kv);
165 isccc_sexpr_free(&kv);
170 * We've already got an entry for this key. Replace it.
172 isccc_sexpr_free(&CDR(kv));
180 isccc_alist_definestring(isccc_sexpr_t *alist, const char *key, const char *str)
182 isccc_sexpr_t *v, *kv;
184 v = isccc_sexpr_fromstring(str);
187 kv = isccc_alist_define(alist, key, v);
189 isccc_sexpr_free(&v);
195 isccc_alist_definebinary(isccc_sexpr_t *alist, const char *key, isccc_region_t *r)
197 isccc_sexpr_t *v, *kv;
199 v = isccc_sexpr_frombinary(r);
202 kv = isccc_alist_define(alist, key, v);
204 isccc_sexpr_free(&v);
210 isccc_alist_lookup(isccc_sexpr_t *alist, const char *key)
214 kv = isccc_alist_assq(alist, key);
221 isccc_alist_lookupstring(isccc_sexpr_t *alist, const char *key, char **strp)
223 isccc_sexpr_t *kv, *v;
225 kv = isccc_alist_assq(alist, key);
228 if (isccc_sexpr_stringp(v)) {
230 *strp = isccc_sexpr_tostring(v);
231 return (ISC_R_SUCCESS);
233 return (ISC_R_EXISTS);
236 return (ISC_R_NOTFOUND);
240 isccc_alist_lookupbinary(isccc_sexpr_t *alist, const char *key, isccc_region_t **r)
242 isccc_sexpr_t *kv, *v;
244 kv = isccc_alist_assq(alist, key);
247 if (isccc_sexpr_binaryp(v)) {
249 *r = isccc_sexpr_tobinary(v);
250 return (ISC_R_SUCCESS);
252 return (ISC_R_EXISTS);
255 return (ISC_R_NOTFOUND);
259 isccc_alist_prettyprint(isccc_sexpr_t *sexpr, unsigned int indent, FILE *stream)
261 isccc_sexpr_t *elt, *kv, *k, *v;
263 if (isccc_alist_alistp(sexpr)) {
264 fprintf(stream, "{\n");
266 for (elt = isccc_alist_first(sexpr);
270 INSIST(isccc_sexpr_listp(kv));
273 INSIST(isccc_sexpr_stringp(k));
274 fprintf(stream, "%.*s%s => ", (int)indent, spaces,
275 isccc_sexpr_tostring(k));
276 isccc_alist_prettyprint(v, indent, stream);
277 if (CDR(elt) != NULL)
278 fprintf(stream, ",");
279 fprintf(stream, "\n");
282 fprintf(stream, "%.*s}", (int)indent, spaces);
283 } else if (isccc_sexpr_listp(sexpr)) {
284 fprintf(stream, "(\n");
289 fprintf(stream, "%.*s", (int)indent, spaces);
290 isccc_alist_prettyprint(CAR(elt), indent, stream);
291 if (CDR(elt) != NULL)
292 fprintf(stream, ",");
293 fprintf(stream, "\n");
296 fprintf(stream, "%.*s)", (int)indent, spaces);
298 isccc_sexpr_print(sexpr, stream);