1 /* $NetBSD: alist.c,v 1.4 2014/12/10 04:38:01 christos Exp $ */
4 * Portions Copyright (C) 2004, 2005, 2007 Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * Portions Copyright (C) 2001 Nominum, Inc.
21 * Permission to use, copy, modify, and/or distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
34 /* Id: alist.c,v 1.8 2007/08/28 07:20:43 tbox Exp */
43 #include <isccc/alist.h>
44 #include <isc/assertions.h>
45 #include <isccc/result.h>
46 #include <isccc/sexpr.h>
47 #include <isccc/util.h>
49 #define CAR(s) (s)->value.as_dottedpair.car
50 #define CDR(s) (s)->value.as_dottedpair.cdr
52 #define ALIST_TAG "*alist*"
55 static char spaces
[MAX_INDENT
+ 1] =
59 isccc_alist_create(void)
61 isccc_sexpr_t
*alist
, *tag
;
63 tag
= isccc_sexpr_fromstring(ALIST_TAG
);
66 alist
= isccc_sexpr_cons(tag
, NULL
);
68 isccc_sexpr_free(&tag
);
76 isccc_alist_alistp(isccc_sexpr_t
*alist
)
80 if (alist
== NULL
|| alist
->type
!= ISCCC_SEXPRTYPE_DOTTEDPAIR
)
83 if (car
== NULL
|| car
->type
!= ISCCC_SEXPRTYPE_STRING
)
85 if (strcmp(car
->value
.as_string
, ALIST_TAG
) != 0)
91 isccc_alist_emptyp(isccc_sexpr_t
*alist
)
93 REQUIRE(isccc_alist_alistp(alist
));
95 if (CDR(alist
) == NULL
)
101 isccc_alist_first(isccc_sexpr_t
*alist
)
103 REQUIRE(isccc_alist_alistp(alist
));
109 isccc_alist_assq(isccc_sexpr_t
*alist
, const char *key
)
111 isccc_sexpr_t
*car
, *caar
;
113 REQUIRE(isccc_alist_alistp(alist
));
116 * Skip alist type tag.
120 while (alist
!= NULL
) {
121 INSIST(alist
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
123 INSIST(car
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
125 if (caar
->type
== ISCCC_SEXPRTYPE_STRING
&&
126 strcmp(caar
->value
.as_string
, key
) == 0)
135 isccc_alist_delete(isccc_sexpr_t
*alist
, const char *key
)
137 isccc_sexpr_t
*car
, *caar
, *rest
, *prev
;
139 REQUIRE(isccc_alist_alistp(alist
));
143 while (rest
!= NULL
) {
144 INSIST(rest
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
146 INSIST(car
!= NULL
&& car
->type
== ISCCC_SEXPRTYPE_DOTTEDPAIR
);
148 if (caar
->type
== ISCCC_SEXPRTYPE_STRING
&&
149 strcmp(caar
->value
.as_string
, key
) == 0) {
150 CDR(prev
) = CDR(rest
);
152 isccc_sexpr_free(&rest
);
161 isccc_alist_define(isccc_sexpr_t
*alist
, const char *key
, isccc_sexpr_t
*value
)
163 isccc_sexpr_t
*kv
, *k
, *elt
;
165 kv
= isccc_alist_assq(alist
, key
);
170 k
= isccc_sexpr_fromstring(key
);
173 kv
= isccc_sexpr_cons(k
, value
);
175 isccc_sexpr_free(&kv
);
178 elt
= isccc_sexpr_addtolist(&alist
, kv
);
180 isccc_sexpr_free(&kv
);
185 * We've already got an entry for this key. Replace it.
187 isccc_sexpr_free(&CDR(kv
));
195 isccc_alist_definestring(isccc_sexpr_t
*alist
, const char *key
, const char *str
)
197 isccc_sexpr_t
*v
, *kv
;
199 v
= isccc_sexpr_fromstring(str
);
202 kv
= isccc_alist_define(alist
, key
, v
);
204 isccc_sexpr_free(&v
);
210 isccc_alist_definebinary(isccc_sexpr_t
*alist
, const char *key
, isccc_region_t
*r
)
212 isccc_sexpr_t
*v
, *kv
;
214 v
= isccc_sexpr_frombinary(r
);
217 kv
= isccc_alist_define(alist
, key
, v
);
219 isccc_sexpr_free(&v
);
225 isccc_alist_lookup(isccc_sexpr_t
*alist
, const char *key
)
229 kv
= isccc_alist_assq(alist
, key
);
236 isccc_alist_lookupstring(isccc_sexpr_t
*alist
, const char *key
, char **strp
)
238 isccc_sexpr_t
*kv
, *v
;
240 kv
= isccc_alist_assq(alist
, key
);
243 if (isccc_sexpr_stringp(v
)) {
245 *strp
= isccc_sexpr_tostring(v
);
246 return (ISC_R_SUCCESS
);
248 return (ISC_R_EXISTS
);
251 return (ISC_R_NOTFOUND
);
255 isccc_alist_lookupbinary(isccc_sexpr_t
*alist
, const char *key
, isccc_region_t
**r
)
257 isccc_sexpr_t
*kv
, *v
;
259 kv
= isccc_alist_assq(alist
, key
);
262 if (isccc_sexpr_binaryp(v
)) {
264 *r
= isccc_sexpr_tobinary(v
);
265 return (ISC_R_SUCCESS
);
267 return (ISC_R_EXISTS
);
270 return (ISC_R_NOTFOUND
);
274 isccc_alist_prettyprint(isccc_sexpr_t
*sexpr
, unsigned int indent
, FILE *stream
)
276 isccc_sexpr_t
*elt
, *kv
, *k
, *v
;
278 if (isccc_alist_alistp(sexpr
)) {
279 fprintf(stream
, "{\n");
281 for (elt
= isccc_alist_first(sexpr
);
285 INSIST(isccc_sexpr_listp(kv
));
288 INSIST(isccc_sexpr_stringp(k
));
289 fprintf(stream
, "%.*s%s => ", (int)indent
, spaces
,
290 isccc_sexpr_tostring(k
));
291 isccc_alist_prettyprint(v
, indent
, stream
);
292 if (CDR(elt
) != NULL
)
293 fprintf(stream
, ",");
294 fprintf(stream
, "\n");
297 fprintf(stream
, "%.*s}", (int)indent
, spaces
);
298 } else if (isccc_sexpr_listp(sexpr
)) {
299 fprintf(stream
, "(\n");
304 fprintf(stream
, "%.*s", (int)indent
, spaces
);
305 isccc_alist_prettyprint(CAR(elt
), indent
, stream
);
306 if (CDR(elt
) != NULL
)
307 fprintf(stream
, ",");
308 fprintf(stream
, "\n");
311 fprintf(stream
, "%.*s)", (int)indent
, spaces
);
313 isccc_sexpr_print(sexpr
, stream
);