diff --git a/sexp.c b/sexp.c index ca0ad79..46d8635 100644 --- a/sexp.c +++ b/sexp.c @@ -130,3 +130,18 @@ cmsg_bytes_t sexp_data(sexp_t *x) { die("Unknown sexp kind %d in data accessor\n", x->kind); } } + +sexp_t *sexp_assoc(sexp_t *list, cmsg_bytes_t key) { + while (list != NULL) { + sexp_t *candidate = sexp_head(list); + if (sexp_stringp(candidate)) { + cmsg_bytes_t candidate_data = sexp_data(candidate); + if ((candidate_data.len == key.len) + && (!memcmp(candidate_data.bytes, key.bytes, key.len))) { + return candidate; + } + } + list = sexp_tail(list); + } + return NULL; +} diff --git a/sexp.h b/sexp.h index c6d41f1..cfcbc40 100644 --- a/sexp.h +++ b/sexp.h @@ -98,4 +98,6 @@ extern cmsg_bytes_t sexp_data(sexp_t *x); __val; \ }) +extern sexp_t *sexp_assoc(sexp_t *list, cmsg_bytes_t key); + #endif