4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) {
37 if (!(n = avahi_malloc(sizeof(AvahiStringList) + size)))
43 /* NUL terminate strings, just to make sure */
49 AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t*text, size_t size) {
54 if (!(n = avahi_string_list_add_anonymous(l, size)))
58 memcpy(n->text, text, size);
63 AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text) {
66 return avahi_string_list_add_arbitrary(l, (const uint8_t*) text, strlen(text));
69 AvahiStringList *avahi_string_list_parse(const void* data, size_t size) {
70 AvahiStringList *r = NULL;
84 if (k > 0) /* Ignore empty strings */
85 r = avahi_string_list_add_arbitrary(r, c, k);
94 void avahi_string_list_free(AvahiStringList *l) {
104 AvahiStringList* avahi_string_list_reverse(AvahiStringList *l) {
105 AvahiStringList *r = NULL, *n;
117 char* avahi_string_list_to_string(AvahiStringList *l) {
122 for (n = l; n; n = n->next) {
129 if (!(t = e = avahi_new(char, s+1)))
132 l = avahi_string_list_reverse(l);
134 for (n = l; n; n = n->next) {
139 strncpy(e, (char*) n->text, n->size);
147 l = avahi_string_list_reverse(l);
154 size_t avahi_string_list_serialize(AvahiStringList *l, void *data, size_t size) {
163 l = avahi_string_list_reverse(l);
166 for (n = l; n; n = n->next) {
179 memcpy(c, n->text, k);
185 l = avahi_string_list_reverse(l);
189 /* Empty lists are treated specially. To comply with
190 * section 6.1 of the DNS-SD spec, we return a single
191 * empty string (i.e. a NUL byte)*/
194 *(uint8_t*) data = 0;
207 for (n = l; n; n = n->next) {
222 int avahi_string_list_equal(const AvahiStringList *a, const AvahiStringList *b) {
231 if (a->size != b->size)
234 if (a->size != 0 && memcmp(a->text, b->text, a->size) != 0)
242 AvahiStringList *avahi_string_list_add_many(AvahiStringList *r, ...) {
246 r = avahi_string_list_add_many_va(r, va);
252 AvahiStringList *avahi_string_list_add_many_va(AvahiStringList *r, va_list va) {
255 while ((txt = va_arg(va, const char*)))
256 r = avahi_string_list_add(r, txt);
261 AvahiStringList *avahi_string_list_new(const char *txt, ...) {
263 AvahiStringList *r = NULL;
266 r = avahi_string_list_add(r, txt);
269 r = avahi_string_list_add_many_va(r, va);
276 AvahiStringList *avahi_string_list_new_va(va_list va) {
277 return avahi_string_list_add_many_va(NULL, va);
280 AvahiStringList *avahi_string_list_copy(const AvahiStringList *l) {
281 AvahiStringList *r = NULL;
283 for (; l; l = l->next)
284 r = avahi_string_list_add_arbitrary(r, l->text, l->size);
286 return avahi_string_list_reverse(r);
289 AvahiStringList *avahi_string_list_new_from_array(const char *array[], int length) {
290 AvahiStringList *r = NULL;
295 for (i = 0; length >= 0 ? i < length : !!array[i]; i++)
296 r = avahi_string_list_add(r, array[i]);
301 unsigned avahi_string_list_length(const AvahiStringList *l) {
304 for (; l; l = l->next)
310 AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) {
316 if (!(r = avahi_malloc(sizeof(AvahiStringList) + len)))
323 n = vsnprintf((char*) r->text, len+1, format, va);
325 if (n >= 0 && n < (int) len)
333 if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) {
343 r->size = strlen((char*) r->text);
348 AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) {
353 va_start(va, format);
354 l = avahi_string_list_add_vprintf(l, format, va);
360 AvahiStringList *avahi_string_list_find(AvahiStringList *l, const char *key) {
366 for (; l; l = l->next) {
367 if (strcasecmp((char*) l->text, key) == 0)
370 if (strncasecmp((char*) l->text, key, n) == 0 && l->text[n] == '=')
377 AvahiStringList *avahi_string_list_add_pair(AvahiStringList *l, const char *key, const char *value) {
381 return avahi_string_list_add_printf(l, "%s=%s", key, value);
383 return avahi_string_list_add(l, key);
386 AvahiStringList *avahi_string_list_add_pair_arbitrary(AvahiStringList *l, const char *key, const uint8_t *value, size_t size) {
391 return avahi_string_list_add(l, key);
395 if (!(l = avahi_string_list_add_anonymous(l, n + 1 + size)))
398 memcpy(l->text, key, n);
400 memcpy(l->text + n + 1, value, size);
406 int avahi_string_list_get_pair(AvahiStringList *l, char **key, char **value, size_t *size) {
412 if (!(e = memchr(l->text, '=', l->size))) {
414 if (!(*key = avahi_strdup((char*) l->text)))
426 if (!(*key = avahi_strndup((char*) l->text, e - (char *) l->text)))
429 e++; /* Advance after '=' */
431 n = l->size - (e - (char*) l->text);
435 if (!(*value = avahi_memdup(e, n+1))) {
450 AvahiStringList *avahi_string_list_get_next(AvahiStringList *l) {
455 const uint8_t *avahi_string_list_get_text(AvahiStringList *l) {
460 size_t avahi_string_list_get_size(AvahiStringList *l) {