#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);
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);
size_t s = 0;
char *t, *e;
- l = avahi_string_list_reverse(l);
-
for (n = l; n; n = n->next) {
if (n != l)
s ++;
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++) = ' ';
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;
+}
/** 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