* 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
}
char *avahi_strdup_vprintf(const char *fmt, va_list ap) {
- size_t len = 100;
+ size_t len = 80;
char *buf;
assert(fmt);
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);
#include <string.h>
#include <stdarg.h>
#include <assert.h>
+#include <stdio.h>
#include "strlst.h"
#include "malloc.h"
n->next = l;
n->size = size;
-
+
+ /* NUL terminate strings, just to make sure */
+ n->text[size] = 0;
+
return n;
}
if (size > 0)
memcpy(n->text, text, size);
-
+
return n;
}
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;
+}
* 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);