From: Lennart Poettering Date: Sun, 21 Aug 2005 16:04:22 +0000 (+0000) Subject: * add two new functions: X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=f61b06f15ae185410e4d5795b4fd22aeb08bbb56;p=catta * add two new functions: * avahi_string_list_add_printf * avahi_string_list_add_vprintf git-svn-id: file:///home/lennart/svn/public/avahi/trunk@383 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-common/malloc.c b/avahi-common/malloc.c index 9b0e22e..2c03d8f 100644 --- a/avahi-common/malloc.c +++ b/avahi-common/malloc.c @@ -192,7 +192,7 @@ void avahi_set_allocator(const AvahiAllocator *a) { } char *avahi_strdup_vprintf(const char *fmt, va_list ap) { - size_t len = 100; + size_t len = 80; char *buf; assert(fmt); diff --git a/avahi-common/strlst-test.c b/avahi-common/strlst-test.c index 12aa623..84d565a 100644 --- a/avahi-common/strlst-test.c +++ b/avahi-common/strlst-test.c @@ -43,6 +43,7 @@ int main(int argc, char *argv[]) { 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'); t = avahi_string_list_to_string(a); printf("--%s--\n", t); diff --git a/avahi-common/strlst.c b/avahi-common/strlst.c index 8a05e02..574b151 100644 --- a/avahi-common/strlst.c +++ b/avahi-common/strlst.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "strlst.h" #include "malloc.h" @@ -38,7 +39,10 @@ AvahiStringList*avahi_string_list_add_anonymous(AvahiStringList *l, size_t size) n->next = l; n->size = size; - + + /* NUL terminate strings, just to make sure */ + n->text[size] = 0; + return n; } @@ -52,7 +56,7 @@ AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8 if (size > 0) memcpy(n->text, text, size); - + return n; } @@ -283,3 +287,53 @@ unsigned avahi_string_list_length(const AvahiStringList *l) { return n; } + +AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va) { + size_t len = 80; + AvahiStringList *r; + + assert(format); + + if (!(r = avahi_malloc(sizeof(AvahiStringList) + len))) + return NULL; + + for (;;) { + int n; + AvahiStringList *nr; + + n = vsnprintf((char*) r->text, len+1, format, va); + + if (n >= 0 && n < (int) len) + break; + + if (n >= 0) + len = n+1; + else + len *= 2; + + if (!(nr = avahi_realloc(r, sizeof(AvahiStringList) + len))) { + avahi_free(r); + return NULL; + } + + r = nr; + } + + + r->next = l; + r->size = strlen((char*) r->text); + + return r; +} + +AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) { + va_list va; + + assert(format); + + va_start(va, format); + l = avahi_string_list_add_vprintf(l, format, va); + va_end(va); + + return l; +} diff --git a/avahi-common/strlst.h b/avahi-common/strlst.h index 10b7aa6..b4e3877 100644 --- a/avahi-common/strlst.h +++ b/avahi-common/strlst.h @@ -66,6 +66,12 @@ void avahi_string_list_free(AvahiStringList *l); * start. */ AvahiStringList *avahi_string_list_add(AvahiStringList *l, const char *text); +/** Append a new NUL terminated formatted string to the specified string list */ +AvahiStringList *avahi_string_list_add_printf(AvahiStringList *l, const char *format, ...) AVAHI_GCC_PRINTF_ATTR23; + +/** Append a new NUL terminated formatted string to the specified string list */ +AvahiStringList *avahi_string_list_add_vprintf(AvahiStringList *l, const char *format, va_list va); + /** Append an arbitrary length byte string to the list. Returns the * new list start. */ AvahiStringList *avahi_string_list_add_arbitrary(AvahiStringList *l, const uint8_t *text, size_t size);