23 #define DEFAULT_STRBUF_ALLOC_SZ 1024
31 sb = calloc(1,
sizeof(*sb));
45 ssize_t avail, needed;
47 va_list args, args_copy;
50 if (sb->
data == NULL) {
51 sb->
pos = sb->
data = malloc(DEFAULT_STRBUF_ALLOC_SZ);
54 sb->
bufsz = DEFAULT_STRBUF_ALLOC_SZ;
59 va_copy(args_copy, args);
60 needed = vsnprintf(NULL, 0, fmt, args_copy) + 1;
76 ssize_t new_bufsz = 2 * sb->
bufsz;
81 while (new_bufsz - (ssize_t) sb->
bufsz < needed)
83 assert(sb->
bufsz > 0);
84 ptr = realloc(sb->
data, new_bufsz);
93 sb->
bufsz = new_bufsz;
97 status = vsnprintf(sb->
pos, needed + 1, fmt, args);
119 void *ptr = sb->
data;
121 ptr = realloc(sb->
data, DEFAULT_STRBUF_ALLOC_SZ);
129 sb->
bufsz = DEFAULT_STRBUF_ALLOC_SZ;
size_t nmsg_strbuf_len(struct nmsg_strbuf *sb)
Find the length of the used portion of the string buffer.
size_t bufsz
size of data allocation
struct nmsg_strbuf * nmsg_strbuf_init(void)
Initialize a string buffer.
char * data
buffer for string data
void nmsg_strbuf_destroy(struct nmsg_strbuf **sb)
Destroy all resources associated with a string buffer.
nmsg_res nmsg_strbuf_append(struct nmsg_strbuf *sb, const char *fmt,...)
Append to a string buffer.
nmsg_res nmsg_strbuf_reset(struct nmsg_strbuf *sb)
Reset a string buffer.