From eb3966f2d8bbfab5554b9312998e1d4812b0e28b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 22 Aug 2005 19:15:22 +0000 Subject: * add four new AvahiStringList functions (this is four you, Sebastien!) git-svn-id: file:///home/lennart/svn/public/avahi/trunk@395 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/strlst.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 5 deletions(-) (limited to 'avahi-common/strlst.c') diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c index 574b151..d1e17be 100644 --- a/avahi-common/strlst.c +++ b/avahi-common/strlst.c @@ -117,8 +117,6 @@ char* avahi_string_list_to_string(AvahiStringList *l) { size_t s = 0; char *t, *e; - l = avahi_string_list_reverse(l); - for (n = l; n; n = n->next) { if (n != l) s ++; @@ -126,11 +124,11 @@ char* avahi_string_list_to_string(AvahiStringList *l) { s += n->size+2; } - if (!(t = e = avahi_new(char, s+1))) { - l = avahi_string_list_reverse(l); + if (!(t = e = avahi_new(char, s+1))) return NULL; - } + l = avahi_string_list_reverse(l); + for (n = l; n; n = n->next) { if (n != l) *(e++) = ' '; @@ -337,3 +335,93 @@ AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *fo return l; } + +AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) { + size_t n; + + assert(key); + n = strlen(key); + + for (; l; l = l->next) { + if (strcasecmp((char*) l->text, key) == 0) + return l; + + if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=') + return l; + } + + return NULL; +} + +AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) { + assert(key); + + if (value) + return avahi_string_list_add_printf(l, "%s=%s", key, value); + else + return avahi_string_list_add(l, key); +} + +AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) { + size_t n; + assert(key); + + if (!value) + return avahi_string_list_add(l, key); + + n = strlen(key); + + if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size))) + return NULL; + + memcpy(l->text, key, n); + l->text[n] = '='; + memcpy(l->text + n + 1, value, size); + + return l; +} + + +int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) { + char *e; + + assert(l); + assert(key); + + if (!(e = memchr(l->text, '=', l->size))) { + + if (!(*key = avahi_strdup((char*) l->text))) + return -1; + + if (value) + *value = NULL; + + if (size) + *size = 0; + + } else { + size_t n; + + if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text))) + return -1; + + e++; /* Advance after '=' */ + + n = l->size - (e - (char*) l->text); + + if (value) { + + if (!(*value = avahi_memdup(e, n+1))) { + avahi_free(*key); + return -1; + } + + (*value)[n] = 0; + } + + if (size) + *size = n; + } + + return 0; +} -- cgit