From: Lennart Poettering Date: Mon, 22 Aug 2005 19:15:22 +0000 (+0000) Subject: * add four new AvahiStringList functions (this is four you, Sebastien!) X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=eb3966f2d8bbfab5554b9312998e1d4812b0e28b;p=catta * 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 --- diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c index 84d565a..a4c758f 100644 --- a/avahi-common/strlst-test.c +++ b/avahi-common/strlst-test.c @@ -30,20 +30,25 @@ #include "malloc.h" int main(int argc, char *argv[]) { - char *t; + char *t, *v; uint8_t data[1024]; - AvahiStringList *a = NULL, *b; + AvahiStringList *a = NULL, *b, *p; size_t size, n; + int r; a = avahi_string_list_new("prefix", "a", "b", NULL); a = avahi_string_list_add(a, "start"); - a = avahi_string_list_add(a, "foo"); + a = avahi_string_list_add(a, "foo=99"); a = avahi_string_list_add(a, "bar"); a = avahi_string_list_add(a, "quux"); a = avahi_string_list_add_arbitrary(a, (const uint8_t*) "null\0null", 9); - a = avahi_string_list_add(a, "end"); a = avahi_string_list_add_printf(a, "seven=%i %c", 7, 'x'); + a = avahi_string_list_add_pair(a, "blubb", "blaa"); + a = avahi_string_list_add_pair(a, "uxknurz", NULL); + a = avahi_string_list_add_pair_arbitrary(a, "uxknurz2", (const uint8_t*) "blafasel\0oerks", 14); + + a = avahi_string_list_add(a, "end"); t = avahi_string_list_to_string(a); printf("--%s--\n", t); @@ -79,6 +84,30 @@ int main(int argc, char *argv[]) { t = avahi_string_list_to_string(b); printf("--%s--\n", t); avahi_free(t); + + p = avahi_string_list_find(a, "seven"); + assert(p); + + r = avahi_string_list_get_pair(p, &t, &v, NULL); + assert(r >= 0); + assert(t); + assert(v); + + printf("<%s>=<%s>\n", t, v); + avahi_free(t); + avahi_free(v); + + p = avahi_string_list_find(a, "quux"); + assert(p); + + r = avahi_string_list_get_pair(p, &t, &v, NULL); + assert(r >= 0); + assert(t); + assert(!v); + + printf("<%s>=<%s>\n", t, v); + avahi_free(t); + avahi_free(v); avahi_string_list_free(a); avahi_string_list_free(b); 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; +} diff --git a/avahi-common/strlst.h b/avahi-common/strlst.h index b4e3877..47fd14b 100644 --- a/avahi-common/strlst.h +++ b/avahi-common/strlst.h @@ -116,6 +116,23 @@ AvahiStringList* avahi_string_list_reverse(AvahiStringList *l); /** Return the number of elements in the string list */ unsigned avahi_string_list_length(const AvahiStringList *l); +/** Find the string list entry for the given DNS-SD TXT key */ +AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key); + +/** Return the DNS-SD TXT key and value for the specified string list + * item. If size is not NULL it will be filled with the length of + * value. (for strings containing NUL bytes). If the entry doesn't + * contain a value *value will be set to NULL. You need to + * avahi_free() the strings returned in *key and *value. */ +int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size); + +/** Add a new DNS-SD TXT key value pair to the string list. value may + * be NULL in case you want to specify a key without a value */ +AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value); + +/** Same as avahi_string_list_add_pair() but allow strings containing NUL bytes in *value. */ +AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size); + AVAHI_C_DECL_END #endif